包文件→ .shtukbase/packages/human-gate/hooks/lib.ts
hooks/lib.ts
69 行, 3.2 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // The human gate: which commands wait for a person's yes (/base/human-gate.md), and the approval records where the
- // agent asks and the person answers (/work/approvals).
- import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
- export const FOLDER = 'work/approvals';
- /** Fields only a person writes. */
- export const PERSON_FIELDS = ['answer', 'answeredOn', 'usedOn'];
- export type Gated = { pattern: RegExp; why: string };
- /** Used when the project has no /base/human-gate.md (an empty `gated: []` there gates nothing). */
- const DEFAULTS: [string, string][] = [
- ['\\b(npm|bun|pnpm|yarn)\\s+publish\\b', 'publishing a package for everyone'],
- ['\\bgh\\s+(pr|issue|release)\\s+create\\b', 'publishing on GitHub'],
- ['\\bgh\\s+(pr|issue)\\s+(comment|review)\\b', 'a message to people on GitHub'],
- ['\\bgit\\s+push\\b[^;&|]*\\s(--force|-f)\\b', 'rewriting history on the server'],
- ['\\bsendmail\\b', 'a letter to people'],
- ];
- export function gated(root: string): Gated[] {
- const page = join(root, 'base', 'human-gate.md');
- let rows: { pattern: string; why: string }[] = DEFAULTS.map(([pattern, why]) => ({ pattern, why }));
- if (existsSync(page)) {
- const fm = parseYaml(splitFrontMatter(readFileSync(page, 'utf8'))[0]);
- if (Array.isArray(fm.gated)) rows = fm.gated.filter((g: any) => g && g.pattern).map((g: any) => ({ pattern: pystr(g.pattern), why: pystr(g.why ?? '') }));
- }
- const out: Gated[] = [];
- for (const r of rows)
- try {
- out.push({ pattern: new RegExp(r.pattern), why: r.why });
- } catch {
- // A pattern that is not a regular expression is skipped; the page's own text says how to write one.
- }
- return out;
- }
- export type Approval = { file: string; addr: string; fm: Record<string, any> };
- export function approvals(root: string): Approval[] {
- const dir = join(root, FOLDER);
- if (!existsSync(dir)) return [];
- return readdirSync(dir).filter((n) => n.endsWith('.md') && !n.startsWith('.')).sort().map((n) => {
- const file = join(dir, n);
- return { file, addr: `/${FOLDER}/${n}`, fm: parseYaml(splitFrontMatter(readFileSync(file, 'utf8'))[0]) };
- });
- }
- export const norm = (s: unknown) => pystr(s ?? '').trim().replace(/\s+/g, ' ');
- const YES = ['yes', 'true'];
- const NO = ['no', 'false'];
- /** The person's answer: yes, no, or '' (none yet); YAML may read yes/no as booleans. */
- export function answer(a: Approval): 'yes' | 'no' | '' {
- const v = norm(a.fm.answer).toLowerCase();
- return YES.includes(v) ? 'yes' : NO.includes(v) ? 'no' : '';
- }
- export const empty = (v: unknown) => v === null || v === undefined || norm(v) === '';
- /** Marks the approval used: `usedOn: <today>` goes into its front matter, so one yes lets one run through. */
- export function markUsed(a: Approval, today: string): void {
- const text = readFileSync(a.file, 'utf8');
- const lines = text.split('\n');
- const end = lines.findIndex((l, i) => i > 0 && l.trimEnd() === '---');
- if (lines[0]?.trim() !== '---' || end < 0) return;
- lines.splice(end, 0, `usedOn: ${today}`);
- writeFileSync(a.file, lines.join('\n'));
- }
- export const today = () => new Date().toISOString().slice(0, 10);