How the open-source repository turns one codebase into thousands of downloadable variants. This page is for contributors and for anyone running their own copy of the download platform. None of this code ships in a download.
The pipeline
apps/ + packages/ ──template:sync──► templates/saas-boilerplate ──template:stage──► .generated/saas-boilerplate
(source of truth) (managed starter source) (clean staged copy)
│
compileScaffoldVariant(modules, platforms, providers)
▼
base archive (cached in R2)
│
+ SETUP.md, .boilerplate-version, .env files
▼
the buyer's ZIP
Modules
scaffold-modules/registry.json lists the base price, the tier step price and every module (id, label, description, creditsCost, requires, incompatibleWith, downloadEnabled, implemented). Each module has scaffold-modules/<id>/manifest.json:
| Key | Meaning |
|---|---|
unselected.remove | Files and folders the module owns, deleted when it is not selected (including its migration folders) |
unselected.jsonRemove | JSON keys to delete, such as a package script or dependency |
unselected.textReplace | One-line edits markers cannot express; each must match exactly once |
unselected.copy / replace | Module-owned slot stubs, such as WorkspaceSlot.tsx. Never snapshot a shared file |
ownedIdentifiers | Strings that must not survive when the module is unselected; the leak check fails on them |
selected | Actions applied only when the module is selected |
Code inside shared files goes between markers in the file's comment syntax:
// scaffold:begin billing
import { billingRouter } from "./billingProcedures";
// scaffold:end billing
{/* scaffold:begin ai */}
<AiMenuItem />
{/* scaffold:end ai */}
# scaffold:begin ai_agents
BACKEND_URL=http://localhost:8000
# scaffold:end ai_agents
Regions can nest. Regions of unselected modules are dropped and marker lines never ship. Model access through (db as any) escapes the type checker, so it needs a marker region too.
Providers
scaffold-providers/registry.json lists each toggle (payment_gateway, image_storage, cms), its env var, its owning module and its values. scaffold-providers/<toggle>/<value>/manifest.json has unselected actions, ownedIdentifiers and selected actions (for example pinning the default to the chosen value). Markers use <toggle>.<value>:
// scaffold:begin payment_gateway.stripe
if (gateway === "stripe") {
// …
}
// scaffold:end payment_gateway.stripe
throw gatewayNotConfigured();
Each provider branch must be self-contained: an if block per provider, then a clear error, never an else another provider fills. A toggle prunes only inside its module, and choices are part of the build key.
Migrations per variant
A module owns the migration folders that only touch its tables. Statements a module adds to a shared migration are stripped with textReplace in the variant; repository migration files are never edited. The boot smoke test proves the remaining migrations match the variant's schema.
Lockfile
pnpm-lock.yaml importers are pruned to the variant's package.json files at compile time, so pnpm install --frozen-lockfile works for buyers on CI and Vercel.
Downloads, charges and the build cache
Every download goes through downloadScaffold in apps/web/lib/scaffold/service.ts:
- Charge: free when the user already owns the same build key; otherwise the price is charged once per idempotency key and a
ScaffoldJobis recorded. - Build: the base archive is served from the build cache (
apps/web/lib/scaffold/build-cache.ts, stored in R2 when its credentials are set), keyed by the starter's content, modules, platforms, providers andBUILDER_VERSION. - Finish: the job becomes
ready, orfailedand refunded exactly once.
Bump BUILDER_VERSION whenever compile or archive output changes; build-cache.test.ts fails until you do. After a deploy, an admin can warm the cache with the scaffold.prewarm mutation.
The wizard's file preview (scaffold.previewIndex) compiles real variants, so it follows the manifests automatically.
Secrets
packages/ui/src/lib/scaffold-secrets.ts decides which env vars are secrets, for the browser and the server alike. /api/scaffold answers 400 secret_not_accepted if a request contains one; the browser adds secrets to the ZIP with zip-append.ts.
HTTP API
| Route | Auth | Purpose |
|---|---|---|
POST /api/scaffold | Session | Wizard download. Body: name, modules, public envVars, expectedTotalCredits. 409 price_changed when the price moved |
POST /api/scaffold/upgrade | Session | Upgrade kit for a saved project |
POST /api/scaffold/builds/[jobId] | Session | Free re-download of an owned build |
/api/scaffold/trpc | Per procedure | tRPC procedures that read the starter (preview, build keys, prewarm, upgrade previews) |
GET /api/v1/scaffold/pricing | API key | Catalog and prices |
GET /api/v1/credits | API key (read:credits) | Credit balance |
GET, POST /api/v1/projects | API key | List or create saved projects |
GET, PATCH /api/v1/projects/[slug] | API key | Read or update a project |
POST /api/v1/projects/[slug]/download | API key (scaffold:download) | Download a project |
GET, POST /api/v1/projects/[slug]/upgrade | API key (scaffold:upgrade) | Quote or buy an upgrade kit |
Vercel file tracing
Vercel ships each route with only its traced files, but the scaffold code reads the registry and the starter with fs. outputFileTracingIncludes in apps/web/next.config.mjs adds the registry to every route and the starter only to /api/scaffold/* and the v1 download and upgrade routes. traceConfig.test.ts checks it.
Validating changes
pnpm scaffold:matrix # every module alone, every pair, all; install, generate, typecheck, arch-check
pnpm scaffold:matrix --static # compile and leak checks only, fast
pnpm scaffold:matrix --only ai+billing
pnpm scaffold:matrix --full # every subset (nightly)
CI runs the pairwise matrix on every pull request. .github/workflows/scaffold-nightly.yml runs the full sweep plus scripts/scaffold-boot-smoke.mjs (migrate, seed, production build, start, sign up) for the none and all variants. A module marked implemented: false must keep an empty manifest.
Adding a module
- Add it to
scaffold-modules/registry.jsonwith a price andimplemented: true. - Create its manifest: owned files in
unselected.remove, identifiers inownedIdentifiers. - Wrap its code in shared files with
scaffold:begin <id>/scaffold:end <id>. - Give it its own
.prismafile and migrations. - Run
pnpm scaffold:matrix, then document it here and on the Modules pages.
