Build a command-line tool that scaffolds React TypeScript component files from a name, typed props and templates, with input validation, no-overwrite safety and tests.
Build a command-line tool that generates React component files in TypeScript for this repository. The tool scaffolds files; it does not design the component's behaviour. Writing a single finished component is a separate task.
## Inputs
Repository conventions (folder layout, file naming, named or default exports, styling, test and story files): [CONVENTIONS]
Runtime for the CLI (Node.js version, tsx or compiled TypeScript) and argument parser if one is already used: [CLI_RUNTIME]
Component types to support (function components; class components only if the codebase still uses them): [COMPONENT_TYPES]
Templates to start from, if any: [TEMPLATES]
## Parameters the CLI accepts
- `--name`: component name
- `--type`: `function` (default) or `class`
- `--props`: list such as `name:string,age?:number,onSelect:(id: string) => void`
- `--state`: include local state scaffolding
- `--lifecycle`: lifecycle methods to include (class components only)
- `--template`: path to a custom template
- `--dir`, `--with-test`, `--with-story`, `--force`
## Instructions
1. Validate inputs before writing anything: PascalCase component name, prop names that are valid identifiers, prop types that parse as TypeScript types, lifecycle methods only with `--type class`, and an existing template path.
2. Generate `<Name>.tsx` with a typed props interface and the requested structure; add test and story files when requested.
3. If a custom template is given, document its placeholders (for example `{{name}}`, `{{propsInterface}}`) and fail with a clear message on unknown placeholders.
4. Never overwrite existing files unless `--force` is passed; print the files that would be written with a `--dry-run` option.
5. Format output with the repository's formatter if available.
6. On any validation error, print the reason, write no files, and exit with a non-zero status.
## Return
1. CLI source code.
2. Default templates.
3. Example commands and the files each generates.
4. Tests for the CLI: valid generation, invalid name, invalid prop type, lifecycle without class, existing file without `--force`, custom template.
5. How to add the CLI to `package.json` scripts.
## Acceptance checks
- Invalid input never produces a partial set of files.
- Generated files compile with the repository's TypeScript configuration.
- Existing files are protected by default.
- Class component support is marked as legacy if included.Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Conventions: src/components/<Name>/<Name>.tsx, named exports, CSS Modules (<Name>.module.css), Vitest tests (<Name>.test.tsx), Storybook 8 stories (<Name>.stories.tsx) CLI runtime: Node 22 with tsx; no argument parser installed yet (node:util parseArgs is acceptable) Component types: function only; the codebase has no class components Example command to support: npm run new:component -- --name UserCard --props "name:string,avatarUrl?:string,onSelect:(id: string) => void" --with-test --with-story
A tsx script using node:util parseArgs that rejects "userCard" (not PascalCase) and a prop type that fails to parse, refuses --type class because only function components are supported in this codebase, writes UserCard.tsx with an exported UserCardProps interface containing name: string, avatarUrl?: string and onSelect: (id: string) => void, plus UserCard.module.css, UserCard.test.tsx and UserCard.stories.tsx, skips existing files without --force, supports --dry-run, and exits non-zero on errors. Illustrative expectation, not a recorded model run.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: