Skip to content

Scaffolding

For more than a couple of files, keep a folder of template files inside your schematic package and mirror it with scaffold instead of writing one create() per file. Most multi-file generators are built on scaffold.

schematics/service/
├── schema.json
├── schema.generated.ts
├── factory.ts
└── files/ # your template folder
├── __name@dasherize__.service.ts.template
├── __name@dasherize__.spec.ts.template
└── config/
└── settings.json
schematics/service/factory.ts
import { scaffold } from "@pbuilder/sdk/commons";
import type { Input } from "./schema.generated.ts";
export default (input: Input) => {
scaffold({
from: "files",
to: "src/services/__name@dasherize__",
options: { name: input.name },
});
};

With name: "userProfile" this emits src/services/user-profile/user-profile.service.ts, …/user-profile.spec.ts, and …/config/settings.json, with contents rendered against the same options, using the template language.

from is package-local: it resolves relative to your schematic’s folder, like copyIn and create({ templateFile }). The CLI passes the package location automatically; in tests you pass packageDir yourself (typically import.meta.dir).

File and folder names carry dynamic values through filename tokens:

  • __name__ becomes {= .name =}, the name option rendered into the path.
  • __name@pipe__ becomes {= .name | pipe =}. The same 7 pipes as templates apply, so __name@dasherize__.service.ts with name: "userProfile" lands as user-profile.service.ts.
  • The to destination is translated the same way. That’s how one scaffold call fans out into a per-option directory (to: "src/services/__name@dasherize__" above).

scaffold only rewrites the marker syntax; the rendering itself happens in the engine, exactly as for create templates.

Three more controls shape each entry’s destination, applied in a pinned order: rename first, token translation second, .template strip last.

A trailing .template is stripped from the destination name. Use it to keep template files from looking like real source to your editor and tooling: user.service.ts.template → user.service.ts.

rename is a static remap table matched against the original source-relative path. Use it for the odd file whose destination doesn’t follow the pattern. Because it runs first in the pinned order, the remapped name still goes through token translation and .template stripping afterwards.

include and exclude are glob filters over the original paths: * matches within a path segment, ** matches across segments, and exclude wins when both match a file.

A file travels verbatim (as copyIn) only when it matches one of the binary globs. They are matched like include and exclude, against the original path inside from. A binary glob that matches nothing is allowed.

scaffold({ from: "files", to: "src", binary: ["**/*.png"] });

Every other file renders as a template. A file that cannot render rejects with invalid-input, naming the file:

  • the file is not valid UTF-8 text;
  • the rendered create, options included, does not fit one emit frame;
  • the file is both marked .template and declared binary.

A text file over the frame limit must be made smaller, or declared binary if it needs no rendering.

Like every verb that writes to a new path, scaffold is fail-closed on collision: if a destination file already exists, the run rejects and nothing is written. Pass force: true inside the args object for a deliberate overwrite.

  • Nested symlinked directories inside from are skipped silently; a symlinked from root rejects outright.
  • A single scaffold call caps at 10,000 entries.
  • The to prefix and every rename value must pass the portable file-name check, as source paths under from already do. Names such as con.ts, out/AUX or src/a|b.ts reject with invalid-input. . and empty components are skipped, so to: "." and to: "./out" still work.

Preview what a scaffold call is about to emit with pendingDirectives(), and see mutation verbs for the full edge-and-error semantics of scaffold and its siblings.