ShtukbaseStandard
файл пакета→ .shtukbase/packages/hooks/hooks/record-pre.ts

hooks/record-pre.ts

52 строки, 2.6 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // HK-R02, before the write (PreToolUse on Write, Edit, MultiEdit). A record of a collection that would break its
  2. // schema is not written: the hook works out the file as the tool would leave it and blocks when
  3. // - the record has no front matter, or front matter that read strictly before and no longer does;
  4. // - `title` is empty;
  5. // - a field the schema marks `required: true`, or `requiredWhen` for the record's values, is empty.
  6. // Links and option values need the rest of the base: record-post.ts checks them right after the write.
  7. import { existsSync, readFileSync } from 'node:fs';
  8. import { join } from 'node:path';
  9. import { schemaProperties, splitFrontMatter, parseYaml } from '../../collections/checks/lib/pages.ts';
  10. import { empty, values } from '../../collections/checks/lib/schemalib.ts';
  11. import { BLOCK, collectionOf, contentAfter, projectPath, projectRoot, readPayload } from '../lib/hooklib.ts';
  12. const strict = (head: string): boolean => {
  13. try {
  14. const v = Bun.YAML.parse(head);
  15. return !!v && typeof v === 'object' && !Array.isArray(v);
  16. } catch {
  17. return false;
  18. }
  19. };
  20. const { p } = readPayload();
  21. if (!p) process.exit(0);
  22. const root = projectRoot(p);
  23. const input = p.tool_input ?? {};
  24. const rel = projectPath(root, input.file_path);
  25. const coll = rel ? collectionOf(root, rel) : null;
  26. if (!rel || !coll) process.exit(0);
  27. const text = contentAfter(root, String(p.tool_name ?? ''), input);
  28. if (text === null) process.exit(0);
  29. const out: string[] = [];
  30. const [head] = splitFrontMatter(text);
  31. const before = existsSync(join(root, rel)) ? splitFrontMatter(readFileSync(join(root, rel), 'utf8'))[0] : '';
  32. if (!head.trim()) out.push('no head (front matter between the --- lines)');
  33. else {
  34. if (!strict(head) && (!before || strict(before))) out.push('the head cannot be read as YAML');
  35. const fm = parseYaml(head);
  36. if (empty(fm.title)) out.push('empty title');
  37. for (const [name, spec] of Object.entries(schemaProperties(root, coll.slice(1) + '.md'))) {
  38. if (!spec || typeof spec !== 'object' || !empty(fm[name])) continue;
  39. const s = spec as Record<string, any>;
  40. if (s.required === true) out.push(`empty required field ${name}`);
  41. else if (s.requiredWhen && typeof s.requiredWhen === 'object')
  42. for (const [f, vals] of Object.entries(s.requiredWhen))
  43. if (values(vals).map(String).includes(String(fm[f] ?? ''))) out.push(`empty field ${name}: it is required when ${f}: ${fm[f]}`);
  44. }
  45. }
  46. if (out.length) {
  47. process.stderr.write(`The record ${rel} (collection ${coll}) was not written: ${out.join('; ')}. Norm HK-R02: a database record that breaks a check does not reach the disk silently. Fix the record and try again.\n`);
  48. process.exit(BLOCK);
  49. }