ShtukbaseStandard
包文件→ .shtukbase/packages/human-gate/hooks/lib.ts

hooks/lib.ts

69 行, 3.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The human gate: which commands wait for a person's yes (/base/human-gate.md), and the approval records where the
  2. // agent asks and the person answers (/work/approvals).
  3. import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
  6. export const FOLDER = 'work/approvals';
  7. /** Fields only a person writes. */
  8. export const PERSON_FIELDS = ['answer', 'answeredOn', 'usedOn'];
  9. export type Gated = { pattern: RegExp; why: string };
  10. /** Used when the project has no /base/human-gate.md (an empty `gated: []` there gates nothing). */
  11. const DEFAULTS: [string, string][] = [
  12. ['\\b(npm|bun|pnpm|yarn)\\s+publish\\b', 'publishing a package for everyone'],
  13. ['\\bgh\\s+(pr|issue|release)\\s+create\\b', 'publishing on GitHub'],
  14. ['\\bgh\\s+(pr|issue)\\s+(comment|review)\\b', 'a message to people on GitHub'],
  15. ['\\bgit\\s+push\\b[^;&|]*\\s(--force|-f)\\b', 'rewriting history on the server'],
  16. ['\\bsendmail\\b', 'a letter to people'],
  17. ];
  18. export function gated(root: string): Gated[] {
  19. const page = join(root, 'base', 'human-gate.md');
  20. let rows: { pattern: string; why: string }[] = DEFAULTS.map(([pattern, why]) => ({ pattern, why }));
  21. if (existsSync(page)) {
  22. const fm = parseYaml(splitFrontMatter(readFileSync(page, 'utf8'))[0]);
  23. 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 ?? '') }));
  24. }
  25. const out: Gated[] = [];
  26. for (const r of rows)
  27. try {
  28. out.push({ pattern: new RegExp(r.pattern), why: r.why });
  29. } catch {
  30. // A pattern that is not a regular expression is skipped; the page's own text says how to write one.
  31. }
  32. return out;
  33. }
  34. export type Approval = { file: string; addr: string; fm: Record<string, any> };
  35. export function approvals(root: string): Approval[] {
  36. const dir = join(root, FOLDER);
  37. if (!existsSync(dir)) return [];
  38. return readdirSync(dir).filter((n) => n.endsWith('.md') && !n.startsWith('.')).sort().map((n) => {
  39. const file = join(dir, n);
  40. return { file, addr: `/${FOLDER}/${n}`, fm: parseYaml(splitFrontMatter(readFileSync(file, 'utf8'))[0]) };
  41. });
  42. }
  43. export const norm = (s: unknown) => pystr(s ?? '').trim().replace(/\s+/g, ' ');
  44. const YES = ['yes', 'true'];
  45. const NO = ['no', 'false'];
  46. /** The person's answer: yes, no, or '' (none yet); YAML may read yes/no as booleans. */
  47. export function answer(a: Approval): 'yes' | 'no' | '' {
  48. const v = norm(a.fm.answer).toLowerCase();
  49. return YES.includes(v) ? 'yes' : NO.includes(v) ? 'no' : '';
  50. }
  51. export const empty = (v: unknown) => v === null || v === undefined || norm(v) === '';
  52. /** Marks the approval used: `usedOn: <today>` goes into its front matter, so one yes lets one run through. */
  53. export function markUsed(a: Approval, today: string): void {
  54. const text = readFileSync(a.file, 'utf8');
  55. const lines = text.split('\n');
  56. const end = lines.findIndex((l, i) => i > 0 && l.trimEnd() === '---');
  57. if (lines[0]?.trim() !== '---' || end < 0) return;
  58. lines.splice(end, 0, `usedOn: ${today}`);
  59. writeFileSync(a.file, lines.join('\n'));
  60. }
  61. export const today = () => new Date().toISOString().slice(0, 10);