ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/human-gate/bin/place.ts

bin/place.ts

45 líneas, 2.1 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // Puts the kind Approval into the placement map /base/ontology/placement.md on install and update (the map asks a
  2. // package that brings a collection to add its kind). A copy of agents/lib/place.ts: the package does not need agents.
  3. import { existsSync, readFileSync, writeFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. export type Row = { itemType: string; path: string; title: string; pkg: string };
  6. const MAP = 'base/ontology/placement.md';
  7. /** Adds the missing rows; returns the kinds it added. Without the map there is nothing to add to: a line on stderr. */
  8. export function place(rows: Row[], root = process.cwd()): string[] {
  9. const file = join(root, MAP);
  10. if (!existsSync(file)) {
  11. console.error(`no map /${MAP}: kinds ${rows.map((r) => r.itemType).join(', ')} were not added (install the package layers)`);
  12. return [];
  13. }
  14. let text = readFileSync(file, 'utf8');
  15. const added: string[] = [];
  16. for (const r of rows) {
  17. if (new RegExp(`itemType:\\s*"?${r.itemType}"?\\s*[,}]`).test(text)) continue;
  18. const head = ` - {itemType: ${r.itemType}, path: ${r.path}, writer: all, reader: all}`;
  19. const row = `| ${r.title} | \`${r.itemType}\` | \`${r.path}\` | all | all | the collection is brought by the package \`${r.pkg}\` |`;
  20. const lines = text.split('\n');
  21. const end = lines.indexOf('---', 1);
  22. if (end < 0) continue;
  23. let at = -1;
  24. for (let i = 1; i < end; i++) if (/^\s+- \{itemType:/.test(lines[i])) at = i;
  25. if (at < 0) {
  26. const p = lines.findIndex((l, i) => i < end && /^placement:\s*$/.test(l));
  27. if (p < 0) continue;
  28. at = p;
  29. }
  30. lines.splice(at + 1, 0, head);
  31. let last = -1;
  32. for (let i = end + 1; i < lines.length; i++) if (lines[i].startsWith('|')) last = i;
  33. if (last >= 0) lines.splice(last + 1, 0, row);
  34. text = lines.join('\n');
  35. added.push(r.itemType);
  36. }
  37. if (added.length) writeFileSync(file, text);
  38. console.log(added.length ? `placement map: added kinds ${added.join(', ')}` : 'placement map: all kinds are already in place');
  39. return added;
  40. }
  41. if (import.meta.main) place([{ itemType: 'Approval', path: '/work/approvals', title: 'Approvals', pkg: 'human-gate' }]);