CLI Scaffold Generator
Generate a project scaffold from templates with variable substitution, conditional files, and a post-install hook so the user gets a runnable project.
On this page
Introduction
This guide covers CLI project scaffold generator, a recurring concern in the cli-tooling scenario. A scaffold that copies a folder and leaves the user to rename things is barely a scaffold. Templating with variable substitution, files that appear only for chosen options, and a post-install hook that runs install and init gives the user a project that builds on first run.
Template tree
Lay out the target structure with placeholders for names and values, so a single source generates every variant the user can pick.
The trade-off worth naming is that lay out the target structure with placeholders for names and values, so a single source generates every variant the user can pick adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
template/
package.json ({{name}}, {{license}})
src/index.{{lang}}
.gitignore
Substitute variables
Replace placeholders with the prompted values, so the generated code has the real name and license, not tokens that break the first build.
The trade-off worth naming is that replace placeholders with the prompted values, so the generated code has the real name and license, not tokens that break the first build adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import { render } from 'ejs';
const out = render(content, { name: opts.name, license: opts.license });
await writeFile(target, out);
Conditional files
Include a file only when the user chose its option, so a TypeScript scaffold includes tsconfig and a JavaScript one does not, which keeps the output clean.
The trade-off worth naming is that include a file only when the user chose its option, so a typescript scaffold includes tsconfig and a javascript one does not, which keeps the output clean adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Post-install hook
Run install and init in the generated directory so the project builds on first run, which is the difference between a scaffold and a copy.
The trade-off worth naming is that run install and init in the generated directory so the project builds on first run, which is the difference between a scaffold and a copy adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Idempotent regen
Make regeneration skip files the user has modified, so updating the scaffold does not clobber their work, which is the safe default.
The trade-off worth naming is that make regeneration skip files the user has modified, so updating the scaffold does not clobber their work, which is the safe default adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the output
Generate a project, run its build, and assert it succeeds, so the scaffold produces a runnable project rather than a folder of templates.
The trade-off worth naming is that generate a project, run its build, and assert it succeeds, so the scaffold produces a runnable project rather than a folder of templates adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
FAQ
- Copy or template?
- Template, so names and values are substituted from the user choices; a plain copy leaves placeholders in the code that surface as syntax errors on first build.
- Should the scaffold run install?
- Yes, in a post-install hook, so the user gets a runnable project rather than a folder they still have to wire up, which is the point of scaffolding.
Report a problem with this articleOpens a pre-filled issue on GitHub.