Code Scaffolding
Scaffolding is not about saving keystrokes — it is about encoding team conventions into every new file. The most dangerous boilerplate is the one that does not match the codebase. This skill detects existing patterns before generating anything.
Every generated file must be indistinguishable from one written by a long-tenured team member. If the codebase uses kebab-case filenames, you use kebab-case. If tests live next to source files, your tests go there too. No exceptions.
When to Activate
- Creating new components, services, routes, or modules
- Setting up new projects or sub-packages within a monorepo
- Adding CRUD endpoints or API routes
- Creating test files alongside new code
- Bootstrapping a new feature with multiple coordinated files
- Generating database models, migrations, or seed files
- Adding CLI commands, background workers, or middleware
Core Workflow: Convention-First Scaffolding
Step 1: Detect Conventions
Before writing a single line, analyze 3-5 existing examples of the same file type in the codebase. Use Glob and Read to find them. Extract:
- File naming:
UserProfile.tsx vs user-profile.tsx vs userProfile.tsx
- Directory placement: Where do files of this type live? Nested by feature or flat by type?
- Import patterns: Absolute paths (
@/components/...) vs relative? Import grouping and ordering?
- Export style: Named exports vs default? Barrel/index re-exports?
- Error handling: Try/catch patterns, error boundaries, Result types?
- Test co-location:
__tests__/ subdirectory, .test.ts alongside, or separate tests/ tree?
- Boilerplate patterns: Common setup code, decorators, annotations, wrapper components?
If fewer than 3 examples exist, widen the search. If the codebase is new or empty, ask the user which conventions to follow before generating.
Step 2: Confirm with User
Present the discovered conventions in a brief summary:
Detected conventions for React components:
- Directory: src/components/{ComponentName}/
- Files: index.tsx (barrel), ComponentName.tsx, ComponentName.test.tsx, ComponentName.module.css
- Exports: Named exports, re-exported from index
- Styling: CSS Modules
- Tests: Co-located with component
Proceed with these conventions, or override?
Never assume. If conventions are ambiguous or inconsistent across the codebase, surface the inconsistency and let the user decide.
Step 3: Generate
Create files that match the detected conventions exactly. Every generated file must include:
- Correct imports matching codebase style
- Proper TypeScript types/interfaces (if the project uses TS)
- Error handling consistent with codebase patterns
TODO comments explaining what business logic to implement — never placeholder "hello world" code
- Consistent formatting (the project's Prettier/ESLint config will handle this, but match indentation and style)
Step 4: Companion Files
After generating the primary file, check if companion files are needed:
- Test stub: Matching the project's test framework and patterns
- Type exports: If the project centralizes types (
types/, *.types.ts)
- Index/barrel updates: If the directory uses barrel exports, update
index.ts
- Storybook file: If the project uses Storybook, generate a
.stories.tsx
- Style file: CSS Module, styled-components file, or Tailwind class extraction
- Documentation: If the project co-locates docs (e.g.,
.mdx alongside components)
Only generate companion files for patterns that already exist in the codebase. Never introduce a new companion file pattern.
Scaffolding Categories
| Type |
What to Detect |
What to Generate |
| Frontend component (page, layout, widget, form) |
Component structure, styling approach, prop patterns, state management |
Component file, test, styles, stories, index re-export |
| API endpoint / route handler |
Route organization, middleware chain, validation, response format |
Route handler, validation schema, test, types |
| Database model / migration |
ORM patterns, naming conventions, field types, relationships |
Model definition, migration file, seed data stub, types |
| Service / repository class |
Interface patterns, dependency injection, error handling |
Service class, interface, test, types |
| CLI command |
Command framework (Commander, yargs, oclif), option patterns |
Command file, test, help text |
| Background job / worker |
Queue framework, retry patterns, logging |
Job handler, test, queue registration |
| Test suite |
Testing framework, assertion style, mock patterns, fixtures |
Test file with describe/it blocks matching project style |
See references/scaffolding-decision-matrix.md for the detailed mapping of user intent to scaffold output.
Convention Detection Checklist
Run through this checklist before generating any scaffold:
Naming and Structure
Code Patterns
Import Conventions
Framework-Specific
See references/template-conventions.md for framework-specific detection guides.
Gotchas
Never Introduce New Patterns
Scaffold must not introduce conventions that do not already exist in the codebase. If the project uses CSS Modules, do not generate a styled-components file. Consistency over novelty, always.
Respect Monorepo Boundaries
In monorepos, each package may follow different conventions. packages/ui may use PascalCase React components while packages/api uses kebab-case Express handlers. Detect conventions within the target package, not across the repo.
Update Barrel/Index Files
Scaffolding a new component often requires updating an index.ts barrel file to include the new export. Search for barrel files in the target directory and its parent. Missing this step breaks the import chain.
Flag New Dependencies
If the scaffold would introduce an import from a library not already in package.json (or equivalent), flag it to the user before generating. Never silently add new dependencies.
Use TODO Comments, Not Placeholder Logic
Never generate fake business logic the user must delete. Instead:
// TODO: Implement user validation logic
// Expected: validate email format, check uniqueness against database
// Returns: validated user object or throws ValidationError
Check for Existing Code Generators
Before scaffolding manually, check if the project already has code generation tooling:
plopfile.js or plopfile.ts (Plop)
.hygen/ directory (Hygen)
nx.json with generators (Nx)
turbo/generators/ (Turborepo)
- Custom scripts in
package.json (generate, scaffold, new)
If generators exist, inform the user and ask whether to use them or generate directly.
Test File Conventions
When generating test stubs, match the project's exact testing patterns:
describe/it vs test blocks
- Import style for test utilities (
@testing-library/react, enzyme, vitest)
- Mock patterns (
jest.mock, vi.mock, manual mocks in __mocks__/)
- Setup/teardown patterns (
beforeEach, afterEach, fixtures)
1---2name: code-scaffolding3description: Generate project boilerplate, component templates, and framework scaffolding that follows existing codebase conventions. Use when the user says 'scaffold', 'generate boilerplate', 'create a new component', 'set up a new service', 'bootstrap', 'new module', 'add a new route', 'create a new endpoint', 'template for', 'stub out', or 'create a new page'. Also triggers on 'scaffolding', 'boilerplate', 'code generation', 'new feature skeleton', 'project setup', 'starter template', or 'generate files for'.4---5
6# Code Scaffolding
7
8Scaffolding is not about saving keystrokes — it is about encoding team conventions into every new file. The most dangerous boilerplate is the one that does not match the codebase. This skill detects existing patterns before generating anything.
9
10Every generated file must be indistinguishable from one written by a long-tenured team member. If the codebase uses `kebab-case` filenames, you use `kebab-case`. If tests live next to source files, your tests go there too. No exceptions.
11
12## When to Activate
13
14- Creating new components, services, routes, or modules
15- Setting up new projects or sub-packages within a monorepo
16- Adding CRUD endpoints or API routes
17- Creating test files alongside new code
18- Bootstrapping a new feature with multiple coordinated files
19- Generating database models, migrations, or seed files
20- Adding CLI commands, background workers, or middleware
21
22## Core Workflow: Convention-First Scaffolding
23
24### Step 1: Detect Conventions
25
26Before writing a single line, analyze **3-5 existing examples** of the same file type in the codebase. Use Glob and Read to find them. Extract:
27
28- **File naming**: `UserProfile.tsx` vs `user-profile.tsx` vs `userProfile.tsx`
29- **Directory placement**: Where do files of this type live? Nested by feature or flat by type?
30- **Import patterns**: Absolute paths (`@/components/...`) vs relative? Import grouping and ordering?
31- **Export style**: Named exports vs default? Barrel/index re-exports?
32- **Error handling**: Try/catch patterns, error boundaries, Result types?
33- **Test co-location**: `__tests__/` subdirectory, `.test.ts` alongside, or separate `tests/` tree?
34- **Boilerplate patterns**: Common setup code, decorators, annotations, wrapper components?
35
36If fewer than 3 examples exist, widen the search. If the codebase is new or empty, ask the user which conventions to follow before generating.
37
38### Step 2: Confirm with User
39
40Present the discovered conventions in a brief summary:
41
42```
43Detected conventions for React components:
44- Directory: src/components/{ComponentName}/
45- Files: index.tsx (barrel), ComponentName.tsx, ComponentName.test.tsx, ComponentName.module.css
46- Exports: Named exports, re-exported from index
47- Styling: CSS Modules
48- Tests: Co-located with component
49
50Proceed with these conventions, or override?
51```
52
53**Never assume.** If conventions are ambiguous or inconsistent across the codebase, surface the inconsistency and let the user decide.
54
55### Step 3: Generate
56
57Create files that match the detected conventions exactly. Every generated file must include:
58
59- Correct imports matching codebase style
60- Proper TypeScript types/interfaces (if the project uses TS)
61- Error handling consistent with codebase patterns
62- `TODO` comments explaining what business logic to implement — never placeholder "hello world" code
63- Consistent formatting (the project's Prettier/ESLint config will handle this, but match indentation and style)
64
65### Step 4: Companion Files
66
67After generating the primary file, check if companion files are needed:
68
69- **Test stub**: Matching the project's test framework and patterns
70- **Type exports**: If the project centralizes types (`types/`, `*.types.ts`)
71- **Index/barrel updates**: If the directory uses barrel exports, update `index.ts`
72- **Storybook file**: If the project uses Storybook, generate a `.stories.tsx`
73- **Style file**: CSS Module, styled-components file, or Tailwind class extraction
74- **Documentation**: If the project co-locates docs (e.g., `.mdx` alongside components)
75
76Only generate companion files for patterns that **already exist** in the codebase. Never introduce a new companion file pattern.
77
78## Scaffolding Categories
79
80| Type | What to Detect | What to Generate |
81|---|---|---|
82| **Frontend component** (page, layout, widget, form) | Component structure, styling approach, prop patterns, state management | Component file, test, styles, stories, index re-export |
83| **API endpoint / route handler** | Route organization, middleware chain, validation, response format | Route handler, validation schema, test, types |
84| **Database model / migration** | ORM patterns, naming conventions, field types, relationships | Model definition, migration file, seed data stub, types |
85| **Service / repository class** | Interface patterns, dependency injection, error handling | Service class, interface, test, types |
86| **CLI command** | Command framework (Commander, yargs, oclif), option patterns | Command file, test, help text |
87| **Background job / worker** | Queue framework, retry patterns, logging | Job handler, test, queue registration |
88| **Test suite** | Testing framework, assertion style, mock patterns, fixtures | Test file with describe/it blocks matching project style |
89
90See `references/scaffolding-decision-matrix.md` for the detailed mapping of user intent to scaffold output.
91
92## Convention Detection Checklist
93
94Run through this checklist before generating any scaffold:
95
96### Naming and Structure
97- [ ] File naming convention: PascalCase vs kebab-case vs camelCase
98- [ ] Directory structure: flat vs nested, feature-based vs type-based
99- [ ] Co-located tests vs separate test directory
100- [ ] Barrel/index export files present?
101
102### Code Patterns
103- [ ] Export style: named exports vs default exports
104- [ ] Component style: functional vs class-based, hooks vs HOCs
105- [ ] State management: local state, Context, Zustand/Redux/MobX
106- [ ] Error handling: try/catch, error boundaries, Result/Either types
107- [ ] Validation: Zod, Yup, Joi, class-validator, or manual
108
109### Import Conventions
110- [ ] Path aliases configured? (`@/`, `~/`, `#/`)
111- [ ] Import grouping: external, internal, relative — in what order?
112- [ ] Type-only imports: `import type { ... }` used?
113
114### Framework-Specific
115- [ ] Routing pattern: file-based, config-based, decorator-based
116- [ ] Middleware chain: how middleware is registered and ordered
117- [ ] Dependency injection: constructor injection, module injection, none
118
119See `references/template-conventions.md` for framework-specific detection guides.
120
121## Gotchas
122
123### Never Introduce New Patterns
124Scaffold must not introduce conventions that do not already exist in the codebase. If the project uses CSS Modules, do not generate a styled-components file. Consistency over novelty, always.
125
126### Respect Monorepo Boundaries
127In monorepos, each package may follow different conventions. `packages/ui` may use PascalCase React components while `packages/api` uses kebab-case Express handlers. Detect conventions **within the target package**, not across the repo.
128
129### Update Barrel/Index Files
130Scaffolding a new component often requires updating an `index.ts` barrel file to include the new export. Search for barrel files in the target directory and its parent. Missing this step breaks the import chain.
131
132### Flag New Dependencies
133If the scaffold would introduce an import from a library not already in `package.json` (or equivalent), flag it to the user before generating. Never silently add new dependencies.
134
135### Use TODO Comments, Not Placeholder Logic
136Never generate fake business logic the user must delete. Instead:
137
138```typescript
139// TODO: Implement user validation logic
140// Expected: validate email format, check uniqueness against database
141// Returns: validated user object or throws ValidationError
142```
143
144### Check for Existing Code Generators
145Before scaffolding manually, check if the project already has code generation tooling:
146- `plopfile.js` or `plopfile.ts` (Plop)
147- `.hygen/` directory (Hygen)
148- `nx.json` with generators (Nx)
149- `turbo/generators/` (Turborepo)
150- Custom scripts in `package.json` (`generate`, `scaffold`, `new`)
151
152If generators exist, inform the user and ask whether to use them or generate directly.
153
154### Test File Conventions
155When generating test stubs, match the project's exact testing patterns:
156- `describe`/`it` vs `test` blocks
157- Import style for test utilities (`@testing-library/react`, `enzyme`, `vitest`)
158- Mock patterns (`jest.mock`, `vi.mock`, manual mocks in `__mocks__/`)
159- Setup/teardown patterns (`beforeEach`, `afterEach`, fixtures)