package file→ .shtukbase/packages/blueprint-content-factory/lib/facts.ts
lib/facts.ts
22 lines, 1.2 KB.
- // The answers of a blueprint's start questions: the facts in the head of /base/packages.md, and for a question
- // nobody answered (an `install` without --fact) the default from the blueprint's own package.md.
- import { existsSync } from 'node:fs';
- import { join } from 'node:path';
- import { pystr, readPage } from '../../collections/checks/lib/pages.ts';
- export type Facts = Record<string, unknown>;
- export function facts(pkgDir: string, root = process.cwd()): Facts {
- const list = join(root, 'base/packages.md');
- const given = existsSync(list) ? readPage(list).fm.facts : null;
- const out: Facts = {};
- const qs = readPage(join(pkgDir, 'package.md')).fm.questions;
- for (const q of Array.isArray(qs) ? qs : []) if (q && typeof q === 'object' && 'default' in q) out[String(q.fact)] = q.default;
- if (given && typeof given === 'object') Object.assign(out, Object.fromEntries(Object.entries(given).filter(([, v]) => v !== null && v !== '')));
- return out;
- }
- export const str = (v: unknown): string => (v === null || v === undefined ? '' : pystr(v).trim());
- /** A list fact: a YAML list or a comma-separated text. */
- export const list = (v: unknown): string[] =>
- (Array.isArray(v) ? v.map(str) : str(v).split(',')).map((s) => s.trim()).filter(Boolean);