#!/usr/bin/env node /** * cli:scaffold-api-client — Generate TypeScript API clients + React Query hooks */ import { parseArgs } from 'node:util' import { mkdirSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { validate } from './validate.js' import { generate, legacyPaths } 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' import { guardedRm } from '../../../../../lib/guarded-rm.js' const COMMAND = 'scaffold-api-client' function main(): void { const { values } = parseArgs({ options: { spec: { type: 'string' }, 'spec-file': { type: 'string' }, outdir: { 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'])); process.exit(1) } const validation = validate(raw) if (!validation.valid) { printEnvelope(failGenerate(COMMAND, validation.errors)); process.exit(1) } const spec = raw as any const files = generate(spec) const outdir = values.outdir ?? spec.projectPath // Idempotent relocate: pre-classification feature files (no segment) // are swept so re-running MOVES the client into src/features//…. The // sweep is guarded (lib/guarded-rm): a legacy path identical to an emitted // path is never listed, an @customised file is preserved with a warning, and // every deletion is traced in the envelope. const emittedPaths = new Set(files.map((f: { path: string }) => f.path)) const legacySweep = legacyPaths(spec).filter((lp: string) => !emittedPaths.has(lp)) if (values.dry_run) { let wouldRemoveLegacy: string[] = [] try { wouldRemoveLegacy = guardedRm(legacySweep, { outdir, dryRun: true }).removed } catch { /* outdir invalid — the real run fails on assertWebProjectRoot */ } printEnvelope(generateEnvelope(COMMAND, { data: { dryRun: true, files: files.map((f: { path: string }) => f.path), wouldRemoveLegacy } })) process.exit(0) } try { assertWebProjectRoot(outdir) } catch (e) { printEnvelope(failGenerate(COMMAND, [e instanceof Error ? e.message : String(e)])); process.exit(1) } const sweep = guardedRm(legacySweep, { outdir }) const written: string[] = [] for (const file of files) { const p = resolve(safeJoinPath(outdir, file.path)); mkdirSync(dirname(p), { recursive: true }); writeFileSync(p, file.content, 'utf-8'); written.push(p) } printEnvelope(generateEnvelope(COMMAND, { data: { module: spec.module, fileCount: written.length, filesRemoved: sweep.removed }, filesCreated: written, warnings: sweep.preserved.map((p) => `legacy path ${p} kept: marked @customised — delete manually if truly superseded.`), nextSteps: ['Run npm run build to verify'], })) } main()