ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-components/actions/place.ts

actions/place.ts

38 строк, 2.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // design-components `place`: adds the kind `Component` to the placement map `/base/ontology/placement.md` of the package
  2. // `layers` (a line in the `placement:` head and a row in the table), so that the layers check knows where components
  3. // 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
  4. // there is nothing to add to: a line on stderr. The working directory is the project root.
  5. import { existsSync, readFileSync, writeFileSync } from 'node:fs';
  6. import { join } from 'node:path';
  7. const MAP = 'base/ontology/placement.md';
  8. const KIND = { itemType: 'Component', path: '/design/components', title: 'Design components' };
  9. export function place(root = process.cwd()): boolean {
  10. const file = join(root, MAP);
  11. if (!existsSync(file)) {
  12. console.error(`no map /${MAP}: kind ${KIND.itemType} was not added (install the package layers)`);
  13. return false;
  14. }
  15. const text = readFileSync(file, 'utf8');
  16. if (new RegExp(`itemType:\\s*"?${KIND.itemType}"?\\s*[,}]`).test(text)) {
  17. console.log('placement map: kind Component is already in place');
  18. return false;
  19. }
  20. const lines = text.split('\n');
  21. const end = lines.indexOf('---', 1);
  22. if (end < 0) { console.error(`/${MAP} has no header: kind ${KIND.itemType} was not added`); return false; }
  23. let at = -1;
  24. for (let i = 1; i < end; i++) if (/^\s+- \{itemType:/.test(lines[i])) at = i;
  25. if (at < 0) at = lines.findIndex((l, i) => i < end && /^placement:\s*$/.test(l));
  26. if (at < 0) { console.error(`/${MAP} has no placement: list: kind ${KIND.itemType} was not added`); return false; }
  27. lines.splice(at + 1, 0, ` - {itemType: ${KIND.itemType}, path: ${KIND.path}, writer: all, reader: all}`);
  28. let last = -1;
  29. for (let i = lines.indexOf('---', 1) + 1; i < lines.length; i++) if (lines[i].startsWith('|')) last = i;
  30. 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\` |`);
  31. writeFileSync(file, lines.join('\n'));
  32. console.log(`placement map: added kind ${KIND.itemType}`);
  33. return true;
  34. }
  35. if (import.meta.main) place();