create-backon 0.14.1 → 0.15.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/dist/cli.js +626 -418
- package/package.json +5 -2
- package/src/cli.ts +323 -340
- package/src/prompts.ts +10 -1
- package/src/schema-edit.ts +25 -12
- package/src/templates/base.ts +14 -7
- package/src/templates/schema.ts +5 -2
- package/src/tui.ts +410 -73
package/src/cli.ts
CHANGED
|
@@ -49,7 +49,22 @@ import {
|
|
|
49
49
|
type OnDeleteAction,
|
|
50
50
|
type TableSpec,
|
|
51
51
|
} from "./templates/schema";
|
|
52
|
-
import { runServerConfigTui } from "./tui";
|
|
52
|
+
import { runServerConfigTui, type TuiPromptUI } from "./tui";
|
|
53
|
+
|
|
54
|
+
const clackUI: TuiPromptUI = {
|
|
55
|
+
text: (opts) =>
|
|
56
|
+
p.text({
|
|
57
|
+
...opts,
|
|
58
|
+
validate: opts.validate ? (value) => opts.validate!(value ?? "") : undefined,
|
|
59
|
+
}),
|
|
60
|
+
select: <T extends string>(opts: {
|
|
61
|
+
message: string;
|
|
62
|
+
options: { value: T; label: string; hint?: string }[];
|
|
63
|
+
initialValue?: T;
|
|
64
|
+
}) => p.select(opts as Parameters<typeof p.select>[0]) as Promise<T | symbol>,
|
|
65
|
+
confirm: (opts) => p.confirm(opts),
|
|
66
|
+
isCancel: (value): value is symbol => p.isCancel(value),
|
|
67
|
+
};
|
|
53
68
|
|
|
54
69
|
const ADD_ROUTE_SUBCOMMANDS = new Set(["add-route", "route"]);
|
|
55
70
|
const REMOVE_ROUTE_SUBCOMMANDS = new Set(["remove-route", "rm-route"]);
|
|
@@ -187,14 +202,23 @@ async function runProjectOverview(): Promise<void> {
|
|
|
187
202
|
// rather than silently no-opping.
|
|
188
203
|
// ---------------------------------------------------------------------------
|
|
189
204
|
|
|
190
|
-
async function
|
|
205
|
+
async function requireSchemaSupport(): Promise<void> {
|
|
191
206
|
const status = await detectDbStatus();
|
|
192
|
-
if (status.driver === "neon" || status.driver === "postgres") return
|
|
193
|
-
|
|
207
|
+
if (status.driver === "neon" || status.driver === "postgres") return;
|
|
208
|
+
throw new Error(
|
|
194
209
|
`Schema management needs a "neon" or "postgres" db driver (src/config/db reports "${status.driver}") — ` +
|
|
195
210
|
`SQLite and "none" use a different schema dialect (or none at all) and aren't supported here yet.`
|
|
196
211
|
);
|
|
197
|
-
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function ensureSchemaSupport(): Promise<boolean> {
|
|
215
|
+
try {
|
|
216
|
+
await requireSchemaSupport();
|
|
217
|
+
return true;
|
|
218
|
+
} catch (err) {
|
|
219
|
+
p.log.error(err instanceof Error ? err.message : String(err));
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
198
222
|
}
|
|
199
223
|
|
|
200
224
|
async function readSchemaTables(): Promise<ParsedTable[]> {
|
|
@@ -208,20 +232,20 @@ async function readSchemaTables(): Promise<ParsedTable[]> {
|
|
|
208
232
|
}
|
|
209
233
|
|
|
210
234
|
/** Type-specific default prompt — returns pre-rendered modifier fragment, "" for none, or null on cancel. */
|
|
211
|
-
async function promptDefaultExpr(type: ColumnType): Promise<string | null> {
|
|
235
|
+
async function promptDefaultExpr(type: ColumnType, ui: TuiPromptUI = clackUI): Promise<string | null> {
|
|
212
236
|
if (type === "serial") return "";
|
|
213
237
|
if (type === "timestamp" || type === "date") {
|
|
214
|
-
const v = await
|
|
215
|
-
if (
|
|
238
|
+
const v = await ui.confirm({ message: "Default to now()?", initialValue: false });
|
|
239
|
+
if (ui.isCancel(v)) return null;
|
|
216
240
|
return v ? ".defaultNow()" : "";
|
|
217
241
|
}
|
|
218
242
|
if (type === "uuid") {
|
|
219
|
-
const v = await
|
|
220
|
-
if (
|
|
243
|
+
const v = await ui.confirm({ message: "Default to a random UUID?", initialValue: false });
|
|
244
|
+
if (ui.isCancel(v)) return null;
|
|
221
245
|
return v ? ".defaultRandom()" : "";
|
|
222
246
|
}
|
|
223
247
|
if (type === "boolean") {
|
|
224
|
-
const v = await
|
|
248
|
+
const v = await ui.select({
|
|
225
249
|
message: "Default value",
|
|
226
250
|
options: [
|
|
227
251
|
{ value: "none", label: "None" },
|
|
@@ -230,11 +254,11 @@ async function promptDefaultExpr(type: ColumnType): Promise<string | null> {
|
|
|
230
254
|
],
|
|
231
255
|
initialValue: "none",
|
|
232
256
|
});
|
|
233
|
-
if (
|
|
257
|
+
if (ui.isCancel(v)) return null;
|
|
234
258
|
return v === "none" ? "" : `.default(${v})`;
|
|
235
259
|
}
|
|
236
260
|
if (type === "jsonb") {
|
|
237
|
-
const v = await
|
|
261
|
+
const v = await ui.select({
|
|
238
262
|
message: "Default JSON value",
|
|
239
263
|
options: [
|
|
240
264
|
{ value: "none", label: "None" },
|
|
@@ -243,25 +267,21 @@ async function promptDefaultExpr(type: ColumnType): Promise<string | null> {
|
|
|
243
267
|
],
|
|
244
268
|
initialValue: "none",
|
|
245
269
|
});
|
|
246
|
-
if (
|
|
270
|
+
if (ui.isCancel(v)) return null;
|
|
247
271
|
return v === "none" ? "" : `.default(${v})`;
|
|
248
272
|
}
|
|
249
273
|
// text / varchar / integer / numeric / real
|
|
250
274
|
const isNumber = type === "integer" || type === "real";
|
|
251
|
-
const v = await
|
|
275
|
+
const v = await ui.text({
|
|
252
276
|
message: "Default value (blank for none)",
|
|
253
277
|
placeholder: isNumber ? "e.g. 0" : type === "numeric" ? "e.g. 0.00" : "e.g. pending",
|
|
278
|
+
validate: isNumber
|
|
279
|
+
? (value) => (value && Number.isNaN(Number(value)) ? `"${value}" isn't a valid number.` : undefined)
|
|
280
|
+
: undefined,
|
|
254
281
|
});
|
|
255
|
-
if (
|
|
282
|
+
if (ui.isCancel(v)) return null;
|
|
256
283
|
if (!v) return "";
|
|
257
|
-
if (isNumber) {
|
|
258
|
-
const n = Number(v);
|
|
259
|
-
if (Number.isNaN(n)) {
|
|
260
|
-
p.log.warn(`"${v}" isn't a number — skipping the default.`);
|
|
261
|
-
return "";
|
|
262
|
-
}
|
|
263
|
-
return `.default(${n})`;
|
|
264
|
-
}
|
|
284
|
+
if (isNumber) return `.default(${Number(v)})`;
|
|
265
285
|
return `.default(${JSON.stringify(v)})`;
|
|
266
286
|
}
|
|
267
287
|
|
|
@@ -269,23 +289,29 @@ async function promptDefaultExpr(type: ColumnType): Promise<string | null> {
|
|
|
269
289
|
async function promptSingleColumn(
|
|
270
290
|
name: string,
|
|
271
291
|
existingTables: ParsedTable[],
|
|
272
|
-
defaultRequired = true
|
|
292
|
+
defaultRequired = true,
|
|
293
|
+
ui: TuiPromptUI = clackUI
|
|
273
294
|
): Promise<ColumnSpec | null> {
|
|
295
|
+
const refTables = existingTables.filter((t) => t.columns.some((c) => c.key === "id"));
|
|
274
296
|
let references: string | undefined;
|
|
297
|
+
let referenceType: "integer" | "uuid" | undefined;
|
|
275
298
|
let onDelete: OnDeleteAction | undefined;
|
|
276
|
-
if (
|
|
277
|
-
const refChoice = await
|
|
299
|
+
if (refTables.length > 0) {
|
|
300
|
+
const refChoice = await ui.select({
|
|
278
301
|
message: `Does "${name}" reference another table's id?`,
|
|
279
302
|
options: [
|
|
280
303
|
{ value: "__none__", label: "No" },
|
|
281
|
-
...
|
|
304
|
+
...refTables.map((t) => ({ value: t.identifier, label: t.identifier })),
|
|
282
305
|
],
|
|
283
306
|
initialValue: "__none__",
|
|
284
307
|
});
|
|
285
|
-
if (
|
|
308
|
+
if (ui.isCancel(refChoice)) return null;
|
|
286
309
|
if (refChoice !== "__none__") {
|
|
287
310
|
references = refChoice;
|
|
288
|
-
const
|
|
311
|
+
const targetTable = refTables.find((t) => t.identifier === refChoice);
|
|
312
|
+
const idCol = targetTable?.columns.find((c) => c.key === "id");
|
|
313
|
+
referenceType = idCol?.type === "uuid" ? "uuid" : "integer";
|
|
314
|
+
const deleteChoice = await ui.select({
|
|
289
315
|
message: "On referenced row delete",
|
|
290
316
|
options: [
|
|
291
317
|
{ value: "__none__", label: "Default (no action)" },
|
|
@@ -293,24 +319,24 @@ async function promptSingleColumn(
|
|
|
293
319
|
],
|
|
294
320
|
initialValue: "cascade",
|
|
295
321
|
});
|
|
296
|
-
if (
|
|
322
|
+
if (ui.isCancel(deleteChoice)) return null;
|
|
297
323
|
if (deleteChoice !== "__none__") onDelete = deleteChoice as OnDeleteAction;
|
|
298
324
|
}
|
|
299
325
|
}
|
|
300
326
|
|
|
301
|
-
let type: ColumnType = "integer";
|
|
327
|
+
let type: ColumnType = referenceType ?? "integer";
|
|
302
328
|
let varcharLength: number | undefined;
|
|
303
329
|
let defaultExpr = "";
|
|
304
330
|
if (!references) {
|
|
305
|
-
const typeChoice = await
|
|
331
|
+
const typeChoice = await ui.select({
|
|
306
332
|
message: "Column type",
|
|
307
333
|
options: COLUMN_TYPES.map((t) => ({ value: t, label: t })),
|
|
308
334
|
});
|
|
309
|
-
if (
|
|
335
|
+
if (ui.isCancel(typeChoice)) return null;
|
|
310
336
|
type = typeChoice;
|
|
311
337
|
|
|
312
338
|
if (type === "varchar") {
|
|
313
|
-
const lenInput = await
|
|
339
|
+
const lenInput = await ui.text({
|
|
314
340
|
message: "Max varchar length",
|
|
315
341
|
initialValue: "255",
|
|
316
342
|
validate: (value) => {
|
|
@@ -318,43 +344,48 @@ async function promptSingleColumn(
|
|
|
318
344
|
return !value || !Number.isInteger(n) || n <= 0 ? "Enter a positive integer." : undefined;
|
|
319
345
|
},
|
|
320
346
|
});
|
|
321
|
-
if (
|
|
347
|
+
if (ui.isCancel(lenInput)) return null;
|
|
322
348
|
varcharLength = Number(lenInput);
|
|
323
349
|
}
|
|
324
350
|
|
|
325
|
-
const expr = await promptDefaultExpr(type);
|
|
351
|
+
const expr = await promptDefaultExpr(type, ui);
|
|
326
352
|
if (expr === null) return null;
|
|
327
353
|
defaultExpr = expr;
|
|
328
354
|
}
|
|
329
355
|
|
|
330
|
-
const notNull = await
|
|
331
|
-
if (
|
|
332
|
-
const unique = await
|
|
333
|
-
if (
|
|
356
|
+
const notNull = await ui.confirm({ message: "Required (NOT NULL)?", initialValue: defaultRequired });
|
|
357
|
+
if (ui.isCancel(notNull)) return null;
|
|
358
|
+
const unique = await ui.confirm({ message: "Unique?", initialValue: false });
|
|
359
|
+
if (ui.isCancel(unique)) return null;
|
|
334
360
|
|
|
335
|
-
return { name, type, notNull, unique, defaultExpr, varcharLength, references, onDelete };
|
|
361
|
+
return { name, type, notNull, unique, defaultExpr, varcharLength, references, referenceType, onDelete };
|
|
336
362
|
}
|
|
337
363
|
|
|
338
364
|
/** Full column-collection loop for one table — stops on a blank column name, propagates a cancel as `null`. */
|
|
339
|
-
async function promptForColumns(
|
|
365
|
+
async function promptForColumns(
|
|
366
|
+
existingTables: ParsedTable[],
|
|
367
|
+
existingKeys: string[] = [],
|
|
368
|
+
ui: TuiPromptUI = clackUI
|
|
369
|
+
): Promise<ColumnSpec[] | null> {
|
|
340
370
|
const columns: ColumnSpec[] = [];
|
|
341
|
-
p.log.step("Add columns one at a time. Leave the name blank to finish.");
|
|
342
371
|
// eslint-disable-next-line no-constant-condition
|
|
343
372
|
while (true) {
|
|
344
|
-
const nameProbe = await
|
|
345
|
-
message: columns.length === 0 ? "
|
|
373
|
+
const nameProbe = await ui.text({
|
|
374
|
+
message: columns.length === 0 ? "First column name (blank to finish)" : "Another column? (blank to finish)",
|
|
346
375
|
placeholder: columns.length === 0 ? "e.g. email" : "",
|
|
376
|
+
validate: (value) => {
|
|
377
|
+
if (!value) return undefined;
|
|
378
|
+
const key = columnIdentifier(value);
|
|
379
|
+
if (existingKeys.includes(key) || columns.some((c) => columnIdentifier(c.name) === key)) {
|
|
380
|
+
return `"${key}" is already a column on this table.`;
|
|
381
|
+
}
|
|
382
|
+
return undefined;
|
|
383
|
+
},
|
|
347
384
|
});
|
|
348
|
-
if (
|
|
385
|
+
if (ui.isCancel(nameProbe)) return null;
|
|
349
386
|
if (!nameProbe) break;
|
|
350
387
|
|
|
351
|
-
const
|
|
352
|
-
if (existingKeys.includes(key) || columns.some((c) => columnIdentifier(c.name) === key)) {
|
|
353
|
-
p.log.error(`"${key}" is already a column on this table.`);
|
|
354
|
-
continue;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const spec = await promptSingleColumn(nameProbe, existingTables);
|
|
388
|
+
const spec = await promptSingleColumn(nameProbe, existingTables, true, ui);
|
|
358
389
|
if (spec === null) return null;
|
|
359
390
|
columns.push(spec);
|
|
360
391
|
}
|
|
@@ -363,16 +394,19 @@ async function promptForColumns(existingTables: ParsedTable[], existingKeys: str
|
|
|
363
394
|
}
|
|
364
395
|
|
|
365
396
|
/** Full table-collection flow: name, id primary key, timestamps preset, and columns. */
|
|
366
|
-
async function collectTableSpec(
|
|
397
|
+
async function collectTableSpec(
|
|
398
|
+
existingTables: ParsedTable[],
|
|
399
|
+
forcedName?: string,
|
|
400
|
+
ui: TuiPromptUI = clackUI
|
|
401
|
+
): Promise<TableSpec | null> {
|
|
367
402
|
let name = forcedName;
|
|
368
403
|
if (name) {
|
|
369
404
|
const id = tableIdentifier(name);
|
|
370
405
|
if (existingTables.some((t) => t.identifier === id)) {
|
|
371
|
-
|
|
372
|
-
return null;
|
|
406
|
+
throw new Error(`"${id}" already exists.`);
|
|
373
407
|
}
|
|
374
408
|
} else {
|
|
375
|
-
const answer = await
|
|
409
|
+
const answer = await ui.text({
|
|
376
410
|
message: "New table name",
|
|
377
411
|
placeholder: "e.g. users",
|
|
378
412
|
validate: (value) => {
|
|
@@ -383,49 +417,52 @@ async function collectTableSpec(existingTables: ParsedTable[], forcedName?: stri
|
|
|
383
417
|
return undefined;
|
|
384
418
|
},
|
|
385
419
|
});
|
|
386
|
-
if (
|
|
420
|
+
if (ui.isCancel(answer)) return null;
|
|
387
421
|
name = answer;
|
|
388
422
|
}
|
|
389
423
|
|
|
390
|
-
const includeId = await
|
|
424
|
+
const includeId = await ui.confirm({
|
|
391
425
|
message: "Add an auto-increment id primary key column?",
|
|
392
426
|
initialValue: true,
|
|
393
427
|
});
|
|
394
|
-
if (
|
|
428
|
+
if (ui.isCancel(includeId)) return null;
|
|
395
429
|
|
|
396
|
-
const includeTimestamps = await
|
|
430
|
+
const includeTimestamps = await ui.confirm({
|
|
397
431
|
message: "Add createdAt / updatedAt timestamp columns?",
|
|
398
432
|
initialValue: true,
|
|
399
433
|
});
|
|
400
|
-
if (
|
|
434
|
+
if (ui.isCancel(includeTimestamps)) return null;
|
|
401
435
|
|
|
402
|
-
const columns = await promptForColumns(existingTables, includeId ? ["id"] : []);
|
|
436
|
+
const columns = await promptForColumns(existingTables, includeId ? ["id"] : [], ui);
|
|
403
437
|
if (columns === null) return null;
|
|
404
438
|
|
|
405
439
|
return { name, includeId, includeTimestamps, columns };
|
|
406
440
|
}
|
|
407
441
|
|
|
408
442
|
/** Interactive wizard for a many-to-many junction table pairing two existing tables. */
|
|
409
|
-
async function collectJunctionTableSpec(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
443
|
+
async function collectJunctionTableSpec(
|
|
444
|
+
existingTables: ParsedTable[],
|
|
445
|
+
ui: TuiPromptUI = clackUI
|
|
446
|
+
): Promise<TableSpec | null> {
|
|
447
|
+
const refTables = existingTables.filter((t) => t.columns.some((c) => c.key === "id"));
|
|
448
|
+
if (refTables.length < 2) {
|
|
449
|
+
throw new Error("Need at least 2 existing tables with an id column to create a junction table.");
|
|
413
450
|
}
|
|
414
451
|
|
|
415
|
-
const leftTable = await
|
|
452
|
+
const leftTable = await ui.select({
|
|
416
453
|
message: "First table",
|
|
417
|
-
options:
|
|
454
|
+
options: refTables.map((t) => ({ value: t.identifier, label: t.identifier })),
|
|
418
455
|
});
|
|
419
|
-
if (
|
|
456
|
+
if (ui.isCancel(leftTable)) return null;
|
|
420
457
|
|
|
421
|
-
const rightTable = await
|
|
458
|
+
const rightTable = await ui.select({
|
|
422
459
|
message: "Second table",
|
|
423
|
-
options:
|
|
460
|
+
options: refTables.map((t) => ({ value: t.identifier, label: t.identifier })),
|
|
424
461
|
});
|
|
425
|
-
if (
|
|
462
|
+
if (ui.isCancel(rightTable)) return null;
|
|
426
463
|
|
|
427
464
|
const defaultName = `${leftTable}_${rightTable}`;
|
|
428
|
-
const name = await
|
|
465
|
+
const name = await ui.text({
|
|
429
466
|
message: "Junction table name",
|
|
430
467
|
initialValue: defaultName,
|
|
431
468
|
validate: (value) => {
|
|
@@ -436,33 +473,34 @@ async function collectJunctionTableSpec(existingTables: ParsedTable[]): Promise<
|
|
|
436
473
|
return undefined;
|
|
437
474
|
},
|
|
438
475
|
});
|
|
439
|
-
if (
|
|
476
|
+
if (ui.isCancel(name)) return null;
|
|
440
477
|
|
|
441
|
-
const onDelete = await
|
|
478
|
+
const onDelete = await ui.select({
|
|
442
479
|
message: "Cascade behavior when either referenced row is deleted",
|
|
443
480
|
options: ON_DELETE_ACTIONS.map((a) => ({ value: a, label: a })),
|
|
444
481
|
initialValue: "cascade" as OnDeleteAction,
|
|
445
482
|
});
|
|
446
|
-
if (
|
|
483
|
+
if (ui.isCancel(onDelete)) return null;
|
|
447
484
|
|
|
448
|
-
const includeTimestamps = await
|
|
485
|
+
const includeTimestamps = await ui.confirm({
|
|
449
486
|
message: "Add createdAt / updatedAt timestamp columns?",
|
|
450
487
|
initialValue: true,
|
|
451
488
|
});
|
|
452
|
-
if (
|
|
489
|
+
if (ui.isCancel(includeTimestamps)) return null;
|
|
453
490
|
|
|
454
|
-
const addExtra = await
|
|
491
|
+
const addExtra = await ui.confirm({
|
|
455
492
|
message: "Add extra attributes to the junction table (e.g. role, quantity)?",
|
|
456
493
|
initialValue: false,
|
|
457
494
|
});
|
|
458
|
-
if (
|
|
495
|
+
if (ui.isCancel(addExtra)) return null;
|
|
459
496
|
|
|
460
497
|
const baseSpec = buildJunctionTableSpec({ leftTable, rightTable, name, includeTimestamps, onDelete });
|
|
461
498
|
let extraColumns: ColumnSpec[] = [];
|
|
462
499
|
if (addExtra) {
|
|
463
500
|
const cols = await promptForColumns(
|
|
464
501
|
existingTables,
|
|
465
|
-
baseSpec.columns.map((c) => columnIdentifier(c.name))
|
|
502
|
+
baseSpec.columns.map((c) => columnIdentifier(c.name)),
|
|
503
|
+
ui
|
|
466
504
|
);
|
|
467
505
|
if (cols === null) return null;
|
|
468
506
|
extraColumns = cols;
|
|
@@ -483,21 +521,22 @@ async function runAddTable(args: string[]): Promise<void> {
|
|
|
483
521
|
|
|
484
522
|
// eslint-disable-next-line no-constant-condition
|
|
485
523
|
while (true) {
|
|
486
|
-
const spec =
|
|
487
|
-
first && junctionFlag
|
|
488
|
-
? await collectJunctionTableSpec(existingTables)
|
|
489
|
-
: await collectTableSpec(existingTables, first ? forcedName : undefined);
|
|
490
|
-
first = false;
|
|
491
|
-
if (!spec) {
|
|
492
|
-
p.cancel("Cancelled.");
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
524
|
try {
|
|
525
|
+
const spec =
|
|
526
|
+
first && junctionFlag
|
|
527
|
+
? await collectJunctionTableSpec(existingTables)
|
|
528
|
+
: await collectTableSpec(existingTables, first ? forcedName : undefined);
|
|
529
|
+
first = false;
|
|
530
|
+
if (!spec) {
|
|
531
|
+
p.cancel("Cancelled.");
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
497
535
|
const { filePath, identifier } = await addTable(spec);
|
|
498
536
|
p.log.success(`Added table "${identifier}" to ${filePath}. Run \`bun run db:generate\` to create a migration for it.`);
|
|
499
537
|
existingTables = await readSchemaTables();
|
|
500
538
|
} catch (err) {
|
|
539
|
+
first = false;
|
|
501
540
|
p.log.error(err instanceof Error ? err.message : String(err));
|
|
502
541
|
}
|
|
503
542
|
|
|
@@ -620,49 +659,39 @@ async function runRemoveTable(args: string[]): Promise<void> {
|
|
|
620
659
|
p.outro("Done.");
|
|
621
660
|
}
|
|
622
661
|
|
|
623
|
-
/** server-config's single-shot version of runAddTable() —
|
|
624
|
-
async function serverConfigAddTable(existingTables: ParsedTable[]): Promise<string | void> {
|
|
625
|
-
const spec = await collectTableSpec(existingTables);
|
|
626
|
-
if (!spec)
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
}
|
|
630
|
-
const { filePath, identifier } = await addTable(spec);
|
|
631
|
-
p.log.success(`Added table "${identifier}" to ${filePath}. Run \`bun run db:generate\` next.`);
|
|
632
|
-
return `Added table "${identifier}"`;
|
|
662
|
+
/** server-config's single-shot version of runAddTable() — runs against any PromptUI (in-TUI modals or @clack/prompts). */
|
|
663
|
+
async function serverConfigAddTable(existingTables: ParsedTable[], ui: TuiPromptUI = clackUI): Promise<string | void> {
|
|
664
|
+
const spec = await collectTableSpec(existingTables, undefined, ui);
|
|
665
|
+
if (!spec) return "Cancelled.";
|
|
666
|
+
const { identifier } = await addTable(spec);
|
|
667
|
+
return `Added table "${identifier}" (run \`bun run db:generate\` next)`;
|
|
633
668
|
}
|
|
634
669
|
|
|
635
|
-
async function serverConfigAddJunctionTable(existingTables: ParsedTable[]): Promise<string | void> {
|
|
636
|
-
const spec = await collectJunctionTableSpec(existingTables);
|
|
637
|
-
if (!spec)
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}
|
|
641
|
-
const { filePath, identifier } = await addTable(spec);
|
|
642
|
-
p.log.success(`Added junction table "${identifier}" to ${filePath}. Run \`bun run db:generate\` next.`);
|
|
643
|
-
return `Added junction table "${identifier}"`;
|
|
670
|
+
async function serverConfigAddJunctionTable(existingTables: ParsedTable[], ui: TuiPromptUI = clackUI): Promise<string | void> {
|
|
671
|
+
const spec = await collectJunctionTableSpec(existingTables, ui);
|
|
672
|
+
if (!spec) return "Cancelled.";
|
|
673
|
+
const { identifier } = await addTable(spec);
|
|
674
|
+
return `Added junction table "${identifier}" (run \`bun run db:generate\` next)`;
|
|
644
675
|
}
|
|
645
676
|
|
|
646
|
-
async function serverConfigAddColumn(
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
677
|
+
async function serverConfigAddColumn(
|
|
678
|
+
existingTables: ParsedTable[],
|
|
679
|
+
forcedTable?: ParsedTable,
|
|
680
|
+
ui: TuiPromptUI = clackUI
|
|
681
|
+
): Promise<string | void> {
|
|
682
|
+
if (existingTables.length === 0) return "No tables yet — add a table first.";
|
|
651
683
|
let target = forcedTable;
|
|
652
684
|
if (!target) {
|
|
653
|
-
const choice = await
|
|
685
|
+
const choice = await ui.select({
|
|
654
686
|
message: "Add a column to which table?",
|
|
655
687
|
options: existingTables.map((t) => ({ value: t.identifier, label: `${t.identifier} (${t.columns.length} columns)` })),
|
|
656
688
|
});
|
|
657
|
-
if (
|
|
658
|
-
p.cancel("Cancelled.");
|
|
659
|
-
return;
|
|
660
|
-
}
|
|
689
|
+
if (ui.isCancel(choice)) return "Cancelled.";
|
|
661
690
|
target = existingTables.find((t) => t.identifier === choice);
|
|
662
691
|
}
|
|
663
692
|
if (!target) return;
|
|
664
693
|
|
|
665
|
-
const colName = await
|
|
694
|
+
const colName = await ui.text({
|
|
666
695
|
message: `New column name on "${target.identifier}"`,
|
|
667
696
|
placeholder: "e.g. avatarUrl",
|
|
668
697
|
validate: (value) => {
|
|
@@ -672,49 +701,36 @@ async function serverConfigAddColumn(existingTables: ParsedTable[], forcedTable?
|
|
|
672
701
|
return undefined;
|
|
673
702
|
},
|
|
674
703
|
});
|
|
675
|
-
if (
|
|
676
|
-
p.cancel("Cancelled.");
|
|
677
|
-
return;
|
|
678
|
-
}
|
|
704
|
+
if (ui.isCancel(colName)) return "Cancelled.";
|
|
679
705
|
|
|
680
|
-
const spec = await promptSingleColumn(colName, existingTables);
|
|
681
|
-
if (!spec)
|
|
682
|
-
p.cancel("Cancelled.");
|
|
683
|
-
return;
|
|
684
|
-
}
|
|
706
|
+
const spec = await promptSingleColumn(colName, existingTables, true, ui);
|
|
707
|
+
if (!spec) return "Cancelled.";
|
|
685
708
|
|
|
686
|
-
const { columnKey
|
|
687
|
-
p.log.success(`Added column "${columnKey}" to "${target.identifier}" in ${filePath}.`);
|
|
709
|
+
const { columnKey } = await addColumn(target.identifier, spec);
|
|
688
710
|
return `Added column "${target.identifier}.${columnKey}"`;
|
|
689
711
|
}
|
|
690
712
|
|
|
691
|
-
async function serverConfigRemoveTable(
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
713
|
+
async function serverConfigRemoveTable(
|
|
714
|
+
existingTables: ParsedTable[],
|
|
715
|
+
forcedTable?: ParsedTable,
|
|
716
|
+
ui: TuiPromptUI = clackUI
|
|
717
|
+
): Promise<string | void> {
|
|
718
|
+
if (existingTables.length === 0) return "No tables yet.";
|
|
696
719
|
let choice = forcedTable?.identifier;
|
|
697
720
|
if (!choice) {
|
|
698
|
-
const picked = await
|
|
721
|
+
const picked = await ui.select({
|
|
699
722
|
message: "Remove which table?",
|
|
700
723
|
options: existingTables.map((t) => ({ value: t.identifier, label: `${t.identifier} (${t.columns.length} columns)` })),
|
|
701
724
|
});
|
|
702
|
-
if (
|
|
703
|
-
p.cancel("Cancelled.");
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
725
|
+
if (ui.isCancel(picked)) return "Cancelled.";
|
|
706
726
|
choice = picked;
|
|
707
727
|
}
|
|
708
|
-
const confirmed = await
|
|
709
|
-
message: `Remove table "${choice}" from schema.ts? Nothing is dropped from the
|
|
728
|
+
const confirmed = await ui.confirm({
|
|
729
|
+
message: `Remove table "${choice}" from schema.ts? Nothing is dropped from the database until you run a migration.`,
|
|
710
730
|
initialValue: false,
|
|
711
731
|
});
|
|
712
|
-
if (
|
|
713
|
-
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
const { filePath } = await removeTable(choice);
|
|
717
|
-
p.log.success(`Removed "${choice}" from ${filePath}. Run \`bun run db:generate\` next.`);
|
|
732
|
+
if (ui.isCancel(confirmed) || !confirmed) return "Cancelled.";
|
|
733
|
+
await removeTable(choice);
|
|
718
734
|
return `Removed table "${choice}"`;
|
|
719
735
|
}
|
|
720
736
|
|
|
@@ -850,107 +866,102 @@ async function runAddMethod(): Promise<void> {
|
|
|
850
866
|
// step's changes are immediately visible to the next one.
|
|
851
867
|
// ---------------------------------------------------------------------------
|
|
852
868
|
|
|
853
|
-
async function serverConfigAddEndpoint(): Promise<
|
|
854
|
-
const name = await
|
|
869
|
+
async function serverConfigAddEndpoint(ui: TuiPromptUI = clackUI): Promise<string> {
|
|
870
|
+
const name = await ui.text({
|
|
855
871
|
message: "New endpoint name",
|
|
856
872
|
placeholder: "e.g. users",
|
|
857
873
|
validate: (value) => (!value ? "Required." : undefined),
|
|
858
874
|
});
|
|
859
|
-
if (
|
|
860
|
-
|
|
861
|
-
return;
|
|
862
|
-
}
|
|
863
|
-
const { route, filesWritten } = await addRoute(name);
|
|
864
|
-
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files, no methods yet).`);
|
|
875
|
+
if (ui.isCancel(name)) return "Cancelled.";
|
|
876
|
+
const { route } = await addRoute(name);
|
|
865
877
|
|
|
866
|
-
const scaffoldCrud = await
|
|
867
|
-
message:
|
|
878
|
+
const scaffoldCrud = await ui.confirm({
|
|
879
|
+
message: `Added /${route}. Scaffold standard CRUD methods (list, create, getById, update, remove)?`,
|
|
868
880
|
initialValue: false,
|
|
869
881
|
});
|
|
870
|
-
if (!
|
|
871
|
-
|
|
882
|
+
if (!ui.isCancel(scaffoldCrud) && scaffoldCrud) {
|
|
883
|
+
const result = await addCrudMethods(route);
|
|
884
|
+
return `Added /${route} with CRUD (${result.added.join(", ") || "none"})`;
|
|
872
885
|
}
|
|
886
|
+
return `Added endpoint /${route}`;
|
|
873
887
|
}
|
|
874
888
|
|
|
875
|
-
async function serverConfigRemoveEndpoint(
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
889
|
+
async function serverConfigRemoveEndpoint(
|
|
890
|
+
summaries: EndpointSummary[],
|
|
891
|
+
forced?: EndpointSummary,
|
|
892
|
+
ui: TuiPromptUI = clackUI
|
|
893
|
+
): Promise<string> {
|
|
894
|
+
if (summaries.length === 0) return "No endpoints yet.";
|
|
895
|
+
let route = forced?.route;
|
|
896
|
+
if (!route) {
|
|
897
|
+
const picked = await ui.select({
|
|
898
|
+
message: "Remove which endpoint?",
|
|
899
|
+
options: summaries.map((s) => ({ value: s.route, label: s.route })),
|
|
900
|
+
});
|
|
901
|
+
if (ui.isCancel(picked)) return "Cancelled.";
|
|
902
|
+
route = picked;
|
|
887
903
|
}
|
|
888
|
-
const confirmed = await
|
|
904
|
+
const confirmed = await ui.confirm({
|
|
889
905
|
message: `Remove src/endpoints/${route} and unmount it? This deletes the folder.`,
|
|
890
906
|
initialValue: false,
|
|
891
907
|
});
|
|
892
|
-
if (
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
}
|
|
896
|
-
const { removedDir } = await removeRoute(route);
|
|
897
|
-
p.log.success(`Removed ${removedDir}.`);
|
|
908
|
+
if (ui.isCancel(confirmed) || !confirmed) return "Cancelled.";
|
|
909
|
+
await removeRoute(route);
|
|
910
|
+
return `Removed endpoint /${route}`;
|
|
898
911
|
}
|
|
899
912
|
|
|
900
|
-
async function serverConfigAddMethod(
|
|
901
|
-
|
|
913
|
+
async function serverConfigAddMethod(
|
|
914
|
+
summaries: EndpointSummary[],
|
|
915
|
+
preferred?: EndpointSummary,
|
|
916
|
+
ui: TuiPromptUI = clackUI
|
|
917
|
+
): Promise<string> {
|
|
918
|
+
const routeChoice = await ui.select({
|
|
902
919
|
message: "Add a method to which endpoint?",
|
|
920
|
+
initialValue: preferred?.route ?? summaries[0]?.route ?? "__new__",
|
|
903
921
|
options: [
|
|
904
922
|
...summaries.map((s) => ({ value: s.route, label: s.route })),
|
|
905
923
|
{ value: "__new__", label: "+ New blank endpoint" },
|
|
906
924
|
],
|
|
907
925
|
});
|
|
908
|
-
if (
|
|
909
|
-
p.cancel("Cancelled.");
|
|
910
|
-
return;
|
|
911
|
-
}
|
|
926
|
+
if (ui.isCancel(routeChoice)) return "Cancelled.";
|
|
912
927
|
|
|
913
928
|
let route = routeChoice;
|
|
914
929
|
let existingMethods = summaries.find((s) => s.route === route)?.methods ?? [];
|
|
915
930
|
if (route === "__new__") {
|
|
916
|
-
const name = await
|
|
931
|
+
const name = await ui.text({
|
|
917
932
|
message: "New endpoint name",
|
|
918
933
|
placeholder: "e.g. users",
|
|
919
934
|
validate: (value) => (!value ? "Required." : undefined),
|
|
920
935
|
});
|
|
921
|
-
if (
|
|
922
|
-
p.cancel("Cancelled.");
|
|
923
|
-
return;
|
|
924
|
-
}
|
|
936
|
+
if (ui.isCancel(name)) return "Cancelled.";
|
|
925
937
|
const added = await addRoute(name);
|
|
926
938
|
route = added.route;
|
|
927
939
|
existingMethods = [];
|
|
928
|
-
p.log.success(`Added blank endpoint src/endpoints/${route}`);
|
|
929
940
|
}
|
|
930
941
|
|
|
931
|
-
const verb = await
|
|
942
|
+
const verb = await ui.select({
|
|
932
943
|
message: "HTTP method",
|
|
933
944
|
options: HTTP_VERBS.map((v) => ({ value: v, label: v.toUpperCase() })),
|
|
934
945
|
});
|
|
935
|
-
if (
|
|
936
|
-
p.cancel("Cancelled.");
|
|
937
|
-
return;
|
|
938
|
-
}
|
|
946
|
+
if (ui.isCancel(verb)) return "Cancelled.";
|
|
939
947
|
|
|
940
948
|
const defaultPath = verb === "get" || verb === "post" ? "/" : "/:id";
|
|
941
|
-
const path = await
|
|
949
|
+
const path = await ui.text({
|
|
942
950
|
message: "Path (relative to this endpoint's mount)",
|
|
943
951
|
placeholder: defaultPath,
|
|
944
952
|
initialValue: defaultPath,
|
|
945
|
-
validate: (value) =>
|
|
953
|
+
validate: (value) => {
|
|
954
|
+
if (!value?.startsWith("/")) return 'Must start with "/".';
|
|
955
|
+
if (existingMethods.some((m) => m.verb === verb && m.path === value)) {
|
|
956
|
+
return `${verb.toUpperCase()} ${value} is already mounted on /${route}.`;
|
|
957
|
+
}
|
|
958
|
+
return undefined;
|
|
959
|
+
},
|
|
946
960
|
});
|
|
947
|
-
if (
|
|
948
|
-
p.cancel("Cancelled.");
|
|
949
|
-
return;
|
|
950
|
-
}
|
|
961
|
+
if (ui.isCancel(path)) return "Cancelled.";
|
|
951
962
|
|
|
952
963
|
const defaultName = suggestMethodName(verb, path);
|
|
953
|
-
const name = await
|
|
964
|
+
const name = await ui.text({
|
|
954
965
|
message: "Name (controller export + service function)",
|
|
955
966
|
placeholder: defaultName,
|
|
956
967
|
initialValue: defaultName,
|
|
@@ -960,62 +971,54 @@ async function serverConfigAddMethod(summaries: EndpointSummary[]): Promise<void
|
|
|
960
971
|
return undefined;
|
|
961
972
|
},
|
|
962
973
|
});
|
|
963
|
-
if (
|
|
964
|
-
p.cancel("Cancelled.");
|
|
965
|
-
return;
|
|
966
|
-
}
|
|
974
|
+
if (ui.isCancel(name)) return "Cancelled.";
|
|
967
975
|
|
|
968
976
|
let needsBody = false;
|
|
969
977
|
if (verb === "post" || verb === "put" || verb === "patch") {
|
|
970
|
-
const answer = await
|
|
971
|
-
if (
|
|
972
|
-
p.cancel("Cancelled.");
|
|
973
|
-
return;
|
|
974
|
-
}
|
|
978
|
+
const answer = await ui.confirm({ message: "Validate a request body?", initialValue: true });
|
|
979
|
+
if (ui.isCancel(answer)) return "Cancelled.";
|
|
975
980
|
needsBody = answer;
|
|
976
981
|
}
|
|
977
982
|
|
|
978
|
-
const needsQuery = await
|
|
979
|
-
if (
|
|
980
|
-
p.cancel("Cancelled.");
|
|
981
|
-
return;
|
|
982
|
-
}
|
|
983
|
+
const needsQuery = await ui.confirm({ message: "Validate query params?", initialValue: false });
|
|
984
|
+
if (ui.isCancel(needsQuery)) return "Cancelled.";
|
|
983
985
|
|
|
984
986
|
const spec: MethodSpec = { verb, path, name, needsBody, needsQuery };
|
|
985
|
-
|
|
986
|
-
|
|
987
|
+
await addMethod(route, spec);
|
|
988
|
+
return `Added ${verb.toUpperCase()} /${route}${path === "/" ? "" : path} → ${name}()`;
|
|
987
989
|
}
|
|
988
990
|
|
|
989
991
|
/** 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. */
|
|
990
|
-
async function selectEndpointWithMethods(
|
|
992
|
+
async function selectEndpointWithMethods(
|
|
993
|
+
summaries: EndpointSummary[],
|
|
994
|
+
message: string,
|
|
995
|
+
preferred?: EndpointSummary,
|
|
996
|
+
ui: TuiPromptUI = clackUI
|
|
997
|
+
): Promise<EndpointSummary | null> {
|
|
991
998
|
const withMethods = summaries.filter((s) => s.methods.length > 0);
|
|
992
|
-
if (withMethods.length === 0)
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
}
|
|
996
|
-
const route = await p.select({
|
|
999
|
+
if (withMethods.length === 0) return null;
|
|
1000
|
+
if (preferred && preferred.methods.length > 0) return preferred;
|
|
1001
|
+
const route = await ui.select({
|
|
997
1002
|
message,
|
|
998
1003
|
options: withMethods.map((s) => ({ value: s.route, label: s.route })),
|
|
999
1004
|
});
|
|
1000
|
-
if (
|
|
1001
|
-
p.cancel("Cancelled.");
|
|
1002
|
-
return null;
|
|
1003
|
-
}
|
|
1005
|
+
if (ui.isCancel(route)) return null;
|
|
1004
1006
|
return withMethods.find((s) => s.route === route) ?? null;
|
|
1005
1007
|
}
|
|
1006
1008
|
|
|
1007
|
-
async function selectMethod(
|
|
1008
|
-
|
|
1009
|
+
async function selectMethod(
|
|
1010
|
+
endpoint: EndpointSummary,
|
|
1011
|
+
message: string,
|
|
1012
|
+
ui: TuiPromptUI = clackUI
|
|
1013
|
+
): Promise<EndpointMethod | null> {
|
|
1014
|
+
const choice = await ui.select({
|
|
1009
1015
|
message,
|
|
1010
1016
|
options: endpoint.methods.map((m) => ({
|
|
1011
1017
|
value: `${m.verb}:${m.path}`,
|
|
1012
1018
|
label: `${m.verb.toUpperCase()} ${m.path} → ${m.name}`,
|
|
1013
1019
|
})),
|
|
1014
1020
|
});
|
|
1015
|
-
if (
|
|
1016
|
-
p.cancel("Cancelled.");
|
|
1017
|
-
return null;
|
|
1018
|
-
}
|
|
1021
|
+
if (ui.isCancel(choice)) return null;
|
|
1019
1022
|
// Split on the FIRST colon only — a path itself can contain no colons
|
|
1020
1023
|
// outside of :param segments, but this keeps the parse correct even if
|
|
1021
1024
|
// that ever changes, since verb never contains one.
|
|
@@ -1023,56 +1026,63 @@ async function selectMethod(endpoint: EndpointSummary, message: string): Promise
|
|
|
1023
1026
|
return endpoint.methods.find((m) => m.verb === verb && m.path === path) ?? null;
|
|
1024
1027
|
}
|
|
1025
1028
|
|
|
1026
|
-
async function serverConfigRemoveMethod(
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
const
|
|
1029
|
+
async function serverConfigRemoveMethod(
|
|
1030
|
+
summaries: EndpointSummary[],
|
|
1031
|
+
preferred?: EndpointSummary,
|
|
1032
|
+
ui: TuiPromptUI = clackUI
|
|
1033
|
+
): Promise<string> {
|
|
1034
|
+
if (!summaries.some((s) => s.methods.length > 0)) return "No methods yet — add one first.";
|
|
1035
|
+
const endpoint = await selectEndpointWithMethods(summaries, "Remove a method from which endpoint?", preferred, ui);
|
|
1036
|
+
if (!endpoint) return "Cancelled.";
|
|
1037
|
+
const method = await selectMethod(endpoint, `Remove which method from /${endpoint.route}?`, ui);
|
|
1038
|
+
if (!method) return "Cancelled.";
|
|
1039
|
+
|
|
1040
|
+
const confirmed = await ui.confirm({
|
|
1033
1041
|
message: `Remove ${method.verb.toUpperCase()} ${method.path} (${method.name}) from ${endpoint.route}?`,
|
|
1034
1042
|
initialValue: false,
|
|
1035
1043
|
});
|
|
1036
|
-
if (
|
|
1037
|
-
p.cancel("Cancelled.");
|
|
1038
|
-
return;
|
|
1039
|
-
}
|
|
1044
|
+
if (ui.isCancel(confirmed) || !confirmed) return "Cancelled.";
|
|
1040
1045
|
|
|
1041
1046
|
const { removed } = await removeMethod(endpoint.route, method.verb, method.path);
|
|
1042
|
-
|
|
1047
|
+
return `Removed ${removed.verb.toUpperCase()} ${removed.path} (${removed.name})`;
|
|
1043
1048
|
}
|
|
1044
1049
|
|
|
1045
1050
|
/** Edits a method's verb/path/name/validation in place: a checked removeMethod() + addMethod() pair, not a separate codegen path. */
|
|
1046
|
-
async function serverConfigModifyMethod(
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
+
async function serverConfigModifyMethod(
|
|
1052
|
+
summaries: EndpointSummary[],
|
|
1053
|
+
preferred?: EndpointSummary,
|
|
1054
|
+
ui: TuiPromptUI = clackUI
|
|
1055
|
+
): Promise<string> {
|
|
1056
|
+
if (!summaries.some((s) => s.methods.length > 0)) return "No methods yet — add one first.";
|
|
1057
|
+
const endpoint = await selectEndpointWithMethods(summaries, "Modify a method on which endpoint?", preferred, ui);
|
|
1058
|
+
if (!endpoint) return "Cancelled.";
|
|
1059
|
+
const old = await selectMethod(endpoint, `Modify which method on /${endpoint.route}?`, ui);
|
|
1060
|
+
if (!old) return "Cancelled.";
|
|
1051
1061
|
|
|
1052
1062
|
const { needsBody: oldNeedsBody, needsQuery: oldNeedsQuery } = await detectMethodValidation(endpoint.route, old.name);
|
|
1053
1063
|
const others = endpoint.methods.filter((m) => m !== old);
|
|
1054
1064
|
|
|
1055
|
-
const verb = await
|
|
1065
|
+
const verb = await ui.select({
|
|
1056
1066
|
message: "HTTP method",
|
|
1057
1067
|
initialValue: old.verb,
|
|
1058
1068
|
options: HTTP_VERBS.map((v) => ({ value: v, label: v.toUpperCase() })),
|
|
1059
1069
|
});
|
|
1060
|
-
if (
|
|
1061
|
-
p.cancel("Cancelled.");
|
|
1062
|
-
return;
|
|
1063
|
-
}
|
|
1070
|
+
if (ui.isCancel(verb)) return "Cancelled.";
|
|
1064
1071
|
|
|
1065
|
-
const path = await
|
|
1072
|
+
const path = await ui.text({
|
|
1066
1073
|
message: "Path (relative to this endpoint's mount)",
|
|
1067
1074
|
initialValue: old.path,
|
|
1068
|
-
validate: (value) =>
|
|
1075
|
+
validate: (value) => {
|
|
1076
|
+
if (!value?.startsWith("/")) return 'Must start with "/".';
|
|
1077
|
+
if (others.some((m) => m.verb === verb && m.path === value)) {
|
|
1078
|
+
return `${verb.toUpperCase()} ${value} is already used by another method on this endpoint.`;
|
|
1079
|
+
}
|
|
1080
|
+
return undefined;
|
|
1081
|
+
},
|
|
1069
1082
|
});
|
|
1070
|
-
if (
|
|
1071
|
-
p.cancel("Cancelled.");
|
|
1072
|
-
return;
|
|
1073
|
-
}
|
|
1083
|
+
if (ui.isCancel(path)) return "Cancelled.";
|
|
1074
1084
|
|
|
1075
|
-
const name = await
|
|
1085
|
+
const name = await ui.text({
|
|
1076
1086
|
message: "Name (controller export + service function)",
|
|
1077
1087
|
initialValue: old.name,
|
|
1078
1088
|
validate: (value) => {
|
|
@@ -1081,39 +1091,27 @@ async function serverConfigModifyMethod(summaries: EndpointSummary[]): Promise<v
|
|
|
1081
1091
|
return undefined;
|
|
1082
1092
|
},
|
|
1083
1093
|
});
|
|
1084
|
-
if (
|
|
1085
|
-
p.cancel("Cancelled.");
|
|
1086
|
-
return;
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
// Checked before anything on disk changes — removeMethod() below is only
|
|
1090
|
-
// safe to run once we know addMethod() won't then fail on a collision.
|
|
1091
|
-
if (others.some((m) => m.verb === verb && m.path === path)) {
|
|
1092
|
-
p.log.error(`${verb.toUpperCase()} ${path} is already used by another method on this endpoint.`);
|
|
1093
|
-
return;
|
|
1094
|
-
}
|
|
1094
|
+
if (ui.isCancel(name)) return "Cancelled.";
|
|
1095
1095
|
|
|
1096
1096
|
let needsBody = false;
|
|
1097
1097
|
if (verb === "post" || verb === "put" || verb === "patch") {
|
|
1098
|
-
const answer = await
|
|
1099
|
-
if (
|
|
1100
|
-
p.cancel("Cancelled.");
|
|
1101
|
-
return;
|
|
1102
|
-
}
|
|
1098
|
+
const answer = await ui.confirm({ message: "Validate a request body?", initialValue: oldNeedsBody });
|
|
1099
|
+
if (ui.isCancel(answer)) return "Cancelled.";
|
|
1103
1100
|
needsBody = answer;
|
|
1104
1101
|
}
|
|
1105
1102
|
|
|
1106
|
-
const needsQuery = await
|
|
1107
|
-
if (
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1103
|
+
const needsQuery = await ui.confirm({ message: "Validate query params?", initialValue: oldNeedsQuery });
|
|
1104
|
+
if (ui.isCancel(needsQuery)) return "Cancelled.";
|
|
1105
|
+
|
|
1106
|
+
const proceed = await ui.confirm({
|
|
1107
|
+
message: `Apply changes to ${old.verb.toUpperCase()} ${old.path} (${old.name})? This regenerates its controller/service stubs.`,
|
|
1108
|
+
initialValue: true,
|
|
1109
|
+
});
|
|
1110
|
+
if (ui.isCancel(proceed) || !proceed) return "Cancelled.";
|
|
1111
1111
|
|
|
1112
1112
|
await removeMethod(endpoint.route, old.verb, old.path);
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
`Updated src/endpoints/${endpoint.route}: ${old.verb.toUpperCase()} ${old.path} → ${verb.toUpperCase()} ${path} (${name}), ${filesWritten.length} files touched.`
|
|
1116
|
-
);
|
|
1113
|
+
await addMethod(endpoint.route, { verb, path, name, needsBody, needsQuery });
|
|
1114
|
+
return `Updated ${old.verb.toUpperCase()} ${old.path} → ${verb.toUpperCase()} ${path} (${name})`;
|
|
1117
1115
|
}
|
|
1118
1116
|
|
|
1119
1117
|
async function runServerConfig(): Promise<void> {
|
|
@@ -1139,48 +1137,31 @@ async function runServerConfig(): Promise<void> {
|
|
|
1139
1137
|
issues: doctor.issues,
|
|
1140
1138
|
};
|
|
1141
1139
|
},
|
|
1142
|
-
addEndpoint:
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
const
|
|
1147
|
-
|
|
1148
|
-
const confirmed = await p.confirm({
|
|
1149
|
-
message: `Remove src/endpoints/${ep.route} and unmount it? This deletes the folder.`,
|
|
1150
|
-
initialValue: false,
|
|
1151
|
-
});
|
|
1152
|
-
if (!p.isCancel(confirmed) && confirmed) await removeRoute(ep.route);
|
|
1153
|
-
} else {
|
|
1154
|
-
await serverConfigRemoveEndpoint(summaries);
|
|
1155
|
-
}
|
|
1156
|
-
},
|
|
1157
|
-
addCrud: async (ep) => {
|
|
1158
|
-
if (!ep) {
|
|
1159
|
-
await serverConfigAddEndpoint();
|
|
1160
|
-
return;
|
|
1161
|
-
}
|
|
1162
|
-
logCrudResult(await addCrudMethods(ep.route));
|
|
1163
|
-
},
|
|
1164
|
-
addMethod: async () => {
|
|
1165
|
-
await serverConfigAddMethod(await listEndpointsWithMethods());
|
|
1166
|
-
},
|
|
1167
|
-
modifyMethod: async () => {
|
|
1168
|
-
await serverConfigModifyMethod(await listEndpointsWithMethods());
|
|
1169
|
-
},
|
|
1170
|
-
removeMethod: async () => {
|
|
1171
|
-
await serverConfigRemoveMethod(await listEndpointsWithMethods());
|
|
1140
|
+
addEndpoint: (ui) => serverConfigAddEndpoint(ui),
|
|
1141
|
+
removeEndpoint: async (ep, ui) => serverConfigRemoveEndpoint(await listEndpointsWithMethods(), ep, ui),
|
|
1142
|
+
addCrud: async (ep, ui) => {
|
|
1143
|
+
if (!ep) return serverConfigAddEndpoint(ui);
|
|
1144
|
+
const result = await addCrudMethods(ep.route);
|
|
1145
|
+
return `Added CRUD to /${result.route}: ${result.added.join(", ") || "already present"}`;
|
|
1172
1146
|
},
|
|
1173
|
-
|
|
1174
|
-
|
|
1147
|
+
addMethod: async (ep, ui) => serverConfigAddMethod(await listEndpointsWithMethods(), ep, ui),
|
|
1148
|
+
modifyMethod: async (ep, ui) => serverConfigModifyMethod(await listEndpointsWithMethods(), ep, ui),
|
|
1149
|
+
removeMethod: async (ep, ui) => serverConfigRemoveMethod(await listEndpointsWithMethods(), ep, ui),
|
|
1150
|
+
addTable: async (tables, ui) => {
|
|
1151
|
+
await requireSchemaSupport();
|
|
1152
|
+
return serverConfigAddTable(tables, ui);
|
|
1175
1153
|
},
|
|
1176
|
-
addJunctionTable: async (tables) => {
|
|
1177
|
-
|
|
1154
|
+
addJunctionTable: async (tables, ui) => {
|
|
1155
|
+
await requireSchemaSupport();
|
|
1156
|
+
return serverConfigAddJunctionTable(tables, ui);
|
|
1178
1157
|
},
|
|
1179
|
-
addColumn: async (tbl, tables) => {
|
|
1180
|
-
|
|
1158
|
+
addColumn: async (tbl, tables, ui) => {
|
|
1159
|
+
await requireSchemaSupport();
|
|
1160
|
+
return serverConfigAddColumn(tables, tbl, ui);
|
|
1181
1161
|
},
|
|
1182
|
-
removeTable: async (tbl, tables) => {
|
|
1183
|
-
|
|
1162
|
+
removeTable: async (tbl, tables, ui) => {
|
|
1163
|
+
await requireSchemaSupport();
|
|
1164
|
+
return serverConfigRemoveTable(tables, tbl, ui);
|
|
1184
1165
|
},
|
|
1185
1166
|
generateClient: async () => {
|
|
1186
1167
|
const { root, ext } = await detectProject();
|
|
@@ -1230,35 +1211,37 @@ async function runServerConfig(): Promise<void> {
|
|
|
1230
1211
|
if (p.isCancel(action) || action === "exit") break;
|
|
1231
1212
|
|
|
1232
1213
|
try {
|
|
1214
|
+
let result: string | void = undefined;
|
|
1233
1215
|
switch (action) {
|
|
1234
1216
|
case "add-endpoint":
|
|
1235
|
-
await serverConfigAddEndpoint();
|
|
1217
|
+
result = await serverConfigAddEndpoint(clackUI);
|
|
1236
1218
|
break;
|
|
1237
1219
|
case "remove-endpoint":
|
|
1238
|
-
await serverConfigRemoveEndpoint(summaries);
|
|
1220
|
+
result = await serverConfigRemoveEndpoint(summaries, undefined, clackUI);
|
|
1239
1221
|
break;
|
|
1240
1222
|
case "add-method":
|
|
1241
|
-
await serverConfigAddMethod(summaries);
|
|
1223
|
+
result = await serverConfigAddMethod(summaries, undefined, clackUI);
|
|
1242
1224
|
break;
|
|
1243
1225
|
case "remove-method":
|
|
1244
|
-
await serverConfigRemoveMethod(summaries);
|
|
1226
|
+
result = await serverConfigRemoveMethod(summaries, undefined, clackUI);
|
|
1245
1227
|
break;
|
|
1246
1228
|
case "modify-method":
|
|
1247
|
-
await serverConfigModifyMethod(summaries);
|
|
1229
|
+
result = await serverConfigModifyMethod(summaries, undefined, clackUI);
|
|
1248
1230
|
break;
|
|
1249
1231
|
case "add-table":
|
|
1250
|
-
if (await ensureSchemaSupport()) await serverConfigAddTable(await readSchemaTables());
|
|
1232
|
+
if (await ensureSchemaSupport()) result = await serverConfigAddTable(await readSchemaTables(), clackUI);
|
|
1251
1233
|
break;
|
|
1252
1234
|
case "add-junction":
|
|
1253
|
-
if (await ensureSchemaSupport()) await serverConfigAddJunctionTable(await readSchemaTables());
|
|
1235
|
+
if (await ensureSchemaSupport()) result = await serverConfigAddJunctionTable(await readSchemaTables(), clackUI);
|
|
1254
1236
|
break;
|
|
1255
1237
|
case "add-column":
|
|
1256
|
-
if (await ensureSchemaSupport()) await serverConfigAddColumn(await readSchemaTables());
|
|
1238
|
+
if (await ensureSchemaSupport()) result = await serverConfigAddColumn(await readSchemaTables(), undefined, clackUI);
|
|
1257
1239
|
break;
|
|
1258
1240
|
case "remove-table":
|
|
1259
|
-
if (await ensureSchemaSupport()) await serverConfigRemoveTable(await readSchemaTables());
|
|
1241
|
+
if (await ensureSchemaSupport()) result = await serverConfigRemoveTable(await readSchemaTables(), undefined, clackUI);
|
|
1260
1242
|
break;
|
|
1261
1243
|
}
|
|
1244
|
+
if (result) p.log.info(result);
|
|
1262
1245
|
} catch (err) {
|
|
1263
1246
|
p.log.error(err instanceof Error ? err.message : String(err));
|
|
1264
1247
|
}
|