create-backon 0.1.0 → 0.1.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 +9 -8
- package/package.json +2 -3
- package/src/generate.ts +7 -7
- package/src/prompts.ts +12 -0
- package/src/templates/base.ts +91 -21
- package/src/templates/route.ts +17 -4
package/README.md
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Scaffolds a new backend project on [@mednours/backon](https://www.npmjs.com/package/@mednours/backon) —
|
|
4
4
|
like `bun create vite@latest`, but for a consistent controller/routes/services
|
|
5
|
-
backend layout.
|
|
6
|
-
|
|
7
|
-
it yet.
|
|
5
|
+
backend layout. Prompts for Hono or Express and generates the matching
|
|
6
|
+
project either way.
|
|
8
7
|
|
|
9
8
|
## Usage
|
|
10
9
|
|
|
@@ -18,8 +17,9 @@ Or, without the `bun create` shorthand:
|
|
|
18
17
|
bunx create-backon
|
|
19
18
|
```
|
|
20
19
|
|
|
21
|
-
Prompts for a project name, a
|
|
22
|
-
[Drizzle](https://orm.drizzle.team)), and any
|
|
20
|
+
Prompts for a project name, a framework (Hono or Express), a database (Neon,
|
|
21
|
+
Postgres, SQLite, or none — via [Drizzle](https://orm.drizzle.team)), and any
|
|
22
|
+
number of routes, then generates:
|
|
23
23
|
|
|
24
24
|
```
|
|
25
25
|
my-api/
|
|
@@ -38,9 +38,10 @@ my-api/
|
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
Each route's `*.routes.ts` wires HTTP methods to controller functions,
|
|
41
|
-
`*.controller.ts` validates the request via
|
|
42
|
-
`
|
|
43
|
-
|
|
41
|
+
`*.controller.ts` validates the request via `defineRoute` (from
|
|
42
|
+
`@mednours/backon` or `@mednours/backon/express`, depending on the
|
|
43
|
+
framework you picked) and calls into `*.services.ts`, which is where real
|
|
44
|
+
data access belongs.
|
|
44
45
|
|
|
45
46
|
## Requirements
|
|
46
47
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backon",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.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",
|
|
@@ -24,8 +24,7 @@
|
|
|
24
24
|
"typecheck": "tsc --noEmit"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@clack/prompts": "^0.9.1"
|
|
28
|
-
"@mednours/backon": "workspace:*"
|
|
27
|
+
"@clack/prompts": "^0.9.1"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"@types/bun": "latest",
|
package/src/generate.ts
CHANGED
|
@@ -34,23 +34,23 @@ export async function generateProject(answers: Answers, cwd: string = process.cw
|
|
|
34
34
|
const filesWritten: string[] = [];
|
|
35
35
|
|
|
36
36
|
const files: Array<[string, string]> = [
|
|
37
|
-
[join(root, "index.ts"), renderIndexTs()],
|
|
37
|
+
[join(root, "index.ts"), renderIndexTs(answers.framework)],
|
|
38
38
|
[join(root, ".env.example"), renderEnvExample(answers.db)],
|
|
39
|
-
[join(root, "package.json"), renderPackageJson(answers.projectName, answers.db)],
|
|
39
|
+
[join(root, "package.json"), renderPackageJson(answers.projectName, answers.framework, answers.db)],
|
|
40
40
|
[join(root, "tsconfig.json"), renderTsconfig()],
|
|
41
41
|
[join(root, "README.md"), renderReadme(answers)],
|
|
42
42
|
[join(root, ".gitignore"), "node_modules/\ndist/\n*.log\n.env\n.env.local\n"],
|
|
43
|
-
[join(root, "src/app.ts"), renderAppTs()],
|
|
43
|
+
[join(root, "src/app.ts"), renderAppTs(answers.framework)],
|
|
44
44
|
[join(root, "src/config/db.ts"), renderDbFile(answers.db)],
|
|
45
|
-
[join(root, "src/middleware/example.middleware.ts"), renderExampleMiddleware()],
|
|
46
|
-
[join(root, "src/endpoints/index.ts"), renderEndpointsIndex(answers.routes)],
|
|
45
|
+
[join(root, "src/middleware/example.middleware.ts"), renderExampleMiddleware(answers.framework)],
|
|
46
|
+
[join(root, "src/endpoints/index.ts"), renderEndpointsIndex(answers.framework, answers.routes)],
|
|
47
47
|
];
|
|
48
48
|
|
|
49
49
|
for (const route of answers.routes) {
|
|
50
50
|
const dir = join(root, "src/endpoints", route);
|
|
51
51
|
files.push(
|
|
52
|
-
[join(dir, `${route}.controller.ts`), renderController(route)],
|
|
53
|
-
[join(dir, `${route}.routes.ts`), renderRoutes(route)],
|
|
52
|
+
[join(dir, `${route}.controller.ts`), renderController(route, answers.framework)],
|
|
53
|
+
[join(dir, `${route}.routes.ts`), renderRoutes(route, answers.framework)],
|
|
54
54
|
[join(dir, `${route}.services.ts`), renderServices(route)]
|
|
55
55
|
);
|
|
56
56
|
}
|
package/src/prompts.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import * as p from "@clack/prompts";
|
|
2
2
|
|
|
3
3
|
export type DbChoice = "neon" | "postgres" | "sqlite" | "none";
|
|
4
|
+
export type Framework = "hono" | "express";
|
|
4
5
|
|
|
5
6
|
export interface Answers {
|
|
6
7
|
projectName: string;
|
|
8
|
+
framework: Framework;
|
|
7
9
|
db: DbChoice;
|
|
8
10
|
routes: string[];
|
|
9
11
|
}
|
|
@@ -35,6 +37,15 @@ export async function runPrompts(): Promise<Answers> {
|
|
|
35
37
|
});
|
|
36
38
|
if (p.isCancel(projectName)) bail();
|
|
37
39
|
|
|
40
|
+
const framework = await p.select({
|
|
41
|
+
message: "Framework",
|
|
42
|
+
options: [
|
|
43
|
+
{ value: "hono", label: "Hono", hint: "faster, runs anywhere — Bun, edge, serverless" },
|
|
44
|
+
{ value: "express", label: "Express", hint: "the ecosystem you already know" },
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
if (p.isCancel(framework)) bail();
|
|
48
|
+
|
|
38
49
|
const db = await p.select({
|
|
39
50
|
message: "Database",
|
|
40
51
|
options: [
|
|
@@ -63,6 +74,7 @@ export async function runPrompts(): Promise<Answers> {
|
|
|
63
74
|
|
|
64
75
|
return {
|
|
65
76
|
projectName: projectName as string,
|
|
77
|
+
framework: framework as Framework,
|
|
66
78
|
db: db as DbChoice,
|
|
67
79
|
routes,
|
|
68
80
|
};
|
package/src/templates/base.ts
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import type { Answers } from "../prompts";
|
|
1
|
+
import type { Answers, Framework } from "../prompts";
|
|
2
2
|
import { camelCase } from "./route";
|
|
3
3
|
import { dbDependencies, dbEnvVars, dbReadmeNote } from "./db";
|
|
4
4
|
|
|
5
|
-
export function renderIndexTs(): string {
|
|
6
|
-
|
|
5
|
+
export function renderIndexTs(framework: Framework): string {
|
|
6
|
+
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
7
|
+
return `import { serve } from "${backonImport}";
|
|
7
8
|
import { app } from "./src/app";
|
|
8
9
|
|
|
9
10
|
serve(app);
|
|
10
11
|
`;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
export function renderAppTs(): string {
|
|
14
|
+
export function renderAppTs(framework: Framework): string {
|
|
15
|
+
if (framework === "express") {
|
|
16
|
+
return `import { createApp } from "@mednours/backon/express";
|
|
17
|
+
import { endpoints } from "./endpoints";
|
|
18
|
+
import { exampleMiddleware } from "./middleware/example.middleware";
|
|
19
|
+
|
|
20
|
+
export const app = createApp();
|
|
21
|
+
|
|
22
|
+
app.use(exampleMiddleware);
|
|
23
|
+
app.use("/", endpoints);
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
|
|
14
27
|
return `import { createApp } from "@mednours/backon";
|
|
15
28
|
import { endpoints } from "./endpoints";
|
|
16
29
|
import { exampleMiddleware } from "./middleware/example.middleware";
|
|
@@ -22,7 +35,31 @@ app.route("/", endpoints);
|
|
|
22
35
|
`;
|
|
23
36
|
}
|
|
24
37
|
|
|
25
|
-
export function renderExampleMiddleware(): string {
|
|
38
|
+
export function renderExampleMiddleware(framework: Framework): string {
|
|
39
|
+
if (framework === "express") {
|
|
40
|
+
return `import type { Request, Response, NextFunction } from "@mednours/backon/express";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Example middleware, just to show the pattern — stamps a request-start
|
|
44
|
+
* timestamp onto res.locals. Error handling, request logging, secure
|
|
45
|
+
* headers, CORS, and the request body size limit are already wired into
|
|
46
|
+
* createApp() by backon itself; you don't need your own
|
|
47
|
+
* error.middleware.ts unless you want to override that default behavior.
|
|
48
|
+
*
|
|
49
|
+
* This is plain Express middleware (req, res, next), not backon's
|
|
50
|
+
* BackonContext — that wrapper only exists inside defineRoute() handlers.
|
|
51
|
+
* Middleware always operates at the framework's own layer. (Also: Express
|
|
52
|
+
* middleware can't safely set a response header after calling next() — by
|
|
53
|
+
* then the response may already be sent — which is why this demonstrates
|
|
54
|
+
* res.locals instead of a header.)
|
|
55
|
+
*/
|
|
56
|
+
export function exampleMiddleware(req: Request, res: Response, next: NextFunction) {
|
|
57
|
+
res.locals.requestStartedAt = Date.now();
|
|
58
|
+
next();
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
}
|
|
62
|
+
|
|
26
63
|
return `import type { Next } from "@mednours/backon";
|
|
27
64
|
import type { BackonContext } from "@mednours/backon";
|
|
28
65
|
|
|
@@ -41,12 +78,23 @@ export async function exampleMiddleware(c: BackonContext, next: Next) {
|
|
|
41
78
|
`;
|
|
42
79
|
}
|
|
43
80
|
|
|
44
|
-
export function renderEndpointsIndex(routes: string[]): string {
|
|
81
|
+
export function renderEndpointsIndex(framework: Framework, routes: string[]): string {
|
|
45
82
|
const imports = routes
|
|
46
83
|
.map((route) => `import { ${camelCase(route)}Routes } from "./${route}/${route}.routes";`)
|
|
47
84
|
.join("\n");
|
|
48
|
-
const mounts = routes.map((route) => `endpoints.route("/${route}", ${camelCase(route)}Routes);`).join("\n");
|
|
49
85
|
|
|
86
|
+
if (framework === "express") {
|
|
87
|
+
const mounts = routes.map((route) => `endpoints.use("/${route}", ${camelCase(route)}Routes);`).join("\n");
|
|
88
|
+
return `import { Router } from "@mednours/backon/express";
|
|
89
|
+
${imports}
|
|
90
|
+
|
|
91
|
+
export const endpoints = Router();
|
|
92
|
+
|
|
93
|
+
${mounts}
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const mounts = routes.map((route) => `endpoints.route("/${route}", ${camelCase(route)}Routes);`).join("\n");
|
|
50
98
|
return `import { Hono } from "@mednours/backon";
|
|
51
99
|
${imports}
|
|
52
100
|
|
|
@@ -69,7 +117,7 @@ export function renderTsconfig(): string {
|
|
|
69
117
|
"module": "ESNext",
|
|
70
118
|
"moduleResolution": "bundler",
|
|
71
119
|
"lib": ["ESNext"],
|
|
72
|
-
"types": ["bun
|
|
120
|
+
"types": ["bun"],
|
|
73
121
|
"strict": true,
|
|
74
122
|
"esModuleInterop": true,
|
|
75
123
|
"skipLibCheck": true,
|
|
@@ -84,13 +132,36 @@ export function renderTsconfig(): string {
|
|
|
84
132
|
`;
|
|
85
133
|
}
|
|
86
134
|
|
|
87
|
-
export function renderPackageJson(projectName: string, db: Answers["db"]): string {
|
|
88
|
-
const dependencies: Record<string, string> =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
135
|
+
export function renderPackageJson(projectName: string, framework: Framework, db: Answers["db"]): string {
|
|
136
|
+
const dependencies: Record<string, string> =
|
|
137
|
+
framework === "express"
|
|
138
|
+
? {
|
|
139
|
+
"@mednours/backon": "latest",
|
|
140
|
+
express: "^4.21.0",
|
|
141
|
+
cors: "^2.8.5",
|
|
142
|
+
helmet: "^8.0.0",
|
|
143
|
+
compression: "^1.7.5",
|
|
144
|
+
zod: "^3.24.1",
|
|
145
|
+
...dbDependencies(db),
|
|
146
|
+
}
|
|
147
|
+
: {
|
|
148
|
+
"@mednours/backon": "latest",
|
|
149
|
+
hono: "^4.6.14",
|
|
150
|
+
zod: "^3.24.1",
|
|
151
|
+
...dbDependencies(db),
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const devDependencies: Record<string, string> =
|
|
155
|
+
framework === "express"
|
|
156
|
+
? {
|
|
157
|
+
"@types/bun": "latest",
|
|
158
|
+
"@types/express": "^4.17.21",
|
|
159
|
+
typescript: "^5.7.2",
|
|
160
|
+
}
|
|
161
|
+
: {
|
|
162
|
+
"@types/bun": "latest",
|
|
163
|
+
typescript: "^5.7.2",
|
|
164
|
+
};
|
|
94
165
|
|
|
95
166
|
const pkg = {
|
|
96
167
|
name: projectName,
|
|
@@ -104,10 +175,7 @@ export function renderPackageJson(projectName: string, db: Answers["db"]): strin
|
|
|
104
175
|
typecheck: "tsc --noEmit",
|
|
105
176
|
},
|
|
106
177
|
dependencies,
|
|
107
|
-
devDependencies
|
|
108
|
-
"@types/bun": "latest",
|
|
109
|
-
typescript: "^5.7.2",
|
|
110
|
-
},
|
|
178
|
+
devDependencies,
|
|
111
179
|
};
|
|
112
180
|
|
|
113
181
|
return `${JSON.stringify(pkg, null, 2)}\n`;
|
|
@@ -118,9 +186,11 @@ export function renderReadme(answers: Answers): string {
|
|
|
118
186
|
? answers.routes.map((route) => `- \`src/endpoints/${route}/\``).join("\n")
|
|
119
187
|
: "- (none yet — add one under src/endpoints/)";
|
|
120
188
|
|
|
189
|
+
const backonImport = answers.framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
190
|
+
|
|
121
191
|
return `# ${answers.projectName}
|
|
122
192
|
|
|
123
|
-
Generated by \`create-backon
|
|
193
|
+
Generated by \`create-backon\` (${answers.framework}).
|
|
124
194
|
|
|
125
195
|
## Getting started
|
|
126
196
|
|
|
@@ -140,7 +210,7 @@ ${routeList}
|
|
|
140
210
|
|
|
141
211
|
Each route folder follows the same shape: \`*.routes.ts\` wires HTTP methods
|
|
142
212
|
to controller functions, \`*.controller.ts\` validates the request (via
|
|
143
|
-
\`defineRoute\` from
|
|
213
|
+
\`defineRoute\` from \`${backonImport}\`) and calls into \`*.services.ts\`,
|
|
144
214
|
which is where real data access belongs.
|
|
145
215
|
`;
|
|
146
216
|
}
|
package/src/templates/route.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Framework } from "../prompts";
|
|
2
|
+
|
|
1
3
|
function pascalCase(name: string): string {
|
|
2
4
|
return name
|
|
3
5
|
.split("-")
|
|
@@ -5,10 +7,11 @@ function pascalCase(name: string): string {
|
|
|
5
7
|
.join("");
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
export function renderController(route: string): string {
|
|
10
|
+
export function renderController(route: string, framework: Framework): string {
|
|
9
11
|
const Route = pascalCase(route);
|
|
12
|
+
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
10
13
|
return `import { z } from "zod";
|
|
11
|
-
import { defineRoute } from "
|
|
14
|
+
import { defineRoute } from "${backonImport}";
|
|
12
15
|
import * as service from "./${route}.services";
|
|
13
16
|
|
|
14
17
|
export const list = defineRoute({}, async (c) => {
|
|
@@ -26,8 +29,18 @@ export const create = defineRoute({ body: create${Route}Schema }, async (c, { bo
|
|
|
26
29
|
`;
|
|
27
30
|
}
|
|
28
31
|
|
|
29
|
-
export function renderRoutes(route: string): string {
|
|
30
|
-
|
|
32
|
+
export function renderRoutes(route: string, framework: Framework): string {
|
|
33
|
+
if (framework === "express") {
|
|
34
|
+
return `import { Router } from "@mednours/backon/express";
|
|
35
|
+
import * as controller from "./${route}.controller";
|
|
36
|
+
|
|
37
|
+
export const ${camelCase(route)}Routes = Router();
|
|
38
|
+
|
|
39
|
+
${camelCase(route)}Routes.get("/", controller.list);
|
|
40
|
+
${camelCase(route)}Routes.post("/", controller.create);
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
|
|
31
44
|
return `import { Hono, type Env } from "@mednours/backon";
|
|
32
45
|
import * as controller from "./${route}.controller";
|
|
33
46
|
|