файл пакета→ .shtukbase/packages/agents/lib/place.ts
lib/place.ts
44 строки, 2.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // Adds the kinds of records a factory package brings to the placement map `/base/ontology/placement.md` (package
- // layers): a line in the `placement:` head and a row in the table. A kind already in the map is left as it is, so the
- // step can run on every install and update. The working directory is the project root.
- 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;
- }