@alexmc2/create-express-api-starter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -0
  3. package/dist/cli.js +930 -0
  4. package/dist/cli.js.map +1 -0
  5. package/package.json +70 -0
  6. package/templates/js/mvc/.env.example.ejs +7 -0
  7. package/templates/js/mvc/.eslintrc.cjs.ejs +24 -0
  8. package/templates/js/mvc/.gitignore.ejs +6 -0
  9. package/templates/js/mvc/README.md.ejs +187 -0
  10. package/templates/js/mvc/__tests__/app.test.js.ejs +51 -0
  11. package/templates/js/mvc/compose.yaml.ejs +13 -0
  12. package/templates/js/mvc/db/schema.sql.ejs +8 -0
  13. package/templates/js/mvc/db/seed.sql.ejs +7 -0
  14. package/templates/js/mvc/jest.config.js.ejs +6 -0
  15. package/templates/js/mvc/package.json.ejs +40 -0
  16. package/templates/js/mvc/scripts/dbCreate.js.ejs +97 -0
  17. package/templates/js/mvc/scripts/dbReset.js.ejs +42 -0
  18. package/templates/js/mvc/scripts/dbSeed.js.ejs +69 -0
  19. package/templates/js/mvc/scripts/dbSetup.js.ejs +69 -0
  20. package/templates/js/mvc/src/app.js.ejs +57 -0
  21. package/templates/js/mvc/src/controllers/usersController.js.ejs +32 -0
  22. package/templates/js/mvc/src/db/pool.js.ejs +19 -0
  23. package/templates/js/mvc/src/errors/AppError.js.ejs +16 -0
  24. package/templates/js/mvc/src/middleware/errorHandler.js.ejs +39 -0
  25. package/templates/js/mvc/src/middleware/notFound.js.ejs +15 -0
  26. package/templates/js/mvc/src/repositories/usersRepository.js.ejs +69 -0
  27. package/templates/js/mvc/src/routes/health.js.ejs +19 -0
  28. package/templates/js/mvc/src/routes/users.js.ejs +22 -0
  29. package/templates/js/mvc/src/server.js.ejs +21 -0
  30. package/templates/js/mvc/src/services/usersService.js.ejs +34 -0
  31. package/templates/js/mvc/src/utils/getPort.js.ejs +18 -0
  32. package/templates/js/simple/.env.example.ejs +7 -0
  33. package/templates/js/simple/.eslintrc.cjs.ejs +24 -0
  34. package/templates/js/simple/.gitignore.ejs +6 -0
  35. package/templates/js/simple/README.md.ejs +182 -0
  36. package/templates/js/simple/__tests__/app.test.js.ejs +51 -0
  37. package/templates/js/simple/compose.yaml.ejs +13 -0
  38. package/templates/js/simple/db/schema.sql.ejs +8 -0
  39. package/templates/js/simple/db/seed.sql.ejs +7 -0
  40. package/templates/js/simple/jest.config.js.ejs +6 -0
  41. package/templates/js/simple/package.json.ejs +40 -0
  42. package/templates/js/simple/scripts/dbCreate.js.ejs +97 -0
  43. package/templates/js/simple/scripts/dbReset.js.ejs +42 -0
  44. package/templates/js/simple/scripts/dbSeed.js.ejs +69 -0
  45. package/templates/js/simple/scripts/dbSetup.js.ejs +69 -0
  46. package/templates/js/simple/src/app.js.ejs +57 -0
  47. package/templates/js/simple/src/db/pool.js.ejs +19 -0
  48. package/templates/js/simple/src/errors/AppError.js.ejs +16 -0
  49. package/templates/js/simple/src/middleware/errorHandler.js.ejs +39 -0
  50. package/templates/js/simple/src/middleware/notFound.js.ejs +15 -0
  51. package/templates/js/simple/src/repositories/usersRepository.js.ejs +69 -0
  52. package/templates/js/simple/src/routes/health.js.ejs +19 -0
  53. package/templates/js/simple/src/routes/users.js.ejs +52 -0
  54. package/templates/js/simple/src/server.js.ejs +21 -0
  55. package/templates/js/simple/src/utils/getPort.js.ejs +18 -0
  56. package/templates/ts/mvc/.env.example.ejs +7 -0
  57. package/templates/ts/mvc/.eslintrc.cjs.ejs +27 -0
  58. package/templates/ts/mvc/.gitignore.ejs +6 -0
  59. package/templates/ts/mvc/README.md.ejs +188 -0
  60. package/templates/ts/mvc/__tests__/app.test.ts.ejs +45 -0
  61. package/templates/ts/mvc/compose.yaml.ejs +13 -0
  62. package/templates/ts/mvc/db/schema.sql.ejs +8 -0
  63. package/templates/ts/mvc/db/seed.sql.ejs +7 -0
  64. package/templates/ts/mvc/jest.config.js.ejs +7 -0
  65. package/templates/ts/mvc/package.json.ejs +51 -0
  66. package/templates/ts/mvc/scripts/dbCreate.js.ejs +93 -0
  67. package/templates/ts/mvc/scripts/dbReset.js.ejs +40 -0
  68. package/templates/ts/mvc/scripts/dbSeed.js.ejs +62 -0
  69. package/templates/ts/mvc/scripts/dbSetup.js.ejs +62 -0
  70. package/templates/ts/mvc/src/app.ts.ejs +45 -0
  71. package/templates/ts/mvc/src/controllers/usersController.ts.ejs +31 -0
  72. package/templates/ts/mvc/src/db/pool.ts.ejs +17 -0
  73. package/templates/ts/mvc/src/errors/AppError.ts.ejs +14 -0
  74. package/templates/ts/mvc/src/middleware/errorHandler.ts.ejs +49 -0
  75. package/templates/ts/mvc/src/middleware/notFound.ts.ejs +13 -0
  76. package/templates/ts/mvc/src/repositories/usersRepository.ts.ejs +87 -0
  77. package/templates/ts/mvc/src/routes/health.ts.ejs +13 -0
  78. package/templates/ts/mvc/src/routes/users.ts.ejs +14 -0
  79. package/templates/ts/mvc/src/server.ts.ejs +15 -0
  80. package/templates/ts/mvc/src/services/usersService.ts.ejs +35 -0
  81. package/templates/ts/mvc/src/utils/getPort.ts.ejs +12 -0
  82. package/templates/ts/mvc/tsconfig.json.ejs +13 -0
  83. package/templates/ts/simple/.env.example.ejs +7 -0
  84. package/templates/ts/simple/.eslintrc.cjs.ejs +27 -0
  85. package/templates/ts/simple/.gitignore.ejs +6 -0
  86. package/templates/ts/simple/README.md.ejs +182 -0
  87. package/templates/ts/simple/__tests__/app.test.ts.ejs +45 -0
  88. package/templates/ts/simple/compose.yaml.ejs +13 -0
  89. package/templates/ts/simple/db/schema.sql.ejs +8 -0
  90. package/templates/ts/simple/db/seed.sql.ejs +7 -0
  91. package/templates/ts/simple/jest.config.js.ejs +7 -0
  92. package/templates/ts/simple/package.json.ejs +51 -0
  93. package/templates/ts/simple/scripts/dbCreate.js.ejs +93 -0
  94. package/templates/ts/simple/scripts/dbReset.js.ejs +40 -0
  95. package/templates/ts/simple/scripts/dbSeed.js.ejs +62 -0
  96. package/templates/ts/simple/scripts/dbSetup.js.ejs +62 -0
  97. package/templates/ts/simple/src/app.ts.ejs +45 -0
  98. package/templates/ts/simple/src/db/pool.ts.ejs +17 -0
  99. package/templates/ts/simple/src/errors/AppError.ts.ejs +14 -0
  100. package/templates/ts/simple/src/middleware/errorHandler.ts.ejs +49 -0
  101. package/templates/ts/simple/src/middleware/notFound.ts.ejs +13 -0
  102. package/templates/ts/simple/src/repositories/usersRepository.ts.ejs +87 -0
  103. package/templates/ts/simple/src/routes/health.ts.ejs +13 -0
  104. package/templates/ts/simple/src/routes/users.ts.ejs +43 -0
  105. package/templates/ts/simple/src/server.ts.ejs +15 -0
  106. package/templates/ts/simple/src/utils/getPort.ts.ejs +12 -0
  107. package/templates/ts/simple/tsconfig.json.ejs +13 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,293 @@
1
+ # @alexmc2/create-express-api-starter
2
+
3
+ An interactive CLI that scaffolds beginner-friendly Express API projects. Answer a few questions and get a fully working, tested API with sensible defaults - ready to run in minutes.
4
+
5
+ ![create-express-api-starter CLI output](https://res.cloudinary.com/drbz4rq7y/image/upload/v1771349250/projects/readme_hn5fvu.png)
6
+
7
+ > Works on Linux, macOS, and Windows. The screenshot above shows Linux-specific Postgres setup, but the generated README includes instructions for all platforms.
8
+
9
+ ```bash
10
+ npx @alexmc2/create-express-api-starter my-api
11
+ ```
12
+
13
+ ---
14
+
15
+ ## Why this exists
16
+
17
+ Express is unopinionated by design. That's great, but it means every new project starts with the same setup: folder structure, middleware wiring, error handlers, test runner, linting. This tool does all of that for you so you can skip straight to writing routes.
18
+
19
+ The generated project is simple enough to understand and modify. It's not a framework - it's a starting point that gets out of your way.
20
+
21
+ ## How it works
22
+
23
+ This is a two-part system:
24
+
25
+ 1. **The CLI** (this package) - asks what you want, then generates files from templates.
26
+ 2. **The generated project** - a standalone Express API in its own folder with its own dependencies, scripts, and tests. The CLI exits after creating it.
27
+
28
+ When you run `npx @alexmc2/create-express-api-starter my-api`, npm downloads the CLI, generates the project into `my-api/`, optionally runs `npm install`, and exits. The generated project has no dependency on this tool.
29
+
30
+ ## Quick start
31
+
32
+ ```bash
33
+ # Interactive - answer a few questions
34
+ npx @alexmc2/create-express-api-starter my-api
35
+
36
+ # Skip prompts and accept all defaults
37
+ npx @alexmc2/create-express-api-starter my-api --yes
38
+
39
+ # Preview what would be generated without writing anything
40
+ npx @alexmc2/create-express-api-starter my-api --dry-run
41
+ ```
42
+
43
+ Then:
44
+
45
+ ```bash
46
+ cd my-api
47
+ cp .env.example .env
48
+ npm run dev # Start the dev server
49
+ npm test # Run the test suite
50
+ npm run lint # Run ESLint
51
+ ```
52
+
53
+ Your API is live at `http://localhost:3000`. Hit `http://localhost:3000/health` to confirm.
54
+
55
+ ## Options
56
+
57
+ The CLI walks you through these choices interactively. Use `--yes` to skip prompts and accept all defaults.
58
+
59
+ | Option | Choices | Default |
60
+ | --------------------------- | --------------------------------------------- | -------------- |
61
+ | **Language** | JavaScript, TypeScript | JavaScript |
62
+ | **Module system** (JS only) | CommonJS, ES Modules | CommonJS |
63
+ | **Dev watcher** (JS only) | `node --watch`, `nodemon` | `node --watch` |
64
+ | **Architecture** | Simple (flat), MVC (layered) | Simple |
65
+ | **Database** | In-memory, Postgres (psql), Postgres (Docker) | In-memory |
66
+ | **Educational comments** | On, Off | On |
67
+ | **Install dependencies** | Yes, No | Yes |
68
+ | **Initialise git repo** | Yes, No | Yes |
69
+
70
+ ### CLI flags
71
+
72
+ | Flag | Effect |
73
+ | -------------- | ----------------------------------------------------------- |
74
+ | `--yes` | Accept all defaults, skip prompts |
75
+ | `--dry-run` | Show the generation plan without writing files |
76
+ | `--no-install` | Skip `npm install` after generation |
77
+ | `--no-git` | Skip `git init` after generation |
78
+ | `--verbose` | Show full `npm install` output instead of the quiet default |
79
+
80
+ ## What gets generated
81
+
82
+ ### Project structure (Simple architecture, JavaScript)
83
+
84
+ ```
85
+ my-api/
86
+ ├── src/
87
+ │ ├── app.js # Express app: middleware, routes, error handling
88
+ │ ├── server.js # Starts the server on PORT
89
+ │ ├── routes/
90
+ │ │ ├── health.js # GET /health
91
+ │ │ └── users.js # GET & POST /api/users
92
+ │ ├── utils/
93
+ │ │ └── getPort.js # PORT parsing helper
94
+ │ ├── errors/
95
+ │ │ └── AppError.js # Custom error class
96
+ │ └── middleware/
97
+ │ ├── notFound.js # 404 handler
98
+ │ └── errorHandler.js # Centralised error handler
99
+ ├── __tests__/
100
+ │ └── app.test.js # Health check and users endpoint tests
101
+ ├── .env.example
102
+ ├── .gitignore
103
+ ├── .eslintrc.cjs
104
+ ├── package.json
105
+ ├── README.md
106
+ └── jest.config.js
107
+ ```
108
+
109
+ MVC architecture adds `controllers/`, `services/`, and `repositories/` directories with clear separation of concerns.
110
+
111
+ Postgres modes additionally include `db/schema.sql`, `db/seed.sql`, `src/db/` (connection pool), and setup scripts in `scripts/`. Docker mode adds a `compose.yaml`.
112
+
113
+ ### Middleware
114
+
115
+ Every generated project comes with these pre-configured:
116
+
117
+ - **express.json()** - parses JSON request bodies
118
+ - **cors** - enables cross-origin requests
119
+ - **helmet** - sets security-related HTTP headers
120
+ - **morgan** - logs HTTP requests in dev format
121
+ - **dotenv** - loads environment variables from `.env`
122
+
123
+ ### Error handling
124
+
125
+ Errors return a consistent JSON shape:
126
+
127
+ ```json
128
+ {
129
+ "status": 404,
130
+ "message": "Resource not found"
131
+ }
132
+ ```
133
+
134
+ In development, a `stack` trace is included for debugging. In production, it's omitted.
135
+
136
+ In PostgreSQL mode, duplicate values for unique fields (like `email`) return `409 Conflict` with a clear message instead of a generic `500`.
137
+
138
+ ### Scripts
139
+
140
+ **JavaScript projects:**
141
+
142
+ | Script | Command | Purpose |
143
+ | -------------- | --------------------------------------------------------------- | ---------------------------- |
144
+ | `npm run dev` | `node --watch src/server.js` or `nodemon src/server.js` | Dev server with auto-restart |
145
+ | `npm start` | `node src/server.js` | Production start |
146
+ | `npm test` | `jest` (CJS) or `node --experimental-vm-modules ... jest` (ESM) | Run test suite |
147
+ | `npm run lint` | `eslint .` | Lint source files |
148
+
149
+ Choosing `nodemon` adds it to `devDependencies` automatically.
150
+
151
+ **TypeScript projects:**
152
+
153
+ | Script | Command | Purpose |
154
+ | --------------- | ------------------------- | ---------------------------- |
155
+ | `npm run dev` | `tsx watch src/server.ts` | Dev server with auto-restart |
156
+ | `npm run build` | `tsc` | Compile to JavaScript |
157
+ | `npm start` | `node dist/server.js` | Production start (compiled) |
158
+ | `npm test` | `jest` | Run test suite |
159
+ | `npm run lint` | `eslint . --ext .ts` | Lint source files |
160
+
161
+ **Postgres modes also include:**
162
+
163
+ | Script | Purpose |
164
+ | ------------------- | ------------------------------------------------- |
165
+ | `npm run db:create` | Create the database (psql mode only) |
166
+ | `npm run db:setup` | Apply `db/schema.sql` |
167
+ | `npm run db:seed` | Insert sample data from `db/seed.sql` |
168
+ | `npm run db:reset` | Drop and recreate tables, then re-seed |
169
+ | `npm run db:up` | Start the PostgreSQL container (Docker mode only) |
170
+ | `npm run db:down` | Stop and remove the container (Docker mode only) |
171
+
172
+ ### Tests
173
+
174
+ Every generated project includes a working test suite using **Jest** and **Supertest**. Tests pass immediately after generation - no extra setup needed (in in-memory mode).
175
+
176
+ ## Database modes
177
+
178
+ ### In-memory (default)
179
+
180
+ Data lives in a plain JavaScript array. No database setup required. Data resets when the server restarts.
181
+
182
+ Good for learning the project structure and getting something running fast. The code uses a repository pattern, so switching to a real database later is straightforward.
183
+
184
+ ### Postgres (psql)
185
+
186
+ For developers who already have PostgreSQL installed locally. The generated project includes:
187
+
188
+ - `pg` as a dependency with a connection pool
189
+ - `db/schema.sql` and `db/seed.sql` for table creation and sample data
190
+ - Node scripts for database management (`db:create`, `db:setup`, `db:seed`, `db:reset`) using the `pg` library
191
+ - A project-specific database name derived from your project name (e.g. `my-api` → `my_api_dev`)
192
+ - `DATABASE_URL` pre-configured with your OS username
193
+
194
+ **Requires:** PostgreSQL installed and running locally.
195
+
196
+ ```bash
197
+ # After generation
198
+ npm run db:create # Create the database
199
+ npm run db:setup # Create tables
200
+ npm run db:seed # Insert sample data
201
+ npm run dev # Start the server
202
+ ```
203
+
204
+ The generated README includes OS-specific PostgreSQL installation and role setup instructions.
205
+
206
+ ### Postgres (Docker)
207
+
208
+ For developers who have Docker but don't want to install PostgreSQL locally. The generated project includes:
209
+
210
+ - A `compose.yaml` that runs PostgreSQL in a container (port 5433 to avoid conflicts with local installs)
211
+ - Node-based setup scripts using the `pg` library - no `psql` CLI needed on your machine
212
+ - A retry helper that waits for the database container to be ready before running setup
213
+
214
+ ```bash
215
+ # After generation
216
+ npm run db:up # Start PostgreSQL container
217
+ npm run db:setup # Create tables (retries until DB is ready)
218
+ npm run db:seed # Insert sample data
219
+ npm run dev # Start the server
220
+
221
+ # When done
222
+ npm run db:down # Stop and remove container + data
223
+ ```
224
+
225
+ ## Educational comments
226
+
227
+ Enabled by default. The generated code includes short inline comments explaining _why_ things are done a certain way:
228
+
229
+ ```javascript
230
+ // Parse JSON request bodies so route handlers can read data from req.body.
231
+ // Without this middleware, req.body is undefined for JSON requests.
232
+ app.use(express.json());
233
+
234
+ // Set common HTTP security headers.
235
+ // Helmet applies safe defaults that reduce exposure to common web attacks.
236
+ app.use(helmet());
237
+ ```
238
+
239
+ These comments cover middleware, routing, error handling, database scripts, and architecture patterns. They teach without getting in the way. Turn them off during generation if you prefer clean code.
240
+
241
+ ## Design decisions
242
+
243
+ **Opinionated defaults, easy to change.** The tool picks Jest, morgan, cors, helmet, and ESLint for you so the project works immediately. The generated README explains each choice, and everything is easy to swap out.
244
+
245
+ **Module system is a choice (JS only).** CommonJS is the default because most Express tutorials use it. ES Modules are available if you prefer modern `import`/`export` syntax. TypeScript projects always use ESM-style imports that compile to CommonJS.
246
+
247
+ **Dev watcher is a choice (JS only).** `node --watch` is built-in and needs no extra dependency (requires Node >= 20.13). `nodemon` is the alternative for developers who already use it. TypeScript projects always use `tsx watch`.
248
+
249
+ **No extras you didn't ask for.** No auth, no ORM, no migrations, no rate limiting, no OpenAPI. Those are decisions worth making when you actually need them.
250
+
251
+ ## Requirements
252
+
253
+ - **Node.js >= 20** (>= 20.13 if choosing `node --watch` as the JavaScript dev watcher)
254
+ - **npm** (ships with Node)
255
+ - **Docker** (only for Postgres Docker mode)
256
+ - **PostgreSQL** (only for Postgres psql mode)
257
+
258
+ ## Built with
259
+
260
+ **The CLI itself:**
261
+
262
+ - TypeScript + ESM, built with [tsup](https://tsup.egoist.dev/)
263
+ - [@clack/prompts](https://github.com/bombshell-elements/clack) for interactive UI
264
+ - [EJS](https://ejs.co/) for template rendering
265
+ - [Vitest](https://vitest.dev/) for the CLI's own tests
266
+
267
+ **Generated projects use:**
268
+
269
+ - Express 4
270
+ - Jest + Supertest for testing
271
+ - ESLint for linting
272
+ - `@swc/jest` for TypeScript test transforms
273
+ - `pg` for PostgreSQL (when applicable)
274
+
275
+ ## Contributing
276
+
277
+ Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
278
+
279
+ ```bash
280
+ git clone https://github.com/alexmc2/create-express-api-starter.git
281
+ cd create-express-api-starter
282
+ npm install
283
+ npm test # Run all tests (unit + integration)
284
+ npm run test:unit # Run unit tests only
285
+ npm run build # Build the CLI
286
+ npm run dev -- my-test-project # Run from source
287
+ ```
288
+
289
+ The test suite includes integration tests that generate real projects across all variant combinations, install dependencies, run their test suites, and run their linters.
290
+
291
+ ## Licence
292
+
293
+ MIT