archivo del paquete→ .shtukbase/packages/blueprint-events/actions/setup.ts
actions/setup.ts
67 líneas, 3.0 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // On install: the one start answer the shtuk tool cannot put into the pages by itself. The tool fills
- // {{fact:key}} when it lays the pages; this action replaces, in the blueprint's own pages (the ones the lock lists
- // for it), {{model:key}} with the default model of the harness package the fact names (its adapter.ts).
- // A marker in a page header must stand inside double quotes; the value is escaped for them.
- // Usage: bun actions/setup.ts [--id <package id>] (from the project root; the id defaults to this folder's name)
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- import { basename, dirname, join } from 'node:path';
- const root = process.cwd();
- const at = process.argv.indexOf('--id');
- const id = at > 0 ? process.argv[at + 1] : basename(dirname(dirname(Bun.fileURLToPath(import.meta.url))));
- const yaml = (t: string): any => (Bun.YAML.parse(t) as any) ?? {};
- const header = (t: string): any => {
- const m = /^---\n([\s\S]*?)\n---/.exec(t);
- return m ? yaml(m[1]) : {};
- };
- const pkgs = join(root, 'base', 'packages.md');
- const facts: Record<string, unknown> = existsSync(pkgs) ? header(readFileSync(pkgs, 'utf8')).facts ?? {} : {};
- function fact(key: string): string {
- let cur: any = key in facts ? facts[key] : facts;
- if (!(key in facts)) for (const p of key.split('.')) cur = cur && typeof cur === 'object' && p in cur ? cur[p] : undefined;
- return cur == null ? '' : String(cur);
- }
- const models = new Map<string, string>();
- async function model(key: string): Promise<string> {
- const h = fact(key);
- if (!models.has(h)) {
- const f = join(root, '.shtukbase', 'packages', `harness-${h}`, 'adapter.ts');
- models.set(h, existsSync(f) ? String((await import(f)).adapter?.defaultModel ?? '') : '');
- }
- return models.get(h)!;
- }
- const pages = Object.keys(yaml(readFileSync(join(root, '.shtukbase', 'packages.lock'), 'utf8')).packages?.[id]?.pages ?? {});
- const MARK = /\{\{\s*model:([\w.-]+)\s*\}\}/g;
- const missing = new Set<string>();
- let changed = 0;
- async function fill(text: string, quoted: boolean): Promise<string> {
- let out = '', last = 0;
- for (const m of text.matchAll(MARK)) {
- const v = await model(m[1]);
- if (!v) missing.add(m[1]);
- out += text.slice(last, m.index) + (quoted ? v.replace(/\\/g, '\\\\').replace(/"/g, '\\"') : v);
- last = m.index! + m[0].length;
- }
- return out + text.slice(last);
- }
- for (const rel of pages) {
- const file = join(root, rel);
- if (!existsSync(file)) continue;
- const text = readFileSync(file, 'utf8');
- if (!text.includes('{{')) continue;
- const m = /^---\n[\s\S]*?\n---\n?/.exec(text);
- const head = m ? await fill(m[0], true) : '';
- const next = head + (await fill(text.slice(head ? m![0].length : 0), false));
- if (next !== text) {
- writeFileSync(file, next);
- changed++;
- }
- }
- console.log(`${id}: the model of the harness is written into ${changed} pages`);
- if (missing.size) console.log(` no model for the harness from ${[...missing].join(', ')} — an empty value was written, fix the pages by hand`);