create-backon 0.4.0 → 0.6.1

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.
package/README.md CHANGED
@@ -44,11 +44,11 @@ my-api/
44
44
  Each route's `*.routes.ts` wires HTTP methods to controller functions,
45
45
  `*.controller.ts` validates the request via `defineRoute` (from
46
46
  `@mednours/backon` or `@mednours/backon/express`, depending on the
47
- framework you picked) and calls into `*.services.ts`, which is where real
48
- data access belongs. In JavaScript mode every file is generated without any
47
+ framework picked) and calls into `*.services.ts`, which is where real data
48
+ access belongs. In JavaScript mode every file is generated without any
49
49
  TypeScript-only syntax (no `import type`, no generics, no type
50
- annotations) — it's not TypeScript with the types stripped, it's meant to
51
- be genuinely plain JavaScript, with JSDoc `@param` comments on the one
50
+ annotations) — generated files are genuinely plain JavaScript rather than
51
+ TypeScript with the types stripped, with JSDoc `@param` comments on the one
52
52
  generated function (the example middleware) that would otherwise have had
53
53
  a type annotation, so editors still get intellisense without requiring
54
54
  TypeScript as a dependency.
@@ -60,21 +60,32 @@ running this right after `git init` on a fresh repo doesn't get blocked.
60
60
 
61
61
  ## Adding a route to an existing project
62
62
 
63
- Re-running the full generator isn't the way to add one more route to a
64
- project you already scaffolded. Use the `add-route` subcommand instead,
65
- from inside the project:
63
+ Re-running the full generator isn't the way to add one more route to an
64
+ already-scaffolded project. Every generated project's `package.json`
65
+ includes a script for this, so from inside the project:
66
66
 
67
67
  ```
68
- bunx create-backon add-route users
68
+ bun run add-route users
69
69
  ```
70
70
 
71
- (`bun create backon add-route users` also works — `bun create` forwards
72
- extra arguments straight through. `route` works as a shorthand for
73
- `add-route` either way.) Leave the name off and it'll prompt for one:
71
+ `bun run` forwards `users` straight through, no `--` needed. Leave the name
72
+ off and it prompts for one:
74
73
 
75
74
  ```
76
- bunx create-backon add-route
75
+ bun run add-route
76
+ ```
77
+
78
+ The same command also works outside a generated `package.json`, or for a
79
+ project scaffolded before this script existed:
80
+
77
81
  ```
82
+ bunx create-backon add-route users
83
+ ```
84
+
85
+ (`bun create backon add-route users` also works — `bun create` forwards
86
+ extra arguments straight through. `route` works as a shorthand for
87
+ `add-route` either way, and `bun run add-route` is exactly this command
88
+ under a shorter name.)
78
89
 
79
90
  It reads the project's own `package.json` to figure out whether it's
80
91
  Hono or Express, TypeScript or JavaScript — no need to answer those again
@@ -89,12 +100,18 @@ name that's already mounted.
89
100
 
90
101
  The undo for `add-route`:
91
102
 
103
+ ```
104
+ bun run remove-route users
105
+ ```
106
+
107
+ or directly:
108
+
92
109
  ```
93
110
  bunx create-backon remove-route users
94
111
  ```
95
112
 
96
113
  (`rm-route` also works.) Leave the name off to be prompted for one. Either
97
- way you'll be asked to confirm before anything is deleted — this removes
114
+ way, confirmation is required before anything is deleted — this removes
98
115
  `src/endpoints/users/` and unmounts it from `src/endpoints/index.ts`.
99
116
  Refuses if the route isn't currently mounted, rather than guessing at
100
117
  deleting a folder that was never wired up.
@@ -102,27 +119,50 @@ deleting a folder that was never wired up.
102
119
  ## Listing routes
103
120
 
104
121
  ```
105
- bunx create-backon list-routes
122
+ bun run list-routes
106
123
  ```
107
124
 
108
- (`routes` also works.) Read-only — prints every route currently mounted in
109
- `src/endpoints/index.ts`.
125
+ or `bunx create-backon list-routes` (`routes` also works). Read-only —
126
+ prints every route currently mounted in `src/endpoints/index.ts`.
110
127
 
111
128
  ## Checking a project for drift
112
129
 
113
130
  ```
114
- bunx create-backon doctor
131
+ bun run doctor
115
132
  ```
116
133
 
117
- Compares what's actually in `src/endpoints/` against what `index.<ext>`
118
- mounts, and flags three things: a route folder that exists but isn't
119
- mounted (an orphan), a mount in `index.<ext>` pointing at a folder that's
120
- gone (dangling), and a route folder missing one of its three expected
121
- files. This kind of drift usually comes from hand-editing `index.ts`,
122
- deleting a route folder without using `remove-route`, or copy-pasting a
123
- route folder without wiring it up. `doctor` only reports issues — it
124
- doesn't fix them — and exits non-zero if it finds any, so it's usable in
125
- a pre-commit hook or CI step.
134
+ or `bunx create-backon doctor`. Compares what's actually in
135
+ `src/endpoints/` against what `index.<ext>` mounts, and flags three things:
136
+ a route folder that exists but isn't mounted (an orphan), a mount in
137
+ `index.<ext>` pointing at a folder that's gone (dangling), and a route
138
+ folder missing one of its three expected files. This kind of drift usually
139
+ comes from hand-editing `index.ts`, deleting a route folder without using
140
+ `remove-route`, or copy-pasting a route folder without wiring it up.
141
+ `doctor` only reports issues — it doesn't fix them — and exits non-zero if
142
+ it finds any, so it's usable in a pre-commit hook or CI step.
143
+
144
+ ## Generating API docs
145
+
146
+ Every generated project includes a `docs` script:
147
+
148
+ ```
149
+ bun run docs
150
+ ```
151
+
152
+ Writes `openapi.json` (built from the Zod schemas already passed to
153
+ `defineRoute()` across every route) and `docs.html` (a rendered reference
154
+ page, [Scalar](https://github.com/scalar/scalar), pointed at that spec) to
155
+ the project root. Both come from `scripts/generate-docs.<ext>`, generated
156
+ alongside everything else — it imports `src/app` directly rather than
157
+ starting a real server, so `bun run docs` works without anything listening
158
+ on a port. Both output files are build artifacts, not source, so they're in
159
+ the generated `.gitignore` by default.
160
+
161
+ This is separate from `serveApiDocs(app)` (documented in
162
+ [coding-with-backon.md](../../docs/coding-with-backon.md)), which mounts the
163
+ same spec and reference page as live routes on a running server instead of
164
+ writing them to disk — useful for CI, static hosting, or feeding the spec
165
+ into an external tool, where `serveApiDocs`'s live routes don't apply.
126
166
 
127
167
  ## Requirements
128
168
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-backon",
3
- "version": "0.4.0",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "description": "Scaffolds a new backend project on backon — like `bun create vite@latest`, but for a consistent controller/routes/services backend layout.",
6
6
  "license": "MIT",
package/src/generate.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  renderEndpointsIndex,
7
7
  renderEnvExample,
8
8
  renderExampleMiddleware,
9
+ renderGenerateDocsScript,
9
10
  renderIndexTs,
10
11
  renderJsconfig,
11
12
  renderPackageJson,
@@ -90,7 +91,7 @@ export async function generateProject(answers: Answers, cwd: string = process.cw
90
91
  ? [join(root, "jsconfig.json"), renderJsconfig()]
91
92
  : [join(root, "tsconfig.json"), renderTsconfig()],
92
93
  [join(root, "README.md"), renderReadme(readmeAnswers)],
93
- [join(root, ".gitignore"), "node_modules/\ndist/\n*.log\n.env\n.env.local\n"],
94
+ [join(root, ".gitignore"), "node_modules/\ndist/\n*.log\n.env\n.env.local\nopenapi.json\ndocs.html\n"],
94
95
  [join(root, `src/app.${ext}`), renderAppTs(answers.framework)],
95
96
  [join(root, `src/config/db.${ext}`), renderDbFile(answers.db)],
96
97
  [
@@ -98,6 +99,7 @@ export async function generateProject(answers: Answers, cwd: string = process.cw
98
99
  renderExampleMiddleware(answers.framework, answers.language),
99
100
  ],
100
101
  [join(root, `src/endpoints/index.${ext}`), renderEndpointsIndex(answers.framework, answers.routes)],
102
+ [join(root, `scripts/generate-docs.${ext}`), renderGenerateDocsScript(packageName, answers.framework)],
101
103
  ];
102
104
 
103
105
  for (const route of answers.routes) {
package/src/prompts.ts CHANGED
@@ -79,7 +79,7 @@ export async function runPrompts(): Promise<Answers> {
79
79
  if (p.isCancel(copyEnv)) bail();
80
80
 
81
81
  const routes: string[] = [];
82
- p.log.step("Add routes one at a time. Leave the name blank when you're done.");
82
+ p.log.step("Add routes one at a time. Leave the name blank to finish.");
83
83
  // eslint-disable-next-line no-constant-condition
84
84
  while (true) {
85
85
  const name = await p.text({
@@ -49,8 +49,8 @@ export function renderExampleMiddleware(framework: Framework, language: Language
49
49
  * Example middleware, just to show the pattern — stamps a request-start
50
50
  * timestamp onto res.locals. Error handling, request logging, secure
51
51
  * headers, CORS, and the request body size limit are already wired into
52
- * createApp() by backon itself; you don't need your own
53
- * error.middleware.js unless you want to override that default behavior.
52
+ * createApp() by backon itself; a separate
53
+ * error.middleware.js is only needed to override that default behavior.
54
54
  *
55
55
  * This is plain Express middleware (req, res, next), not backon's
56
56
  * BackonContext — that wrapper only exists inside defineRoute() handlers.
@@ -75,8 +75,8 @@ export function exampleMiddleware(req, res, next) {
75
75
  * Example middleware, just to show the pattern — stamps a request-start
76
76
  * timestamp onto res.locals. Error handling, request logging, secure
77
77
  * headers, CORS, and the request body size limit are already wired into
78
- * createApp() by backon itself; you don't need your own
79
- * error.middleware.ts unless you want to override that default behavior.
78
+ * createApp() by backon itself; a separate
79
+ * error.middleware.ts is only needed to override that default behavior.
80
80
  *
81
81
  * This is plain Express middleware (req, res, next), not backon's
82
82
  * BackonContext — that wrapper only exists inside defineRoute() handlers.
@@ -97,8 +97,8 @@ export function exampleMiddleware(req: Request, res: Response, next: NextFunctio
97
97
  * Example middleware, just to show the pattern — adds a response-time
98
98
  * header. Error handling, request logging, secure headers, CORS, and the
99
99
  * request body size limit are already wired into createApp() by backon
100
- * itself; you don't need your own error.middleware.js unless you want to
101
- * override that default behavior.
100
+ * itself; a separate error.middleware.js is only needed to override that
101
+ * default behavior.
102
102
  *
103
103
  * @param {import("@mednours/backon").BackonContext} c
104
104
  * @param {import("@mednours/backon").Next} next
@@ -117,8 +117,8 @@ import type { BackonContext } from "@mednours/backon";
117
117
  * Example middleware, just to show the pattern — adds a response-time
118
118
  * header. Error handling, request logging, secure headers, CORS, and the
119
119
  * request body size limit are already wired into createApp() by backon
120
- * itself; you don't need your own error.middleware.ts unless you want to
121
- * override that default behavior.
120
+ * itself; a separate error.middleware.ts is only needed to override that
121
+ * default behavior.
122
122
  */
123
123
  export async function exampleMiddleware(c: BackonContext, next: Next) {
124
124
  const start = performance.now();
@@ -154,6 +154,31 @@ ${mounts}
154
154
  `;
155
155
  }
156
156
 
157
+ // Not language-aware, same reasoning as renderIndexTs/renderAppTs above: no
158
+ // type annotations, so the same content is valid JS or TS. Imports "../src/app"
159
+ // rather than starting the server, so running this doesn't require anything
160
+ // to be listening — generateOpenApiSpec() only needs the app's routes
161
+ // registered, which happens at import time.
162
+ //
163
+ // docs.html embeds the spec directly (apiReferenceHtmlInline), rather than
164
+ // pointing at ./openapi.json the way the live serveApiDocs() page does —
165
+ // this file gets opened straight from disk as often as it's served over
166
+ // HTTP, and a browser blocks one file:// URL from fetching another, so a
167
+ // URL-based page would fail exactly there.
168
+ export function renderGenerateDocsScript(projectName: string, framework: Framework): string {
169
+ const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
170
+ return `import { writeFileSync } from "node:fs";
171
+ import { generateOpenApiSpec, apiReferenceHtmlInline } from "${backonImport}";
172
+ import { app } from "../src/app";
173
+
174
+ const spec = generateOpenApiSpec(app, { title: "${projectName}", version: "0.1.0" });
175
+ writeFileSync("openapi.json", JSON.stringify(spec, null, 2));
176
+ writeFileSync("docs.html", apiReferenceHtmlInline(spec, "${projectName}"));
177
+
178
+ console.log("Wrote openapi.json and docs.html");
179
+ `;
180
+ }
181
+
157
182
  export function renderEnvExample(db: Answers["db"]): string {
158
183
  return `PORT=3000
159
184
  ALLOWED_ORIGINS=
@@ -177,7 +202,7 @@ export function renderTsconfig(): string {
177
202
  "noEmit": true,
178
203
  "allowJs": false
179
204
  },
180
- "include": ["index.ts", "src"]
205
+ "include": ["index.ts", "src", "scripts"]
181
206
  }
182
207
  `;
183
208
  }
@@ -200,7 +225,7 @@ export function renderJsconfig(): string {
200
225
  "checkJs": false,
201
226
  "allowJs": true
202
227
  },
203
- "include": ["index.js", "src"]
228
+ "include": ["index.js", "src", "scripts"]
204
229
  }
205
230
  `;
206
231
  }
@@ -245,6 +270,16 @@ export function renderPackageJson(
245
270
  dev: `bun run --watch index.${ext}`,
246
271
  start: `bun run index.${ext}`,
247
272
  test: "bun test",
273
+ // Thin aliases over `bunx create-backon <subcommand>` — running
274
+ // `bun run add-route users` from inside the project is shorter than
275
+ // remembering the create-backon invocation, and bun forwards extra
276
+ // arguments (like the route name) straight through without needing
277
+ // a `--` separator.
278
+ "add-route": "bunx create-backon add-route",
279
+ "remove-route": "bunx create-backon remove-route",
280
+ "list-routes": "bunx create-backon list-routes",
281
+ doctor: "bunx create-backon doctor",
282
+ docs: `bun run scripts/generate-docs.${ext}`,
248
283
  };
249
284
  if (language === "typescript") {
250
285
  scripts.typecheck = "tsc --noEmit";
@@ -39,7 +39,7 @@ import { Pool } from "pg";
39
39
  // NOTE: this pooled connection is for local dev or a long-running server
40
40
  // (Docker, a VPS, Render, etc). It is NOT safe to use as-is on Vercel
41
41
  // serverless functions — each cold start can open a new connection until
42
- // you exhaust the database's connection limit. Use the "neon" db option
42
+ // the database's connection limit is exhausted. Use the "neon" db option
43
43
  // instead for serverless deploys.
44
44
 
45
45
  if (!process.env.DATABASE_URL) {