包文件→ .shtukbase/packages/quality/actions/lib/place.ts
actions/lib/place.ts
39 行, 2.2 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Adds the record types a package brings to the placement map /base/ontology/placement.md (package `layers`):
- // a `placement:` row in the head and a row in the table. A row whose itemType is already in the map is left alone,
- // so running it again changes nothing. Without the map (no `layers`) it does nothing. The working directory is the
- // project root.
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- export type Place = { itemType: string; path: string; label: string; pkg: string; records?: string };
- const MAP = 'base/ontology/placement.md';
- export function place(rows: Place[], root = process.cwd()): string[] {
- const file = `${root}/${MAP}`;
- if (!existsSync(file)) return [`no /${MAP}: the placement map is installed by the package layers, rows were not added`];
- let text = readFileSync(file, 'utf8');
- const end = text.indexOf('\n---', 3);
- if (!text.startsWith('---') || end < 0) return [`/${MAP}: no head, rows were not added`];
- let head = text.slice(0, end), body = text.slice(end);
- const done: string[] = [];
- for (const r of rows) {
- if (new RegExp(`itemType:\\s*["']?${r.itemType}["']?\\s*[,}]`).test(head)) continue;
- const line = ` - {itemType: ${r.itemType}, path: ${r.path}${r.records ? `, records: ${r.records}` : ''}, writer: all, reader: all}`;
- const lines = head.split('\n');
- let at = lines.findIndex((l) => /^placement:\s*$/.test(l));
- if (at < 0) { lines.push('placement:'); at = lines.length - 1; }
- let last = at;
- while (last + 1 < lines.length && /^\s+-\s/.test(lines[last + 1])) last++;
- lines.splice(last + 1, 0, line);
- head = lines.join('\n');
- const tline = `| ${r.label} | \`${r.itemType}\` | \`${r.path}\` | all | all | the collection is brought by the package \`${r.pkg}\` |`;
- const bl = body.split('\n');
- let lastRow = -1;
- for (let i = 0; i < bl.length; i++) if (/^\|.*\|\s*$/.test(bl[i])) lastRow = i;
- if (lastRow >= 0) bl.splice(lastRow + 1, 0, tline);
- body = bl.join('\n');
- done.push(r.itemType);
- }
- if (done.length) writeFileSync(file, head + body);
- return [done.length ? `placement map: added ${done.join(', ')}` : 'placement map: all kinds are already in place'];
- }