The @codefast/cli package is a small Node CLI. Its commands are named namespace:action, as Laravel's Artisan
names them — audit and audit:<name>, exports:generate, publish:prepare, since:stamp — and every one of them
runs through one command model. Behaviour is wired with plain functions, Commander parses argv, and
Result<T, AppError> carries recoverable failures.
One command model
A command is a CommandSpec: its name and description, the help for its one [target], the
boolean flags it takes, a run, a verdict over its report and the flags it ran with, and a present for a human.
Everything else is written once, in core/command.ts, and no command repeats any of it:
- The workspace.
loadWorkspaceresolves the project root from the working directory, loads the root's onecodefast.config.*, and reads every package manifest once. A command never resolves a root, loads a config or parses apackage.jsonof its own. - The target.
resolveTargetturns the[target]argument into a path inside the root: the given path against the working directory, else the target the config names for the command, else the root. A command that acts on packages reaches them throughpackagesInTarget; one that reads files walks the target. - The run.
run(context)receives the workspace, the target and the flags, and returns a report or anAppError. - The afterWrite hook. When the run wrote files and
writtenFileslists them, the config'safterWritehook receives them; a hook that throws fails the run. - The output.
--jsonprints one document on stdout, shaped the same for every command —{ schemaVersion, command, ok, rootDir, target, elapsedSeconds, result }, or{ …, ok: false, error }when the run could not happen. Without it,presentprints the report and ends with the one summary line every command ends with. - The exit code. The
verdictispass,failorerror, and maps to0,1or2; everyAppErroris2, and so is a throw no run anticipated, reported as anUNEXPECTEDerror rather than left to exit Node's1.
The vocabulary a report is written in is shared too, in core/report.ts: a Finding is something
at a file:line a person must look at, and a PackageOutcome is what a writing command did to one package — changed,
unchanged, skipped or failed. Their presenters draw the same marks in every command.
One finding model for the audits
An audit is an Audit: a name, its config section and a scan. The scan is the audit's
own — which files or packages it reads and what it detects there — and it returns an AuditScan: the findings, what it
scanned, and the files --fix rewrote. Everything after the scan is written once in
audit/command.ts: the allowlist from the audit's config section
(applyAllowlist), the audit:<name> command, the verdict (any finding fails), the
presenter (audit/output.ts), and the bare audit that runs them all. A scan that throws fails
only its own audit, as an UNEXPECTED error, so the bare audit still reports every other one, as a writing command
still reports every other package. A detector states what it found as a Detection — line, raw text, reason — and
the run places it in its file.
Layout (src/)
| Area | Role |
|---|---|
bin.ts |
Process entry: runs runCli and sets process.exitCode, never process.exit, which cuts a piped stdout short. |
cli.ts |
Composition root: the program, --no-color, and one createCommand per spec; run bare, it lists the commands. |
core/ |
The command model, the workspace and target, the report vocabulary, Result and AppError, the filesystem port, logger and paint, and the AST helpers. |
core/config/ |
The strict Zod schema, the loader, defineConfig, and the selector that reads a per-package record by name or glob. |
audit/ |
audit and every audit:<name>: one directory per audit holding its Audit (command.ts), its scan (run.ts) and its pure domain/ detectors; the allowlist, the presenters and the full run are shared. |
exports/ |
exports:generate: one package's sync in sync-package.ts, the writers, and the export-map generation in domain/exports.ts. |
publish/ |
publish:prepare: the working-tree guard and the pure manifest slim in domain/transform.ts, which audit:publish applies too. |
since/ |
since:stamp: the run over each package's source and the @since writer under writer/. |
A command directory holds command.ts (its spec), run.ts (the orchestrator), output.ts (its presenter) and
domain/ (pure logic and its report types); a file that fits none of those is named for the one concept it holds.
Boundaries
- Domain modules (
*/domain/**) stay pure: no Commander, noprocess, nologger— only types and algorithms. - Runs take a
CommandContextand return aResult; they never print, and never touchprocess.exitCode. - Presenters print through
loggerand returnvoid; the--jsondocument and the exit code belong to the pipeline, which reads both off the report. - Zod validates what crosses in from outside — the config file — never a value the CLI builds itself.
Imports
Internal code uses the #… alias (see package.json#imports): #core/…, #audit/…, never a deep relative path.
Testing
Tests live under tests/ only, in the workspace's four categories. A unit test drives a run over
createMemoryFilesystem through the same loadWorkspace and resolveTarget
the pipeline uses (createCommandContext); what needs a real disk — a JS
config, the type server, the built binary — is an integration or e2e test over a temporary directory.
Further reading
- Product behaviour and commands:
README.md. - Design decisions and their reasons:
DECISIONS.md.
License
Released under the MIT License.