create-backon 0.6.2 → 0.10.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.
- package/README.md +141 -12
- package/dist/cli.js +1675 -0
- package/package.json +10 -6
- package/src/add-method.ts +101 -0
- package/src/add-route.ts +11 -9
- package/src/cli.ts +512 -3
- package/src/colors.ts +35 -0
- package/src/db-status.ts +117 -0
- package/src/generate.ts +17 -6
- package/src/prompts.ts +26 -0
- package/src/remove-method.ts +100 -0
- package/src/route-methods.ts +100 -0
- package/src/templates/base.ts +95 -5
- package/src/templates/route.ts +143 -64
package/src/templates/route.ts
CHANGED
|
@@ -1,107 +1,186 @@
|
|
|
1
1
|
import type { Framework, Language } from "../prompts";
|
|
2
2
|
|
|
3
|
-
function pascalCase(name: string): string {
|
|
3
|
+
export function pascalCase(name: string): string {
|
|
4
4
|
return name
|
|
5
5
|
.split("-")
|
|
6
6
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
7
7
|
.join("");
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// file is already valid JavaScript as-is. Only the file extension changes.
|
|
14
|
-
export function renderController(route: string, framework: Framework): string {
|
|
15
|
-
const Route = pascalCase(route);
|
|
16
|
-
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
17
|
-
return `import { z } from "zod";
|
|
18
|
-
import { defineRoute } from "${backonImport}";
|
|
19
|
-
import * as service from "./${route}.services";
|
|
20
|
-
|
|
21
|
-
export const list = defineRoute({}, async (c) => {
|
|
22
|
-
const items = await service.list();
|
|
23
|
-
return c.json(items);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// TODO: replace with the real shape of a ${Route}
|
|
27
|
-
const create${Route}Schema = z.object({});
|
|
28
|
-
|
|
29
|
-
export const create = defineRoute({ body: create${Route}Schema }, async (c, { body }) => {
|
|
30
|
-
const item = await service.create(body);
|
|
31
|
-
return c.json(item, 201);
|
|
32
|
-
});
|
|
33
|
-
`;
|
|
10
|
+
export function camelCase(name: string): string {
|
|
11
|
+
const [first, ...rest] = name.split("-");
|
|
12
|
+
return [first, ...rest.map((part) => part.charAt(0).toUpperCase() + part.slice(1))].join("");
|
|
34
13
|
}
|
|
35
14
|
|
|
36
|
-
export
|
|
15
|
+
export type HttpVerb = "get" | "post" | "put" | "patch" | "delete";
|
|
16
|
+
|
|
17
|
+
export const HTTP_VERBS: readonly HttpVerb[] = ["get", "post", "put", "patch", "delete"];
|
|
18
|
+
|
|
19
|
+
export interface MethodSpec {
|
|
20
|
+
verb: HttpVerb;
|
|
21
|
+
/** e.g. "/" or "/:id" — path params come straight from this, nothing else derives them. */
|
|
22
|
+
path: string;
|
|
23
|
+
/** Identifier for the controller export and the matching service function. Caller's job to keep it unique. */
|
|
24
|
+
name: string;
|
|
25
|
+
needsBody: boolean;
|
|
26
|
+
needsQuery: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Path params come straight from the URL pattern — no guessing needed, unlike body/query shape. */
|
|
30
|
+
export function paramNamesFromPath(path: string): string[] {
|
|
31
|
+
return [...path.matchAll(/:([A-Za-z0-9_]+)/g)]
|
|
32
|
+
.map((match) => match[1])
|
|
33
|
+
.filter((name): name is string => name !== undefined);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Blank endpoint — an endpoint folder with zero methods wired yet. This is
|
|
38
|
+
// what every route starts as now, whether it comes from the initial
|
|
39
|
+
// `bun create backon` route prompt or a later `add-route`: methods are
|
|
40
|
+
// always a deliberate second step via `bun run add-method`, rather than
|
|
41
|
+
// guessing that every route wants a GET-list/POST-create pair.
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
const PLACEHOLDER_COMMENT = "// No methods yet — run `bun run add-method` to add one.";
|
|
45
|
+
|
|
46
|
+
export function renderBlankController(): string {
|
|
47
|
+
return `${PLACEHOLDER_COMMENT}\n`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function renderBlankRoutes(route: string, framework: Framework, language: Language): string {
|
|
51
|
+
const varName = `${camelCase(route)}Routes`;
|
|
52
|
+
|
|
37
53
|
if (framework === "express") {
|
|
38
|
-
// Router() takes no generic parameter either way — same content for
|
|
39
|
-
// both languages, same reasoning as renderController above.
|
|
40
54
|
return `import { Router } from "@mednours/backon/express";
|
|
41
|
-
import * as controller from "./${route}.controller";
|
|
42
55
|
|
|
43
|
-
export const ${
|
|
56
|
+
export const ${varName} = Router();
|
|
44
57
|
|
|
45
|
-
${
|
|
46
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
58
|
+
${PLACEHOLDER_COMMENT}
|
|
47
59
|
`;
|
|
48
60
|
}
|
|
49
61
|
|
|
50
62
|
if (language === "javascript") {
|
|
51
|
-
// `new Hono<Env>()` is TypeScript-only syntax (a generic type
|
|
52
|
-
// argument) — not valid JavaScript, so the JS variant drops both the
|
|
53
|
-
// `type Env` import and the `<Env>` argument. The Env generic only
|
|
54
|
-
// affects c.get()/c.set() typing anyway, which doesn't exist in plain
|
|
55
|
-
// JS regardless.
|
|
56
63
|
return `import { Hono } from "@mednours/backon";
|
|
57
|
-
import * as controller from "./${route}.controller";
|
|
58
64
|
|
|
59
|
-
export const ${
|
|
65
|
+
export const ${varName} = new Hono();
|
|
60
66
|
|
|
61
|
-
${
|
|
62
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
67
|
+
${PLACEHOLDER_COMMENT}
|
|
63
68
|
`;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
return `import { Hono, type Env } from "@mednours/backon";
|
|
67
|
-
import * as controller from "./${route}.controller";
|
|
68
72
|
|
|
69
|
-
export const ${
|
|
73
|
+
export const ${varName} = new Hono<Env>();
|
|
70
74
|
|
|
71
|
-
${
|
|
72
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
75
|
+
${PLACEHOLDER_COMMENT}
|
|
73
76
|
`;
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
export function
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
export function renderBlankServices(): string {
|
|
80
|
+
return `// No routes yet — service functions land here as methods are added via \`bun run add-method\`.\n`;
|
|
81
|
+
}
|
|
79
82
|
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// Per-method codegen — appended to an existing endpoint's three files by
|
|
85
|
+
// add-method.ts, one HTTP method at a time. Handler signature is always
|
|
86
|
+
// `(c, { body, query, params })`, regardless of which schemas this method
|
|
87
|
+
// actually declares (defineRoute always hands back all three, unused ones
|
|
88
|
+
// just come through as undefined) — keeping it uniform means no conditional
|
|
89
|
+
// destructuring to get right, and the generated tsconfig has neither
|
|
90
|
+
// noUnusedLocals nor noUnusedParameters on, so an unused destructured name
|
|
91
|
+
// is not a compile error.
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
function defaultServiceReturn(spec: MethodSpec): string {
|
|
95
|
+
const { verb, needsBody } = spec;
|
|
96
|
+
const hasParams = paramNamesFromPath(spec.path).length > 0;
|
|
97
|
+
|
|
98
|
+
if (verb === "get" && !hasParams) return "return [];";
|
|
99
|
+
if (verb === "get") return "return null; // TODO: fetch the real record";
|
|
100
|
+
if (verb === "post") return needsBody ? "return { id: crypto.randomUUID(), ...body };" : "return { id: crypto.randomUUID() };";
|
|
101
|
+
if (verb === "put" || verb === "patch") return needsBody ? "return { ...params, ...body };" : "return { ...params };";
|
|
102
|
+
return "return { success: true };"; // delete
|
|
82
103
|
}
|
|
83
104
|
|
|
84
|
-
|
|
85
|
-
|
|
105
|
+
/** Positional args the controller passes to the matching service function — only what this method actually declared. */
|
|
106
|
+
function serviceCallArgs(spec: MethodSpec): string[] {
|
|
107
|
+
const args: string[] = [];
|
|
108
|
+
if (paramNamesFromPath(spec.path).length > 0) args.push("params");
|
|
109
|
+
if (spec.needsBody) args.push("body");
|
|
110
|
+
if (spec.needsQuery) args.push("query");
|
|
111
|
+
return args;
|
|
86
112
|
}
|
|
87
|
-
`;
|
|
88
|
-
}
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
/** Matching typed parameter list for the service function itself (TS only — see renderServiceMethod). */
|
|
115
|
+
function serviceParamList(spec: MethodSpec): string {
|
|
116
|
+
const parts: string[] = [];
|
|
117
|
+
if (paramNamesFromPath(spec.path).length > 0) parts.push(`params: z.infer<typeof ${spec.name}ParamsSchema>`);
|
|
118
|
+
if (spec.needsBody) parts.push(`body: z.infer<typeof ${spec.name}BodySchema>`);
|
|
119
|
+
if (spec.needsQuery) parts.push(`query: z.infer<typeof ${spec.name}QuerySchema>`);
|
|
120
|
+
return parts.join(", ");
|
|
121
|
+
}
|
|
91
122
|
|
|
92
|
-
export
|
|
93
|
-
|
|
123
|
+
export function controllerImports(route: string, framework: Framework): string[] {
|
|
124
|
+
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
125
|
+
return [`import { z } from "zod";`, `import { defineRoute } from "${backonImport}";`, `import * as service from "./${route}.services";`];
|
|
94
126
|
}
|
|
95
127
|
|
|
96
|
-
export
|
|
97
|
-
|
|
128
|
+
export function renderControllerMethod(spec: MethodSpec): string {
|
|
129
|
+
const paramNames = paramNamesFromPath(spec.path);
|
|
130
|
+
const lines: string[] = [];
|
|
131
|
+
|
|
132
|
+
if (paramNames.length > 0) {
|
|
133
|
+
const shape = paramNames.map((name) => `${name}: z.string()`).join(", ");
|
|
134
|
+
lines.push(`export const ${spec.name}ParamsSchema = z.object({ ${shape} });`);
|
|
135
|
+
}
|
|
136
|
+
if (spec.needsBody) {
|
|
137
|
+
lines.push(`// TODO: replace with the real shape of "${spec.name}"'s request body`);
|
|
138
|
+
lines.push(`export const ${spec.name}BodySchema = z.object({});`);
|
|
139
|
+
}
|
|
140
|
+
if (spec.needsQuery) {
|
|
141
|
+
lines.push(`// TODO: replace with the real shape of "${spec.name}"'s query params`);
|
|
142
|
+
lines.push(`export const ${spec.name}QuerySchema = z.object({});`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const schemaEntries: string[] = [];
|
|
146
|
+
if (paramNames.length > 0) schemaEntries.push(`params: ${spec.name}ParamsSchema`);
|
|
147
|
+
if (spec.needsBody) schemaEntries.push(`body: ${spec.name}BodySchema`);
|
|
148
|
+
if (spec.needsQuery) schemaEntries.push(`query: ${spec.name}QuerySchema`);
|
|
149
|
+
const schemaObject = schemaEntries.length > 0 ? `{ ${schemaEntries.join(", ")} }` : "{}";
|
|
150
|
+
|
|
151
|
+
const callArgs = serviceCallArgs(spec).join(", ");
|
|
152
|
+
const status = spec.verb === "post" ? ", 201" : "";
|
|
153
|
+
|
|
154
|
+
lines.push(
|
|
155
|
+
`export const ${spec.name} = defineRoute(${schemaObject}, async (c, { body, query, params }) => {`,
|
|
156
|
+
` const result = await service.${spec.name}(${callArgs});`,
|
|
157
|
+
` return c.json(result${status});`,
|
|
158
|
+
`});`
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return lines.join("\n");
|
|
98
162
|
}
|
|
99
|
-
|
|
163
|
+
|
|
164
|
+
export function routeMountLine(route: string, spec: MethodSpec): string {
|
|
165
|
+
return `${camelCase(route)}Routes.${spec.verb}("${spec.path}", controller.${spec.name});`;
|
|
100
166
|
}
|
|
101
167
|
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
168
|
+
export function serviceImports(route: string, spec: MethodSpec, language: Language): string[] {
|
|
169
|
+
if (language === "javascript") return [];
|
|
170
|
+
const imports: string[] = [`import type { z } from "zod";`];
|
|
171
|
+
const named: string[] = [];
|
|
172
|
+
if (paramNamesFromPath(spec.path).length > 0) named.push(`${spec.name}ParamsSchema`);
|
|
173
|
+
if (spec.needsBody) named.push(`${spec.name}BodySchema`);
|
|
174
|
+
if (spec.needsQuery) named.push(`${spec.name}QuerySchema`);
|
|
175
|
+
if (named.length > 0) imports.push(`import type { ${named.join(", ")} } from "./${route}.controller";`);
|
|
176
|
+
return imports;
|
|
105
177
|
}
|
|
106
178
|
|
|
107
|
-
export
|
|
179
|
+
export function renderServiceMethod(spec: MethodSpec, language: Language): string {
|
|
180
|
+
const ext = language === "javascript" ? "js" : "ts";
|
|
181
|
+
const argList = language === "javascript" ? serviceCallArgs(spec).join(", ") : serviceParamList(spec);
|
|
182
|
+
return `// TODO: replace with real data access (see src/config/db.${ext})
|
|
183
|
+
export async function ${spec.name}(${argList}) {
|
|
184
|
+
${defaultServiceReturn(spec)}
|
|
185
|
+
}`;
|
|
186
|
+
}
|