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.
- // Puts the kind Approval into the placement map /base/ontology/placement.md on install and update (the map asks a
- // package that brings a collection to add its kind). A copy of agents/lib/place.ts: the package does not need agents.
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- export type Row = { itemType: string; path: string; title: string; pkg: string };
- const MAP = 'base/ontology/placement.md';
- /** Adds the missing rows; returns the kinds it added. Without the map there is nothing to add to: a line on stderr. */
- export function place(rows: Row[], root = process.cwd()): string[] {
- const file = join(root, MAP);
- if (!existsSync(file)) {
- console.error(`no map /${MAP}: kinds ${rows.map((r) => r.itemType).join(', ')} were not added (install the package layers)`);
- return [];
- }
- let text = readFileSync(file, 'utf8');
- const added: string[] = [];
- for (const r of rows) {
- if (new RegExp(`itemType:\\s*"?${r.itemType}"?\\s*[,}]`).test(text)) continue;
- const head = ` - {itemType: ${r.itemType}, path: ${r.path}, writer: all, reader: all}`;
- const row = `| ${r.title} | \`${r.itemType}\` | \`${r.path}\` | all | all | the collection is brought by the package \`${r.pkg}\` |`;
- const lines = text.split('\n');
- const end = lines.indexOf('---', 1);
- if (end < 0) continue;
- let at = -1;
- for (let i = 1; i < end; i++) if (/^\s+- \{itemType:/.test(lines[i])) at = i;
- if (at < 0) {
- const p = lines.findIndex((l, i) => i < end && /^placement:\s*$/.test(l));
- if (p < 0) continue;
- at = p;
- }
- lines.splice(at + 1, 0, head);
- let last = -1;
- for (let i = end + 1; i < lines.length; i++) if (lines[i].startsWith('|')) last = i;
- if (last >= 0) lines.splice(last + 1, 0, row);
- text = lines.join('\n');
- added.push(r.itemType);
- }
- if (added.length) writeFileSync(file, text);
- console.log(added.length ? `placement map: added kinds ${added.join(', ')}` : 'placement map: all kinds are already in place');
- return added;
- }
- if (import.meta.main) place([{ itemType: 'Approval', path: '/work/approvals', title: 'Approvals', pkg: 'human-gate' }]);