ShtukbaseStandard
package file→ .shtukbase/packages/blueprint-content-factory/lib/facts.ts

lib/facts.ts

22 lines, 1.2 KB.

  1. // The answers of a blueprint's start questions: the facts in the head of /base/packages.md, and for a question
  2. // nobody answered (an `install` without --fact) the default from the blueprint's own package.md.
  3. import { existsSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { pystr, readPage } from '../../collections/checks/lib/pages.ts';
  6. export type Facts = Record<string, unknown>;
  7. export function facts(pkgDir: string, root = process.cwd()): Facts {
  8. const list = join(root, 'base/packages.md');
  9. const given = existsSync(list) ? readPage(list).fm.facts : null;
  10. const out: Facts = {};
  11. const qs = readPage(join(pkgDir, 'package.md')).fm.questions;
  12. for (const q of Array.isArray(qs) ? qs : []) if (q && typeof q === 'object' && 'default' in q) out[String(q.fact)] = q.default;
  13. if (given && typeof given === 'object') Object.assign(out, Object.fromEntries(Object.entries(given).filter(([, v]) => v !== null && v !== '')));
  14. return out;
  15. }
  16. export const str = (v: unknown): string => (v === null || v === undefined ? '' : pystr(v).trim());
  17. /** A list fact: a YAML list or a comma-separated text. */
  18. export const list = (v: unknown): string[] =>
  19. (Array.isArray(v) ? v.map(str) : str(v).split(',')).map((s) => s.trim()).filter(Boolean);