archivo del paquete→ .shtukbase/packages/design-components/actions/place.ts
actions/place.ts
38 líneas, 2.1 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // design-components `place`: adds the kind `Component` to the placement map `/base/ontology/placement.md` of the package
- // `layers` (a line in the `placement:` head and a row in the table), so that the layers check knows where components
- // lie. A kind already in the map is left as it is, so the step can run on every install and update. Without the map
- // there is nothing to add to: a line on stderr. The working directory is the project root.
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- const MAP = 'base/ontology/placement.md';
- const KIND = { itemType: 'Component', path: '/design/components', title: 'Design components' };
- export function place(root = process.cwd()): boolean {
- const file = join(root, MAP);
- if (!existsSync(file)) {
- console.error(`no map /${MAP}: kind ${KIND.itemType} was not added (install the package layers)`);
- return false;
- }
- const text = readFileSync(file, 'utf8');
- if (new RegExp(`itemType:\\s*"?${KIND.itemType}"?\\s*[,}]`).test(text)) {
- console.log('placement map: kind Component is already in place');
- return false;
- }
- const lines = text.split('\n');
- const end = lines.indexOf('---', 1);
- if (end < 0) { console.error(`/${MAP} has no header: kind ${KIND.itemType} was not added`); return false; }
- let at = -1;
- for (let i = 1; i < end; i++) if (/^\s+- \{itemType:/.test(lines[i])) at = i;
- if (at < 0) at = lines.findIndex((l, i) => i < end && /^placement:\s*$/.test(l));
- if (at < 0) { console.error(`/${MAP} has no placement: list: kind ${KIND.itemType} was not added`); return false; }
- lines.splice(at + 1, 0, ` - {itemType: ${KIND.itemType}, path: ${KIND.path}, writer: all, reader: all}`);
- let last = -1;
- for (let i = lines.indexOf('---', 1) + 1; i < lines.length; i++) if (lines[i].startsWith('|')) last = i;
- if (last >= 0) lines.splice(last + 1, 0, `| ${KIND.title} | \`${KIND.itemType}\` | \`${KIND.path}\` | all | all | the collection is brought by the package \`design-components\` |`);
- writeFileSync(file, lines.join('\n'));
- console.log(`placement map: added kind ${KIND.itemType}`);
- return true;
- }
- if (import.meta.main) place();