|
| 1 | +# @tbk/cli |
| 2 | + |
| 3 | +CLI tool for TypeScript Backend Toolkit projects. Generate modules, plugins, middleware, seeders, and factories with a single command. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +This package is typically installed as a dev dependency in TypeScript Backend Toolkit projects: |
| 8 | + |
| 9 | +```bash |
| 10 | +pnpm add -D @tbk/cli |
| 11 | +``` |
| 12 | + |
| 13 | +Or via npm: |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install -D @tbk/cli |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +After installation, use the `tbk` command: |
| 22 | + |
| 23 | +```bash |
| 24 | +# Generate a complete module |
| 25 | +pnpm tbk generate:module <name> [--path /api/v1] |
| 26 | + |
| 27 | +# Generate a plugin |
| 28 | +pnpm tbk generate:plugin <name> |
| 29 | + |
| 30 | +# Generate middleware |
| 31 | +pnpm tbk generate:middleware <name> |
| 32 | + |
| 33 | +# Run database seeders |
| 34 | +pnpm tbk seed [--group dev] [--only SeederName] [--fresh] |
| 35 | + |
| 36 | +# Create a seeder for a module |
| 37 | +pnpm tbk make:seeder <module>/<name> [--count 5] [--unique field] |
| 38 | + |
| 39 | +# Create a factory for a module |
| 40 | +pnpm tbk make:factory <module>/<name> [--model ExportName] |
| 41 | +``` |
| 42 | + |
| 43 | +## Commands |
| 44 | + |
| 45 | +### generate:module |
| 46 | + |
| 47 | +Generate a complete module with all files (dto, model, schema, services, controller, router). |
| 48 | + |
| 49 | +```bash |
| 50 | +pnpm tbk generate:module user |
| 51 | +pnpm tbk generate:module product --path /api/v1 |
| 52 | +``` |
| 53 | + |
| 54 | +**Options:** |
| 55 | +- `-p, --path <path>` - API path prefix (default: `/api`) |
| 56 | + |
| 57 | +**Creates:** |
| 58 | +- `src/modules/<name>/<name>.dto.ts` - Zod schemas and TypeScript types |
| 59 | +- `src/modules/<name>/<name>.model.ts` - Mongoose model |
| 60 | +- `src/modules/<name>/<name>.schema.ts` - Request/response validation schemas |
| 61 | +- `src/modules/<name>/<name>.services.ts` - Business logic and data access |
| 62 | +- `src/modules/<name>/<name>.controller.ts` - HTTP request handlers |
| 63 | +- `src/modules/<name>/<name>.router.ts` - MagicRouter route definitions |
| 64 | + |
| 65 | +### generate:plugin |
| 66 | + |
| 67 | +Generate a new plugin with the standard structure. |
| 68 | + |
| 69 | +```bash |
| 70 | +pnpm tbk generate:plugin cache |
| 71 | +``` |
| 72 | + |
| 73 | +**Creates:** |
| 74 | +- `src/plugins/<name>/index.ts` - Plugin factory and registration |
| 75 | + |
| 76 | +### generate:middleware |
| 77 | + |
| 78 | +Generate a new Express middleware. |
| 79 | + |
| 80 | +```bash |
| 81 | +pnpm tbk generate:middleware rateLimiter |
| 82 | +``` |
| 83 | + |
| 84 | +**Creates:** |
| 85 | +- `src/middlewares/<name>.ts` - Middleware function |
| 86 | + |
| 87 | +### seed |
| 88 | + |
| 89 | +Run database seeders to populate test data. |
| 90 | + |
| 91 | +```bash |
| 92 | +# Run all seeders in dev group |
| 93 | +pnpm tbk seed |
| 94 | + |
| 95 | +# Run specific seeders |
| 96 | +pnpm tbk seed --only UserSeeder,ProductSeeder |
| 97 | + |
| 98 | +# Fresh run (drops collections) |
| 99 | +pnpm tbk seed --fresh |
| 100 | + |
| 101 | +# Dry run (no writes) |
| 102 | +pnpm tbk seed --dry-run |
| 103 | + |
| 104 | +# Force run in production |
| 105 | +pnpm tbk seed --force |
| 106 | +``` |
| 107 | + |
| 108 | +**Options:** |
| 109 | +- `-g, --group <group>` - Group to run (base|dev|test|demo) (default: `dev`) |
| 110 | +- `--only <names>` - Comma-separated seeder names |
| 111 | +- `--fresh` - Drop involved collections before seeding |
| 112 | +- `--force` - Force run in production |
| 113 | +- `--dry-run` - Do not write, only log actions |
| 114 | +- `--seed <number>` - Random seed for data generation (default: `1`) |
| 115 | +- `--no-transaction` - Disable transactions |
| 116 | + |
| 117 | +### make:seeder |
| 118 | + |
| 119 | +Scaffold a new seeder for a module. Automatically detects model fields and dependencies. |
| 120 | + |
| 121 | +```bash |
| 122 | +pnpm tbk make:seeder user/User |
| 123 | +pnpm tbk make:seeder product/Product --count 10 --unique slug |
| 124 | +``` |
| 125 | + |
| 126 | +**Options:** |
| 127 | +- `-c, --count <number>` - Default count for dev/test (default: `5`) |
| 128 | +- `-u, --unique <field>` - Unique field to upsert by |
| 129 | +- `-d, --depends-on <names>` - Comma-separated additional dependencies |
| 130 | +- `--model <export>` - Model export name when not default |
| 131 | + |
| 132 | +**Creates:** |
| 133 | +- `src/modules/<module>/seeders/<Name>Seeder.ts` |
| 134 | + |
| 135 | +**Note:** The seeder must be manually registered in `src/seeders/registry.ts`. |
| 136 | + |
| 137 | +### make:factory |
| 138 | + |
| 139 | +Scaffold a new factory for a module. Automatically detects model fields and create functions. |
| 140 | + |
| 141 | +```bash |
| 142 | +pnpm tbk make:factory user/User |
| 143 | +pnpm tbk make:factory product/Product --model ProductModel --use service |
| 144 | +``` |
| 145 | + |
| 146 | +**Options:** |
| 147 | +- `--model <export>` - Model export name when not default |
| 148 | +- `--use <service|model>` - Prefer using service create function when present (default: `service`) |
| 149 | +- `--id-type <string|objectId>` - Hint for _id type when ambiguous |
| 150 | + |
| 151 | +**Creates:** |
| 152 | +- `src/modules/<module>/factories/<name>.factory.ts` |
| 153 | + |
| 154 | +## Requirements |
| 155 | + |
| 156 | +- Node.js >= 18.0.0 |
| 157 | +- TypeScript Backend Toolkit project structure |
| 158 | +- MongoDB connection (for seed command) |
| 159 | + |
| 160 | +## How It Works |
| 161 | + |
| 162 | +The CLI tool uses dynamic imports and model introspection to: |
| 163 | +- Analyze Mongoose schemas to detect fields, types, and relationships |
| 164 | +- Generate type-safe code following project patterns |
| 165 | +- Ensure consistency with existing codebase structure |
| 166 | + |
| 167 | +## Development |
| 168 | + |
| 169 | +```bash |
| 170 | +# Build the package |
| 171 | +pnpm build |
| 172 | + |
| 173 | +# Watch mode |
| 174 | +pnpm dev |
| 175 | + |
| 176 | +# Type check |
| 177 | +pnpm typecheck |
| 178 | +``` |
| 179 | + |
| 180 | +## License |
| 181 | + |
| 182 | +MIT |
| 183 | + |
| 184 | +## Links |
| 185 | + |
| 186 | +- [Main Toolkit Repository](https://github.com/muneebhashone/typescript-backend-toolkit) |
| 187 | +- [Report Issues](https://github.com/muneebhashone/typescript-backend-toolkit/issues) |
| 188 | + |
0 commit comments