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/cli.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
// No shebang here on purpose — the published bin is the bundled
|
|
2
|
+
// dist/cli.js esbuild produces (see package.json's "build" script), which
|
|
3
|
+
// gets its own "#!/usr/bin/env node" shebang from esbuild's --banner flag.
|
|
4
|
+
// A shebang on *this* file would end up duplicated as the bundle's second
|
|
5
|
+
// line (esbuild carries a source shebang straight through), which Node
|
|
6
|
+
// rejects outright — a shebang only means anything as a file's literal
|
|
7
|
+
// first line. Local dev (`bun run src/cli.ts`, the "start" script) doesn't
|
|
8
|
+
// need a shebang either; `bun run` executes the given file directly, it
|
|
9
|
+
// doesn't rely on the OS reading a shebang the way `./cli.ts` would.
|
|
2
10
|
import * as p from "@clack/prompts";
|
|
3
11
|
import { runPrompts } from "./prompts";
|
|
4
12
|
import { generateProject } from "./generate";
|
|
@@ -6,11 +14,35 @@ import { addRoute } from "./add-route";
|
|
|
6
14
|
import { removeRoute } from "./remove-route";
|
|
7
15
|
import { listRoutes } from "./list-routes";
|
|
8
16
|
import { runDoctor } from "./doctor";
|
|
17
|
+
import { addMethod } from "./add-method";
|
|
18
|
+
import { removeMethod } from "./remove-method";
|
|
19
|
+
import { detectDbStatus } from "./db-status";
|
|
20
|
+
import { detectProject } from "./detect";
|
|
21
|
+
import {
|
|
22
|
+
detectMethodValidation,
|
|
23
|
+
formatEndpointsDashboard,
|
|
24
|
+
listEndpointsWithMethods,
|
|
25
|
+
type EndpointMethod,
|
|
26
|
+
type EndpointSummary,
|
|
27
|
+
} from "./route-methods";
|
|
28
|
+
import { HTTP_VERBS, paramNamesFromPath, pascalCase, type HttpVerb, type MethodSpec } from "./templates/route";
|
|
9
29
|
|
|
10
30
|
const ADD_ROUTE_SUBCOMMANDS = new Set(["add-route", "route"]);
|
|
11
31
|
const REMOVE_ROUTE_SUBCOMMANDS = new Set(["remove-route", "rm-route"]);
|
|
12
32
|
const LIST_ROUTES_SUBCOMMANDS = new Set(["list-routes", "routes"]);
|
|
13
33
|
const DOCTOR_SUBCOMMANDS = new Set(["doctor"]);
|
|
34
|
+
const ADD_METHOD_SUBCOMMANDS = new Set(["add-method", "method"]);
|
|
35
|
+
// project: a read-only overview (db status, endpoints, methods, doctor
|
|
36
|
+
// summary) — nothing here writes to disk. server-config: the interactive
|
|
37
|
+
// wrapper around add-route/remove-route/add-method/remove-method (plus
|
|
38
|
+
// modify, which neither standalone command offers) for managing endpoints
|
|
39
|
+
// without remembering individual subcommand names. Deliberately two
|
|
40
|
+
// separate commands rather than one combined dashboard — "just show me
|
|
41
|
+
// what's here" and "let me start changing things" are different intents,
|
|
42
|
+
// and collapsing them risks an accidental edit from someone who only
|
|
43
|
+
// wanted to look.
|
|
44
|
+
const PROJECT_SUBCOMMANDS = new Set(["project"]);
|
|
45
|
+
const SERVER_CONFIG_SUBCOMMANDS = new Set(["server-config"]);
|
|
14
46
|
|
|
15
47
|
async function promptForRouteName(introText: string): Promise<string> {
|
|
16
48
|
p.intro(introText);
|
|
@@ -35,7 +67,8 @@ async function runAddRoute(args: string[]): Promise<void> {
|
|
|
35
67
|
}
|
|
36
68
|
|
|
37
69
|
const { route, root, filesWritten } = await addRoute(name);
|
|
38
|
-
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files) in ${root}`);
|
|
70
|
+
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files, no methods yet) in ${root}`);
|
|
71
|
+
p.log.info("Run `bun run add-method` to give it a GET/POST/etc.");
|
|
39
72
|
}
|
|
40
73
|
|
|
41
74
|
async function runRemoveRoute(args: string[]): Promise<void> {
|
|
@@ -64,7 +97,8 @@ async function runListRoutes(): Promise<void> {
|
|
|
64
97
|
p.log.info(`No routes mounted in ${root}.`);
|
|
65
98
|
return;
|
|
66
99
|
}
|
|
67
|
-
|
|
100
|
+
const summaries = await listEndpointsWithMethods();
|
|
101
|
+
p.note(formatEndpointsDashboard(summaries), `Endpoints in ${root}`);
|
|
68
102
|
}
|
|
69
103
|
|
|
70
104
|
async function runDoctorCommand(): Promise<void> {
|
|
@@ -77,6 +111,469 @@ async function runDoctorCommand(): Promise<void> {
|
|
|
77
111
|
process.exitCode = 1;
|
|
78
112
|
}
|
|
79
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Read-only: db status, every endpoint and its methods, and a doctor
|
|
116
|
+
* summary, in one shot. No prompts, no writes — `bun run server-config` is
|
|
117
|
+
* where anything actually changes.
|
|
118
|
+
*/
|
|
119
|
+
async function runProjectOverview(): Promise<void> {
|
|
120
|
+
p.intro("create-backon project");
|
|
121
|
+
|
|
122
|
+
const { root } = await detectProject();
|
|
123
|
+
const dbStatus = await detectDbStatus();
|
|
124
|
+
const summaries = await listEndpointsWithMethods();
|
|
125
|
+
const { issues } = await runDoctor();
|
|
126
|
+
|
|
127
|
+
p.note(dbStatus.summary, "Database");
|
|
128
|
+
p.note(formatEndpointsDashboard(summaries), `Endpoints in ${root}`);
|
|
129
|
+
|
|
130
|
+
if (issues.length > 0) {
|
|
131
|
+
p.log.warn(`${issues.length} issue(s) between src/endpoints/ and index — run \`bun run doctor\` for details.`);
|
|
132
|
+
} else {
|
|
133
|
+
p.log.success("No drift between src/endpoints/ and index.");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
p.outro("Manage endpoints and methods with `bun run server-config`.");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function suggestMethodName(verb: HttpVerb, path: string): string {
|
|
140
|
+
const params = paramNamesFromPath(path);
|
|
141
|
+
if (verb === "get" && params.length === 0) return "list";
|
|
142
|
+
if (verb === "get") return `getBy${pascalCase(params[0] ?? "id")}`;
|
|
143
|
+
if (verb === "post") return "create";
|
|
144
|
+
if (verb === "put" || verb === "patch") return "update";
|
|
145
|
+
return "remove";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const IDENTIFIER_PATTERN = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Interactive, loops until the user's done: shows every endpoint and the
|
|
152
|
+
* methods it already has (colored by verb), then either picks one to add
|
|
153
|
+
* to or spins up a new blank one, then walks through verb/path/name/schema
|
|
154
|
+
* choices for a single method. Kept separate from add-route/remove-route on
|
|
155
|
+
* purpose — those stay simple whole-endpoint create/delete for scripted use.
|
|
156
|
+
*/
|
|
157
|
+
async function runAddMethod(): Promise<void> {
|
|
158
|
+
p.intro("create-backon add-method");
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line no-constant-condition
|
|
161
|
+
while (true) {
|
|
162
|
+
const { root, routes } = await listRoutes();
|
|
163
|
+
const summaries = await listEndpointsWithMethods();
|
|
164
|
+
p.note(formatEndpointsDashboard(summaries), `Endpoints in ${root}`);
|
|
165
|
+
|
|
166
|
+
const routeChoice = await p.select({
|
|
167
|
+
message: "Add a method to which endpoint?",
|
|
168
|
+
options: [
|
|
169
|
+
...routes.map((route) => ({ value: route, label: route })),
|
|
170
|
+
{ value: "__new__", label: "+ New blank endpoint" },
|
|
171
|
+
],
|
|
172
|
+
});
|
|
173
|
+
if (p.isCancel(routeChoice)) {
|
|
174
|
+
p.cancel("Cancelled.");
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let route = routeChoice;
|
|
179
|
+
if (route === "__new__") {
|
|
180
|
+
const name = await p.text({
|
|
181
|
+
message: "New endpoint name",
|
|
182
|
+
placeholder: "e.g. users",
|
|
183
|
+
validate: (value) => (!value ? "Required." : undefined),
|
|
184
|
+
});
|
|
185
|
+
if (p.isCancel(name)) {
|
|
186
|
+
p.cancel("Cancelled.");
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const added = await addRoute(name);
|
|
190
|
+
route = added.route;
|
|
191
|
+
p.log.success(`Added blank endpoint src/endpoints/${route}`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const verb = await p.select({
|
|
195
|
+
message: "HTTP method",
|
|
196
|
+
options: HTTP_VERBS.map((v) => ({ value: v, label: v.toUpperCase() })),
|
|
197
|
+
});
|
|
198
|
+
if (p.isCancel(verb)) {
|
|
199
|
+
p.cancel("Cancelled.");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const defaultPath = verb === "get" || verb === "post" ? "/" : "/:id";
|
|
204
|
+
const path = await p.text({
|
|
205
|
+
message: "Path (relative to this endpoint's mount)",
|
|
206
|
+
placeholder: defaultPath,
|
|
207
|
+
initialValue: defaultPath,
|
|
208
|
+
validate: (value) => (!value.startsWith("/") ? 'Must start with "/".' : undefined),
|
|
209
|
+
});
|
|
210
|
+
if (p.isCancel(path)) {
|
|
211
|
+
p.cancel("Cancelled.");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const existingMethods = summaries.find((s) => s.route === route)?.methods ?? [];
|
|
216
|
+
const defaultName = suggestMethodName(verb, path);
|
|
217
|
+
const name = await p.text({
|
|
218
|
+
message: "Name (controller export + service function)",
|
|
219
|
+
placeholder: defaultName,
|
|
220
|
+
initialValue: defaultName,
|
|
221
|
+
validate: (value) => {
|
|
222
|
+
if (!IDENTIFIER_PATTERN.test(value)) return "Must be a valid identifier.";
|
|
223
|
+
if (existingMethods.some((m) => m.name === value)) return `"${value}" is already used in this endpoint.`;
|
|
224
|
+
return undefined;
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
if (p.isCancel(name)) {
|
|
228
|
+
p.cancel("Cancelled.");
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let needsBody = false;
|
|
233
|
+
if (verb === "post" || verb === "put" || verb === "patch") {
|
|
234
|
+
const answer = await p.confirm({ message: "Validate a request body?", initialValue: true });
|
|
235
|
+
if (p.isCancel(answer)) {
|
|
236
|
+
p.cancel("Cancelled.");
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
needsBody = answer;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const needsQuery = await p.confirm({ message: "Validate query params?", initialValue: false });
|
|
243
|
+
if (p.isCancel(needsQuery)) {
|
|
244
|
+
p.cancel("Cancelled.");
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const spec: MethodSpec = { verb, path, name, needsBody, needsQuery };
|
|
249
|
+
try {
|
|
250
|
+
const { filesWritten } = await addMethod(route, spec);
|
|
251
|
+
p.log.success(`Added ${verb.toUpperCase()} ${path} → ${name} in src/endpoints/${route} (${filesWritten.length} files)`);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
p.log.error(err instanceof Error ? err.message : String(err));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const again = await p.confirm({ message: "Add another method?", initialValue: true });
|
|
257
|
+
if (p.isCancel(again) || !again) break;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
p.outro("Done.");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// ---------------------------------------------------------------------------
|
|
264
|
+
// server-config: the interactive endpoint/method manager. Each of these is
|
|
265
|
+
// a single, self-contained step (no intro/outro of its own, no "add
|
|
266
|
+
// another?" loop of its own) — runServerConfig()'s own while loop is what
|
|
267
|
+
// repeats, and it re-reads the endpoint list fresh every iteration so any
|
|
268
|
+
// step's changes are immediately visible to the next one.
|
|
269
|
+
// ---------------------------------------------------------------------------
|
|
270
|
+
|
|
271
|
+
async function serverConfigAddEndpoint(): Promise<void> {
|
|
272
|
+
const name = await p.text({
|
|
273
|
+
message: "New endpoint name",
|
|
274
|
+
placeholder: "e.g. users",
|
|
275
|
+
validate: (value) => (!value ? "Required." : undefined),
|
|
276
|
+
});
|
|
277
|
+
if (p.isCancel(name)) {
|
|
278
|
+
p.cancel("Cancelled.");
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const { route, filesWritten } = await addRoute(name);
|
|
282
|
+
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files, no methods yet).`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function serverConfigRemoveEndpoint(summaries: EndpointSummary[]): Promise<void> {
|
|
286
|
+
if (summaries.length === 0) {
|
|
287
|
+
p.log.info("No endpoints yet.");
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const route = await p.select({
|
|
291
|
+
message: "Remove which endpoint?",
|
|
292
|
+
options: summaries.map((s) => ({ value: s.route, label: s.route })),
|
|
293
|
+
});
|
|
294
|
+
if (p.isCancel(route)) {
|
|
295
|
+
p.cancel("Cancelled.");
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const confirmed = await p.confirm({
|
|
299
|
+
message: `Remove src/endpoints/${route} and unmount it? This deletes the folder.`,
|
|
300
|
+
initialValue: false,
|
|
301
|
+
});
|
|
302
|
+
if (p.isCancel(confirmed) || !confirmed) {
|
|
303
|
+
p.cancel("Cancelled.");
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const { removedDir } = await removeRoute(route);
|
|
307
|
+
p.log.success(`Removed ${removedDir}.`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async function serverConfigAddMethod(summaries: EndpointSummary[]): Promise<void> {
|
|
311
|
+
const routeChoice = await p.select({
|
|
312
|
+
message: "Add a method to which endpoint?",
|
|
313
|
+
options: [
|
|
314
|
+
...summaries.map((s) => ({ value: s.route, label: s.route })),
|
|
315
|
+
{ value: "__new__", label: "+ New blank endpoint" },
|
|
316
|
+
],
|
|
317
|
+
});
|
|
318
|
+
if (p.isCancel(routeChoice)) {
|
|
319
|
+
p.cancel("Cancelled.");
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
let route = routeChoice;
|
|
324
|
+
let existingMethods = summaries.find((s) => s.route === route)?.methods ?? [];
|
|
325
|
+
if (route === "__new__") {
|
|
326
|
+
const name = await p.text({
|
|
327
|
+
message: "New endpoint name",
|
|
328
|
+
placeholder: "e.g. users",
|
|
329
|
+
validate: (value) => (!value ? "Required." : undefined),
|
|
330
|
+
});
|
|
331
|
+
if (p.isCancel(name)) {
|
|
332
|
+
p.cancel("Cancelled.");
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const added = await addRoute(name);
|
|
336
|
+
route = added.route;
|
|
337
|
+
existingMethods = [];
|
|
338
|
+
p.log.success(`Added blank endpoint src/endpoints/${route}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const verb = await p.select({
|
|
342
|
+
message: "HTTP method",
|
|
343
|
+
options: HTTP_VERBS.map((v) => ({ value: v, label: v.toUpperCase() })),
|
|
344
|
+
});
|
|
345
|
+
if (p.isCancel(verb)) {
|
|
346
|
+
p.cancel("Cancelled.");
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const defaultPath = verb === "get" || verb === "post" ? "/" : "/:id";
|
|
351
|
+
const path = await p.text({
|
|
352
|
+
message: "Path (relative to this endpoint's mount)",
|
|
353
|
+
placeholder: defaultPath,
|
|
354
|
+
initialValue: defaultPath,
|
|
355
|
+
validate: (value) => (!value.startsWith("/") ? 'Must start with "/".' : undefined),
|
|
356
|
+
});
|
|
357
|
+
if (p.isCancel(path)) {
|
|
358
|
+
p.cancel("Cancelled.");
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const defaultName = suggestMethodName(verb, path);
|
|
363
|
+
const name = await p.text({
|
|
364
|
+
message: "Name (controller export + service function)",
|
|
365
|
+
placeholder: defaultName,
|
|
366
|
+
initialValue: defaultName,
|
|
367
|
+
validate: (value) => {
|
|
368
|
+
if (!IDENTIFIER_PATTERN.test(value)) return "Must be a valid identifier.";
|
|
369
|
+
if (existingMethods.some((m) => m.name === value)) return `"${value}" is already used in this endpoint.`;
|
|
370
|
+
return undefined;
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
if (p.isCancel(name)) {
|
|
374
|
+
p.cancel("Cancelled.");
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let needsBody = false;
|
|
379
|
+
if (verb === "post" || verb === "put" || verb === "patch") {
|
|
380
|
+
const answer = await p.confirm({ message: "Validate a request body?", initialValue: true });
|
|
381
|
+
if (p.isCancel(answer)) {
|
|
382
|
+
p.cancel("Cancelled.");
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
needsBody = answer;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const needsQuery = await p.confirm({ message: "Validate query params?", initialValue: false });
|
|
389
|
+
if (p.isCancel(needsQuery)) {
|
|
390
|
+
p.cancel("Cancelled.");
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const spec: MethodSpec = { verb, path, name, needsBody, needsQuery };
|
|
395
|
+
const { filesWritten } = await addMethod(route, spec);
|
|
396
|
+
p.log.success(`Added ${verb.toUpperCase()} ${path} → ${name} in src/endpoints/${route} (${filesWritten.length} files)`);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Endpoints with at least one method — remove/modify both operate on an existing method, so an endpoint with none isn't a valid choice for either. */
|
|
400
|
+
async function selectEndpointWithMethods(summaries: EndpointSummary[], message: string): Promise<EndpointSummary | null> {
|
|
401
|
+
const withMethods = summaries.filter((s) => s.methods.length > 0);
|
|
402
|
+
if (withMethods.length === 0) {
|
|
403
|
+
p.log.info("No methods yet — add one first.");
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
const route = await p.select({
|
|
407
|
+
message,
|
|
408
|
+
options: withMethods.map((s) => ({ value: s.route, label: s.route })),
|
|
409
|
+
});
|
|
410
|
+
if (p.isCancel(route)) {
|
|
411
|
+
p.cancel("Cancelled.");
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
return withMethods.find((s) => s.route === route) ?? null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async function selectMethod(endpoint: EndpointSummary, message: string): Promise<EndpointMethod | null> {
|
|
418
|
+
const choice = await p.select({
|
|
419
|
+
message,
|
|
420
|
+
options: endpoint.methods.map((m) => ({
|
|
421
|
+
value: `${m.verb}:${m.path}`,
|
|
422
|
+
label: `${m.verb.toUpperCase()} ${m.path} → ${m.name}`,
|
|
423
|
+
})),
|
|
424
|
+
});
|
|
425
|
+
if (p.isCancel(choice)) {
|
|
426
|
+
p.cancel("Cancelled.");
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
// Split on the FIRST colon only — a path itself can contain no colons
|
|
430
|
+
// outside of :param segments, but this keeps the parse correct even if
|
|
431
|
+
// that ever changes, since verb never contains one.
|
|
432
|
+
const [verb, path] = choice.split(/:(.*)/s) as [HttpVerb, string];
|
|
433
|
+
return endpoint.methods.find((m) => m.verb === verb && m.path === path) ?? null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async function serverConfigRemoveMethod(summaries: EndpointSummary[]): Promise<void> {
|
|
437
|
+
const endpoint = await selectEndpointWithMethods(summaries, "Remove a method from which endpoint?");
|
|
438
|
+
if (!endpoint) return;
|
|
439
|
+
const method = await selectMethod(endpoint, "Remove which method?");
|
|
440
|
+
if (!method) return;
|
|
441
|
+
|
|
442
|
+
const confirmed = await p.confirm({
|
|
443
|
+
message: `Remove ${method.verb.toUpperCase()} ${method.path} (${method.name}) from ${endpoint.route}?`,
|
|
444
|
+
initialValue: false,
|
|
445
|
+
});
|
|
446
|
+
if (p.isCancel(confirmed) || !confirmed) {
|
|
447
|
+
p.cancel("Cancelled.");
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const { removed } = await removeMethod(endpoint.route, method.verb, method.path);
|
|
452
|
+
p.log.success(`Removed ${removed.verb.toUpperCase()} ${removed.path} (${removed.name}) from src/endpoints/${endpoint.route}.`);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Edits a method's verb/path/name/validation in place: a checked removeMethod() + addMethod() pair, not a separate codegen path. */
|
|
456
|
+
async function serverConfigModifyMethod(summaries: EndpointSummary[]): Promise<void> {
|
|
457
|
+
const endpoint = await selectEndpointWithMethods(summaries, "Modify a method on which endpoint?");
|
|
458
|
+
if (!endpoint) return;
|
|
459
|
+
const old = await selectMethod(endpoint, "Modify which method?");
|
|
460
|
+
if (!old) return;
|
|
461
|
+
|
|
462
|
+
const { needsBody: oldNeedsBody, needsQuery: oldNeedsQuery } = await detectMethodValidation(endpoint.route, old.name);
|
|
463
|
+
const others = endpoint.methods.filter((m) => m !== old);
|
|
464
|
+
|
|
465
|
+
const verb = await p.select({
|
|
466
|
+
message: "HTTP method",
|
|
467
|
+
initialValue: old.verb,
|
|
468
|
+
options: HTTP_VERBS.map((v) => ({ value: v, label: v.toUpperCase() })),
|
|
469
|
+
});
|
|
470
|
+
if (p.isCancel(verb)) {
|
|
471
|
+
p.cancel("Cancelled.");
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const path = await p.text({
|
|
476
|
+
message: "Path (relative to this endpoint's mount)",
|
|
477
|
+
initialValue: old.path,
|
|
478
|
+
validate: (value) => (!value.startsWith("/") ? 'Must start with "/".' : undefined),
|
|
479
|
+
});
|
|
480
|
+
if (p.isCancel(path)) {
|
|
481
|
+
p.cancel("Cancelled.");
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const name = await p.text({
|
|
486
|
+
message: "Name (controller export + service function)",
|
|
487
|
+
initialValue: old.name,
|
|
488
|
+
validate: (value) => {
|
|
489
|
+
if (!IDENTIFIER_PATTERN.test(value)) return "Must be a valid identifier.";
|
|
490
|
+
if (others.some((m) => m.name === value)) return `"${value}" is already used in this endpoint.`;
|
|
491
|
+
return undefined;
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
if (p.isCancel(name)) {
|
|
495
|
+
p.cancel("Cancelled.");
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Checked before anything on disk changes — removeMethod() below is only
|
|
500
|
+
// safe to run once we know addMethod() won't then fail on a collision.
|
|
501
|
+
if (others.some((m) => m.verb === verb && m.path === path)) {
|
|
502
|
+
p.log.error(`${verb.toUpperCase()} ${path} is already used by another method on this endpoint.`);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
let needsBody = false;
|
|
507
|
+
if (verb === "post" || verb === "put" || verb === "patch") {
|
|
508
|
+
const answer = await p.confirm({ message: "Validate a request body?", initialValue: oldNeedsBody });
|
|
509
|
+
if (p.isCancel(answer)) {
|
|
510
|
+
p.cancel("Cancelled.");
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
needsBody = answer;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const needsQuery = await p.confirm({ message: "Validate query params?", initialValue: oldNeedsQuery });
|
|
517
|
+
if (p.isCancel(needsQuery)) {
|
|
518
|
+
p.cancel("Cancelled.");
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
await removeMethod(endpoint.route, old.verb, old.path);
|
|
523
|
+
const { filesWritten } = await addMethod(endpoint.route, { verb, path, name, needsBody, needsQuery });
|
|
524
|
+
p.log.success(
|
|
525
|
+
`Updated src/endpoints/${endpoint.route}: ${old.verb.toUpperCase()} ${old.path} → ${verb.toUpperCase()} ${path} (${name}), ${filesWritten.length} files touched.`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async function runServerConfig(): Promise<void> {
|
|
530
|
+
p.intro("create-backon server-config");
|
|
531
|
+
|
|
532
|
+
// eslint-disable-next-line no-constant-condition
|
|
533
|
+
while (true) {
|
|
534
|
+
const { root } = await detectProject();
|
|
535
|
+
const summaries = await listEndpointsWithMethods();
|
|
536
|
+
p.note(formatEndpointsDashboard(summaries), `Endpoints in ${root}`);
|
|
537
|
+
|
|
538
|
+
const action = await p.select({
|
|
539
|
+
message: "What do you want to do?",
|
|
540
|
+
options: [
|
|
541
|
+
{ value: "add-endpoint", label: "Add a new endpoint" },
|
|
542
|
+
{ value: "remove-endpoint", label: "Remove an endpoint" },
|
|
543
|
+
{ value: "add-method", label: "Add a method" },
|
|
544
|
+
{ value: "remove-method", label: "Remove a method" },
|
|
545
|
+
{ value: "modify-method", label: "Modify a method" },
|
|
546
|
+
{ value: "exit", label: "Exit" },
|
|
547
|
+
],
|
|
548
|
+
});
|
|
549
|
+
if (p.isCancel(action) || action === "exit") break;
|
|
550
|
+
|
|
551
|
+
try {
|
|
552
|
+
switch (action) {
|
|
553
|
+
case "add-endpoint":
|
|
554
|
+
await serverConfigAddEndpoint();
|
|
555
|
+
break;
|
|
556
|
+
case "remove-endpoint":
|
|
557
|
+
await serverConfigRemoveEndpoint(summaries);
|
|
558
|
+
break;
|
|
559
|
+
case "add-method":
|
|
560
|
+
await serverConfigAddMethod(summaries);
|
|
561
|
+
break;
|
|
562
|
+
case "remove-method":
|
|
563
|
+
await serverConfigRemoveMethod(summaries);
|
|
564
|
+
break;
|
|
565
|
+
case "modify-method":
|
|
566
|
+
await serverConfigModifyMethod(summaries);
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
} catch (err) {
|
|
570
|
+
p.log.error(err instanceof Error ? err.message : String(err));
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
p.outro("Done.");
|
|
575
|
+
}
|
|
576
|
+
|
|
80
577
|
async function runScaffold(): Promise<void> {
|
|
81
578
|
const answers = await runPrompts();
|
|
82
579
|
const { root, filesWritten } = await generateProject(answers);
|
|
@@ -106,6 +603,18 @@ async function main() {
|
|
|
106
603
|
await runDoctorCommand();
|
|
107
604
|
return;
|
|
108
605
|
}
|
|
606
|
+
if (subcommand && ADD_METHOD_SUBCOMMANDS.has(subcommand)) {
|
|
607
|
+
await runAddMethod();
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (subcommand && PROJECT_SUBCOMMANDS.has(subcommand)) {
|
|
611
|
+
await runProjectOverview();
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (subcommand && SERVER_CONFIG_SUBCOMMANDS.has(subcommand)) {
|
|
615
|
+
await runServerConfig();
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
109
618
|
|
|
110
619
|
await runScaffold();
|
|
111
620
|
}
|
package/src/colors.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { HttpVerb } from "./templates/route";
|
|
2
|
+
|
|
3
|
+
// Standard REST-tooling convention (Scalar, Postman, Insomnia all agree on
|
|
4
|
+
// roughly this mapping) — GET blue/cyan, POST green, PUT/PATCH amber,
|
|
5
|
+
// DELETE red. Plain ANSI codes rather than a new dependency: create-backon
|
|
6
|
+
// only depends on @clack/prompts today, and this is five lines.
|
|
7
|
+
const VERB_CODES: Record<HttpVerb, string> = {
|
|
8
|
+
get: "36",
|
|
9
|
+
post: "32",
|
|
10
|
+
put: "33",
|
|
11
|
+
patch: "35",
|
|
12
|
+
delete: "31",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const RESET = "\x1b[0m";
|
|
16
|
+
|
|
17
|
+
export function colorVerb(verb: HttpVerb): string {
|
|
18
|
+
const label = verb.toUpperCase().padEnd(6);
|
|
19
|
+
const code = VERB_CODES[verb];
|
|
20
|
+
return code ? `\x1b[${code}m${label}${RESET}` : label;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function dim(text: string): string {
|
|
24
|
+
return `\x1b[2m${text}${RESET}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Green — a status line that's actually fine (a DB driver with everything it needs, a clean doctor run). */
|
|
28
|
+
export function ok(text: string): string {
|
|
29
|
+
return `\x1b[32m${text}${RESET}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Amber — a status line that needs attention, but isn't a hard failure. */
|
|
33
|
+
export function warn(text: string): string {
|
|
34
|
+
return `\x1b[33m${text}${RESET}`;
|
|
35
|
+
}
|