daniel-code-generator
v1.2.4
Published
Generate code files.
Maintainers
Readme
daniel-code-generator
A tiny, focused CLI that generates TypeScript class boilerplate for you.
Given a class name, it will:
- Create a
.tsfile next to the compiled CLI - Define a class with that name
- Export a singleton instance of that class
This is handy when you’re rapidly sketching new classes and don’t want to keep writing the same scaffolding over and over.
Installation
Add it to your project as a dev dependency:
npm install daniel-code-generator --save-devOr with pnpm:
pnpm add daniel-code-generator -DBuild the TypeScript once so the CLI is available:
npm run buildThis compiles to the build directory and wires the generate binary (see bin.generate in package.json).
Usage
From your project root, run:
npx generate MyServiceor, if installed locally and your node_modules/.bin is on PATH:
generate MyServiceThis will create a new file MyService.ts next to the built CLI (inside the compiled build directory), containing:
class MyService {
}
export const myService = new MyService();Where:
classNameis the argument you pass (e.g.MyService)- The exported constant name is the same but with a lower‑cased first letter (e.g.
myService)
How it works (internals)
- Entry point:
src/app.ts- Reads the class name from
process.argv[2] - Calls
generateCode(className)
- Reads the class name from
- Core logic:
src/generator.ts- Builds a small TypeScript snippet with a class and exported singleton
- Writes it synchronously using
fs.writeFileSync - Resolves the output path with Node’s
path.join(__dirname, className + ".ts")
You don’t need to interact with these files directly to use the CLI, but they’re simple enough to tweak if you want to change the generated template.
Examples
Generate a repository class
npx generate UserRepositoryProduces
UserRepository.ts:class UserRepository { } export const userRepository = new UserRepository();Generate a controller
npx generate AuthControllerProduces
AuthController.tswithexport const authController = new AuthController();
Notes & limitations
- Single responsibility: the generator currently only creates:
- an empty class body, and
- a default exported instance.
- Output directory: files are created relative to the compiled CLI (
buildfolder afternpm run build), not your projectsrcfolder. - TypeScript only: the generated files are
.tsfiles.
If you want to evolve this into a more powerful scaffolding tool (e.g. templates, custom output paths, multiple files), this project is a good minimal starting point.
License
ISC © Daniel Machluf
