#!/usr/bin/env node /** * cli:scaffold-theme — Bootstrap src/index.css for the generated app. * * Idempotent: skips writes if existing file content matches. Honors a * `/* @customised *\/` marker at the top of the existing file (preserves it). */ import { parseArgs } from 'node:util'; import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { validate } from './validate.js'; import { generate } from './generate.js'; import { generateEnvelope, failGenerate, printEnvelope } from '../../../../../lib/output.js'; import { readSpecArg } from '../../../../../lib/spec-arg.js'; import { assertWebProjectRoot, safeJoinPath } from '../../../../../lib/fs.js'; const COMMAND = 'scaffold-theme'; function main(): void { const { values } = parseArgs({ options: { spec: { type: 'string' }, 'spec-file': { type: 'string' }, dry_run: { type: 'boolean', default: false }, }, strict: true, }); const specSrc = readSpecArg(values) if ('error' in specSrc) { printEnvelope(failGenerate(COMMAND, [specSrc.error])) process.exit(1) } let raw: unknown; try { raw = JSON.parse(specSrc.raw); } catch { printEnvelope(failGenerate(COMMAND, ['Invalid JSON in --spec'])); process.exit(1); } const validation = validate(raw); if (!validation.valid || !validation.spec) { printEnvelope(failGenerate(COMMAND, validation.errors)); process.exit(1); } const spec = validation.spec; const files = generate(spec); if (values.dry_run) { printEnvelope(generateEnvelope(COMMAND, { data: { dryRun: true, files: files.map((f) => f.path) }, warnings: validation.warnings, })); process.exit(0); } // Fail-closed guard: refuse to write into a .NET backend / repo root. Frontend // scaffolds write to /src/… — a wrong projectPath (the repo root) // would land files in the backend's src/ and trigger destructive "cleanup". try { assertWebProjectRoot(spec.projectPath); } catch (e) { printEnvelope(failGenerate(COMMAND, [e instanceof Error ? e.message : String(e)])); process.exit(1); } const filesCreated: string[] = []; const filesModified: string[] = []; const skipped: string[] = []; for (const file of files) { const abs = resolve(safeJoinPath(spec.projectPath, file.path)); if (existsSync(abs)) { const existing = readFileSync(abs, 'utf-8'); if (existing.trimStart().startsWith('/* @customised')) { skipped.push(file.path); continue; } if (!spec.force && existing === file.content) { // No drift — no-op. continue; } mkdirSync(dirname(abs), { recursive: true }); writeFileSync(abs, file.content, 'utf-8'); filesModified.push(abs); } else { mkdirSync(dirname(abs), { recursive: true }); writeFileSync(abs, file.content, 'utf-8'); filesCreated.push(abs); } } const nextSteps: string[] = []; if (skipped.length > 0) { nextSteps.push(`Skipped ${skipped.length} customised file(s): ${skipped.join(', ')}. Remove the @customised marker to allow overwrites.`); } if (filesCreated.length === 0 && filesModified.length === 0 && skipped.length === 0) { nextSteps.push('Theme already up-to-date — no changes needed.'); } if (filesCreated.length > 0) { nextSteps.push(`Verify src/main.tsx imports './index.css' (Vite entry).`); } printEnvelope(generateEnvelope(COMMAND, { data: { fileCount: filesCreated.length + filesModified.length, skipped: skipped.length }, filesCreated, filesModified, warnings: validation.warnings, nextSteps, })); } main();