create-backon 0.14.0 → 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/tui.ts
CHANGED
|
@@ -23,18 +23,68 @@ export interface TuiState {
|
|
|
23
23
|
statusMessage?: string;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export interface SelectOption<T extends string = string> {
|
|
27
|
+
value: T;
|
|
28
|
+
label: string;
|
|
29
|
+
hint?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const TUI_CANCEL: unique symbol = Symbol("tui-cancel");
|
|
33
|
+
|
|
34
|
+
/** Prompt interface implemented by both the in-TUI modal engine and the non-TTY @clack/prompts fallback. */
|
|
35
|
+
export interface TuiPromptUI {
|
|
36
|
+
text(opts: {
|
|
37
|
+
message: string;
|
|
38
|
+
placeholder?: string;
|
|
39
|
+
initialValue?: string;
|
|
40
|
+
validate?: (value: string) => string | undefined;
|
|
41
|
+
}): Promise<string | symbol>;
|
|
42
|
+
select<T extends string>(opts: {
|
|
43
|
+
message: string;
|
|
44
|
+
options: SelectOption<T>[];
|
|
45
|
+
initialValue?: T;
|
|
46
|
+
}): Promise<T | symbol>;
|
|
47
|
+
confirm(opts: { message: string; initialValue?: boolean }): Promise<boolean | symbol>;
|
|
48
|
+
isCancel(value: unknown): value is symbol;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type TuiModalState =
|
|
52
|
+
| {
|
|
53
|
+
kind: "text";
|
|
54
|
+
message: string;
|
|
55
|
+
placeholder?: string;
|
|
56
|
+
value: string;
|
|
57
|
+
cursor: number;
|
|
58
|
+
error?: string;
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
kind: "select";
|
|
62
|
+
message: string;
|
|
63
|
+
options: SelectOption<string>[];
|
|
64
|
+
selectedIndex: number;
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
67
|
+
kind: "confirm";
|
|
68
|
+
message: string;
|
|
69
|
+
value: boolean;
|
|
70
|
+
}
|
|
71
|
+
| {
|
|
72
|
+
kind: "error";
|
|
73
|
+
message: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
26
76
|
export interface TuiActions {
|
|
27
77
|
loadSnapshot: () => Promise<TuiSnapshot>;
|
|
28
|
-
addEndpoint: () => Promise<void>;
|
|
29
|
-
removeEndpoint: (endpoint: EndpointSummary | undefined) => Promise<void>;
|
|
30
|
-
addCrud: (endpoint: EndpointSummary | undefined) => Promise<void>;
|
|
31
|
-
addMethod: (endpoint: EndpointSummary | undefined) => Promise<void>;
|
|
32
|
-
modifyMethod: (endpoint: EndpointSummary | undefined) => Promise<void>;
|
|
33
|
-
removeMethod: (endpoint: EndpointSummary | undefined) => Promise<void>;
|
|
34
|
-
addTable: (tables: ParsedTable[]) => Promise<void>;
|
|
35
|
-
addJunctionTable: (tables: ParsedTable[]) => Promise<void>;
|
|
36
|
-
addColumn: (table: ParsedTable | undefined, tables: ParsedTable[]) => Promise<void>;
|
|
37
|
-
removeTable: (table: ParsedTable | undefined, tables: ParsedTable[]) => Promise<void>;
|
|
78
|
+
addEndpoint: (ui: TuiPromptUI) => Promise<string | void>;
|
|
79
|
+
removeEndpoint: (endpoint: EndpointSummary | undefined, ui: TuiPromptUI) => Promise<string | void>;
|
|
80
|
+
addCrud: (endpoint: EndpointSummary | undefined, ui: TuiPromptUI) => Promise<string | void>;
|
|
81
|
+
addMethod: (endpoint: EndpointSummary | undefined, ui: TuiPromptUI) => Promise<string | void>;
|
|
82
|
+
modifyMethod: (endpoint: EndpointSummary | undefined, ui: TuiPromptUI) => Promise<string | void>;
|
|
83
|
+
removeMethod: (endpoint: EndpointSummary | undefined, ui: TuiPromptUI) => Promise<string | void>;
|
|
84
|
+
addTable: (tables: ParsedTable[], ui: TuiPromptUI) => Promise<string | void>;
|
|
85
|
+
addJunctionTable: (tables: ParsedTable[], ui: TuiPromptUI) => Promise<string | void>;
|
|
86
|
+
addColumn: (table: ParsedTable | undefined, tables: ParsedTable[], ui: TuiPromptUI) => Promise<string | void>;
|
|
87
|
+
removeTable: (table: ParsedTable | undefined, tables: ParsedTable[], ui: TuiPromptUI) => Promise<string | void>;
|
|
38
88
|
generateClient: () => Promise<string>;
|
|
39
89
|
generateDocs: () => Promise<string>;
|
|
40
90
|
}
|
|
@@ -61,6 +111,26 @@ function padOrTruncate(text: string, width: number): string {
|
|
|
61
111
|
return width <= 1 ? plain.slice(0, width) : plain.slice(0, width - 1) + "…";
|
|
62
112
|
}
|
|
63
113
|
|
|
114
|
+
function wrapText(text: string, width: number): string[] {
|
|
115
|
+
if (width <= 4) return [text.slice(0, Math.max(1, width))];
|
|
116
|
+
const out: string[] = [];
|
|
117
|
+
for (const rawLine of text.split("\n")) {
|
|
118
|
+
let remaining = rawLine.trim();
|
|
119
|
+
if (!remaining) {
|
|
120
|
+
out.push("");
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
while (remaining.length > width) {
|
|
124
|
+
let splitAt = remaining.lastIndexOf(" ", width);
|
|
125
|
+
if (splitAt <= 0) splitAt = width;
|
|
126
|
+
out.push(remaining.slice(0, splitAt));
|
|
127
|
+
remaining = remaining.slice(splitAt).trimStart();
|
|
128
|
+
}
|
|
129
|
+
if (remaining) out.push(remaining);
|
|
130
|
+
}
|
|
131
|
+
return out.length > 0 ? out : [""];
|
|
132
|
+
}
|
|
133
|
+
|
|
64
134
|
function verbBadge(verb: string): string {
|
|
65
135
|
const upper = verb.toUpperCase().padEnd(6);
|
|
66
136
|
if (verb === "get") return `${ANSI_GREEN}${upper}${ANSI_RESET}`;
|
|
@@ -90,8 +160,102 @@ export function nextTab(current: TuiTab, delta: 1 | -1): TuiTab {
|
|
|
90
160
|
return TABS[(idx + delta + TABS.length) % TABS.length]!;
|
|
91
161
|
}
|
|
92
162
|
|
|
93
|
-
|
|
94
|
-
|
|
163
|
+
function renderModalBox(modal: TuiModalState, maxWidth: number, maxHeight: number): string[] {
|
|
164
|
+
const boxWidth = Math.max(40, Math.min(68, maxWidth));
|
|
165
|
+
const innerWidth = boxWidth - 4;
|
|
166
|
+
const borderColor = modal.kind === "error" ? ANSI_RED : ANSI_CYAN;
|
|
167
|
+
const titleLabel = modal.kind === "error" ? " Error " : " server-config ";
|
|
168
|
+
const topDashes = Math.max(0, boxWidth - 2 - titleLabel.length - 1);
|
|
169
|
+
const topBorder = `${borderColor}┌─${ANSI_BOLD}${titleLabel}${ANSI_RESET}${borderColor}${"─".repeat(topDashes)}┐${ANSI_RESET}`;
|
|
170
|
+
const bottomBorder = `${borderColor}└${"─".repeat(boxWidth - 2)}┘${ANSI_RESET}`;
|
|
171
|
+
|
|
172
|
+
const boxRow = (content: string) =>
|
|
173
|
+
`${borderColor}│${ANSI_RESET} ${padOrTruncate(content, innerWidth)} ${borderColor}│${ANSI_RESET}`;
|
|
174
|
+
|
|
175
|
+
const contentLines: string[] = [];
|
|
176
|
+
|
|
177
|
+
if (modal.kind === "text") {
|
|
178
|
+
for (const line of wrapText(modal.message, innerWidth)) {
|
|
179
|
+
contentLines.push(`${ANSI_BOLD}${line}${ANSI_RESET}`);
|
|
180
|
+
}
|
|
181
|
+
contentLines.push("");
|
|
182
|
+
const shown = modal.value
|
|
183
|
+
? modal.value.slice(0, modal.cursor) + "█" + modal.value.slice(modal.cursor)
|
|
184
|
+
: `█${ANSI_DIM}${modal.placeholder ?? ""}${ANSI_RESET}`;
|
|
185
|
+
contentLines.push(`${ANSI_CYAN}>${ANSI_RESET} ${shown}`);
|
|
186
|
+
if (modal.error) {
|
|
187
|
+
contentLines.push("");
|
|
188
|
+
for (const errLine of wrapText(modal.error, innerWidth)) {
|
|
189
|
+
contentLines.push(`${ANSI_RED}${errLine}${ANSI_RESET}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
contentLines.push("");
|
|
193
|
+
contentLines.push(`${ANSI_DIM}Enter Submit · Esc Cancel${ANSI_RESET}`);
|
|
194
|
+
} else if (modal.kind === "select") {
|
|
195
|
+
for (const line of wrapText(modal.message, innerWidth)) {
|
|
196
|
+
contentLines.push(`${ANSI_BOLD}${line}${ANSI_RESET}`);
|
|
197
|
+
}
|
|
198
|
+
contentLines.push("");
|
|
199
|
+
const maxVisible = Math.max(4, maxHeight - contentLines.length - 5);
|
|
200
|
+
const total = modal.options.length;
|
|
201
|
+
const start = Math.max(0, Math.min(modal.selectedIndex - Math.floor(maxVisible / 2), total - maxVisible));
|
|
202
|
+
const end = Math.min(total, start + maxVisible);
|
|
203
|
+
if (start > 0) contentLines.push(`${ANSI_DIM} ↑ (${start} more)${ANSI_RESET}`);
|
|
204
|
+
for (let i = start; i < end; i++) {
|
|
205
|
+
const opt = modal.options[i]!;
|
|
206
|
+
const active = i === modal.selectedIndex;
|
|
207
|
+
const prefix = active ? `${ANSI_CYAN}▸${ANSI_RESET} ` : " ";
|
|
208
|
+
const hint = opt.hint ? ` ${ANSI_DIM}(${opt.hint})${ANSI_RESET}` : "";
|
|
209
|
+
const text = `${prefix}${active ? ANSI_BOLD : ""}${opt.label}${ANSI_RESET}${hint}`;
|
|
210
|
+
contentLines.push(text);
|
|
211
|
+
}
|
|
212
|
+
if (end < total) contentLines.push(`${ANSI_DIM} ↓ (${total - end} more)${ANSI_RESET}`);
|
|
213
|
+
contentLines.push("");
|
|
214
|
+
contentLines.push(`${ANSI_DIM}↑/↓ Move · Enter Select · Esc Cancel${ANSI_RESET}`);
|
|
215
|
+
} else if (modal.kind === "confirm") {
|
|
216
|
+
for (const line of wrapText(modal.message, innerWidth)) {
|
|
217
|
+
contentLines.push(`${ANSI_BOLD}${line}${ANSI_RESET}`);
|
|
218
|
+
}
|
|
219
|
+
contentLines.push("");
|
|
220
|
+
const yesBtn = modal.value ? `${ANSI_INVERSE}${ANSI_BOLD} Yes ${ANSI_RESET}` : `${ANSI_DIM} Yes ${ANSI_RESET}`;
|
|
221
|
+
const noBtn = !modal.value ? `${ANSI_INVERSE}${ANSI_BOLD} No ${ANSI_RESET}` : `${ANSI_DIM} No ${ANSI_RESET}`;
|
|
222
|
+
contentLines.push(`${yesBtn} ${noBtn}`);
|
|
223
|
+
contentLines.push("");
|
|
224
|
+
contentLines.push(`${ANSI_DIM}←/→/Tab Toggle · y/n · Enter Confirm · Esc Cancel${ANSI_RESET}`);
|
|
225
|
+
} else {
|
|
226
|
+
for (const line of wrapText(modal.message, innerWidth)) {
|
|
227
|
+
contentLines.push(`${ANSI_RED}${line}${ANSI_RESET}`);
|
|
228
|
+
}
|
|
229
|
+
contentLines.push("");
|
|
230
|
+
contentLines.push(`${ANSI_DIM}Press any key to continue...${ANSI_RESET}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return [topBorder, ...contentLines.map(boxRow), bottomBorder];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function overlayModal(frameLines: string[], modal: TuiModalState, cols: number, rows: number): string[] {
|
|
237
|
+
const boxLines = renderModalBox(modal, cols - 4, rows - 4);
|
|
238
|
+
const boxWidth = visibleLength(boxLines[0]!);
|
|
239
|
+
const startRow = Math.max(2, Math.floor((rows - boxLines.length) / 2));
|
|
240
|
+
const leftPad = Math.max(0, Math.floor((cols - boxWidth) / 2));
|
|
241
|
+
const result = frameLines.slice();
|
|
242
|
+
for (let i = 0; i < boxLines.length; i++) {
|
|
243
|
+
const targetRow = startRow + i;
|
|
244
|
+
if (targetRow >= 0 && targetRow < result.length) {
|
|
245
|
+
result[targetRow] = `${" ".repeat(leftPad)}${boxLines[i]}`;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Pure renderer producing a complete terminal frame (plus optional in-TUI modal overlay). */
|
|
252
|
+
export function renderTuiFrame(
|
|
253
|
+
snapshot: TuiSnapshot,
|
|
254
|
+
rawState: TuiState,
|
|
255
|
+
width = 96,
|
|
256
|
+
height = 26,
|
|
257
|
+
modal?: TuiModalState | null
|
|
258
|
+
): string {
|
|
95
259
|
const state = clampSelection(snapshot, rawState);
|
|
96
260
|
const cols = Math.max(60, width);
|
|
97
261
|
const rows = Math.max(16, height);
|
|
@@ -210,79 +374,249 @@ export function renderTuiFrame(snapshot: TuiSnapshot, rawState: TuiState, width
|
|
|
210
374
|
? "[a] Add table [m] Junction (M:N) [c] Add column [d] Remove table"
|
|
211
375
|
: "[g] Generate client [o] Generate docs [r] Re-run doctor";
|
|
212
376
|
|
|
213
|
-
const statusLine = state.statusMessage
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
377
|
+
const statusLine = state.statusMessage
|
|
378
|
+
? `${ANSI_CYAN}${state.statusMessage}${ANSI_RESET}`
|
|
379
|
+
: `${ANSI_DIM}↑/↓ Move Tab/1-3 Switch r Refresh q Quit${ANSI_RESET}`;
|
|
380
|
+
|
|
381
|
+
const frameLines = [
|
|
382
|
+
padOrTruncate(header, cols),
|
|
383
|
+
padOrTruncate(tabBar, cols),
|
|
384
|
+
rule,
|
|
385
|
+
...body,
|
|
386
|
+
rule,
|
|
387
|
+
padOrTruncate(`${ANSI_DIM}${shortcuts}${ANSI_RESET}`, cols),
|
|
388
|
+
padOrTruncate(statusLine, cols),
|
|
389
|
+
];
|
|
390
|
+
|
|
391
|
+
const finalLines = modal ? overlayModal(frameLines, modal, cols, rows) : frameLines;
|
|
392
|
+
return finalLines.join("\n");
|
|
217
393
|
}
|
|
218
394
|
|
|
219
|
-
|
|
220
|
-
|
|
395
|
+
/** The two streams the TUI reads from and draws to. Injectable so key handling can be tested without a real terminal. */
|
|
396
|
+
export interface TuiIO {
|
|
397
|
+
stdin: NodeJS.ReadStream;
|
|
398
|
+
stdout: NodeJS.WriteStream;
|
|
221
399
|
}
|
|
222
400
|
|
|
223
|
-
function
|
|
224
|
-
|
|
401
|
+
function enterAltScreen(out: NodeJS.WriteStream): void {
|
|
402
|
+
out.write("\x1b[?1049h\x1b[?25l");
|
|
225
403
|
}
|
|
226
404
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
await new Promise<void>((resolve) => {
|
|
230
|
-
const wasRaw = process.stdin.isRaw;
|
|
231
|
-
process.stdin.setRawMode(true);
|
|
232
|
-
process.stdin.resume();
|
|
233
|
-
const onData = () => {
|
|
234
|
-
process.stdin.removeListener("data", onData);
|
|
235
|
-
if (!wasRaw) process.stdin.setRawMode(false);
|
|
236
|
-
resolve();
|
|
237
|
-
};
|
|
238
|
-
process.stdin.once("data", onData);
|
|
239
|
-
});
|
|
405
|
+
function exitAltScreen(out: NodeJS.WriteStream): void {
|
|
406
|
+
out.write("\x1b[?25h\x1b[?1049l");
|
|
240
407
|
}
|
|
241
408
|
|
|
242
|
-
/** Runs an interactive full-screen keyboard TUI
|
|
243
|
-
export async function runServerConfigTui(
|
|
409
|
+
/** Runs an interactive full-screen keyboard TUI with all prompts rendered as in-TUI modals. */
|
|
410
|
+
export async function runServerConfigTui(
|
|
411
|
+
actions: TuiActions,
|
|
412
|
+
io: TuiIO = { stdin: process.stdin, stdout: process.stdout }
|
|
413
|
+
): Promise<void> {
|
|
414
|
+
const { stdin, stdout } = io;
|
|
244
415
|
let snapshot = await actions.loadSnapshot();
|
|
245
416
|
let state: TuiState = { tab: "endpoints", selectedIndex: 0 };
|
|
246
417
|
|
|
247
|
-
emitKeypressEvents(
|
|
248
|
-
enterAltScreen();
|
|
418
|
+
emitKeypressEvents(stdin);
|
|
419
|
+
enterAltScreen(stdout);
|
|
420
|
+
|
|
421
|
+
let activeModal: TuiModalState | null = null;
|
|
422
|
+
let onModalKey: ((str: string, key: Key) => void) | null = null;
|
|
423
|
+
let busy = false;
|
|
249
424
|
|
|
250
425
|
const draw = () => {
|
|
251
|
-
const width =
|
|
252
|
-
const height =
|
|
253
|
-
|
|
426
|
+
const width = stdout.columns || 96;
|
|
427
|
+
const height = stdout.rows || 26;
|
|
428
|
+
stdout.write("\x1b[H\x1b[2J" + renderTuiFrame(snapshot, state, width, height, activeModal));
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const tuiPrompt: TuiPromptUI = {
|
|
432
|
+
isCancel: (value): value is symbol => value === TUI_CANCEL,
|
|
433
|
+
text: (opts) =>
|
|
434
|
+
new Promise<string | symbol>((resolve) => {
|
|
435
|
+
const initial = opts.initialValue ?? "";
|
|
436
|
+
const modal: Extract<TuiModalState, { kind: "text" }> = {
|
|
437
|
+
kind: "text",
|
|
438
|
+
message: opts.message,
|
|
439
|
+
placeholder: opts.placeholder,
|
|
440
|
+
value: initial,
|
|
441
|
+
cursor: initial.length,
|
|
442
|
+
};
|
|
443
|
+
activeModal = modal;
|
|
444
|
+
onModalKey = (str, key) => {
|
|
445
|
+
if (key.name === "escape" || (key.ctrl && key.name === "c")) {
|
|
446
|
+
activeModal = null;
|
|
447
|
+
onModalKey = null;
|
|
448
|
+
resolve(TUI_CANCEL);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (key.name === "return" || key.name === "enter") {
|
|
452
|
+
const err = opts.validate?.(modal.value);
|
|
453
|
+
if (err) {
|
|
454
|
+
modal.error = err;
|
|
455
|
+
draw();
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
activeModal = null;
|
|
459
|
+
onModalKey = null;
|
|
460
|
+
resolve(modal.value);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (key.name === "backspace") {
|
|
464
|
+
if (modal.cursor > 0) {
|
|
465
|
+
modal.value = modal.value.slice(0, modal.cursor - 1) + modal.value.slice(modal.cursor);
|
|
466
|
+
modal.cursor--;
|
|
467
|
+
modal.error = undefined;
|
|
468
|
+
}
|
|
469
|
+
} else if (key.name === "delete") {
|
|
470
|
+
if (modal.cursor < modal.value.length) {
|
|
471
|
+
modal.value = modal.value.slice(0, modal.cursor) + modal.value.slice(modal.cursor + 1);
|
|
472
|
+
modal.error = undefined;
|
|
473
|
+
}
|
|
474
|
+
} else if (key.name === "left") {
|
|
475
|
+
modal.cursor = Math.max(0, modal.cursor - 1);
|
|
476
|
+
} else if (key.name === "right") {
|
|
477
|
+
modal.cursor = Math.min(modal.value.length, modal.cursor + 1);
|
|
478
|
+
} else if (key.name === "home") {
|
|
479
|
+
modal.cursor = 0;
|
|
480
|
+
} else if (key.name === "end") {
|
|
481
|
+
modal.cursor = modal.value.length;
|
|
482
|
+
} else if (str && !key.ctrl && !key.meta && str >= " ") {
|
|
483
|
+
modal.value = modal.value.slice(0, modal.cursor) + str + modal.value.slice(modal.cursor);
|
|
484
|
+
modal.cursor += str.length;
|
|
485
|
+
modal.error = undefined;
|
|
486
|
+
}
|
|
487
|
+
draw();
|
|
488
|
+
};
|
|
489
|
+
draw();
|
|
490
|
+
}),
|
|
491
|
+
select: <T extends string>(opts: { message: string; options: SelectOption<T>[]; initialValue?: T }) =>
|
|
492
|
+
new Promise<T | symbol>((resolve) => {
|
|
493
|
+
if (opts.options.length === 0) {
|
|
494
|
+
resolve(TUI_CANCEL);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const initialIdx = opts.initialValue ? Math.max(0, opts.options.findIndex((o) => o.value === opts.initialValue)) : 0;
|
|
498
|
+
const modal: Extract<TuiModalState, { kind: "select" }> = {
|
|
499
|
+
kind: "select",
|
|
500
|
+
message: opts.message,
|
|
501
|
+
options: opts.options,
|
|
502
|
+
selectedIndex: initialIdx,
|
|
503
|
+
};
|
|
504
|
+
activeModal = modal;
|
|
505
|
+
onModalKey = (str, key) => {
|
|
506
|
+
if (key.name === "escape" || (key.ctrl && key.name === "c")) {
|
|
507
|
+
activeModal = null;
|
|
508
|
+
onModalKey = null;
|
|
509
|
+
resolve(TUI_CANCEL);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (key.name === "return" || key.name === "enter") {
|
|
513
|
+
const chosen = opts.options[modal.selectedIndex]!;
|
|
514
|
+
activeModal = null;
|
|
515
|
+
onModalKey = null;
|
|
516
|
+
resolve(chosen.value);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (key.name === "up" || str === "k") {
|
|
520
|
+
modal.selectedIndex = (modal.selectedIndex - 1 + opts.options.length) % opts.options.length;
|
|
521
|
+
} else if (key.name === "down" || str === "j") {
|
|
522
|
+
modal.selectedIndex = (modal.selectedIndex + 1) % opts.options.length;
|
|
523
|
+
}
|
|
524
|
+
draw();
|
|
525
|
+
};
|
|
526
|
+
draw();
|
|
527
|
+
}),
|
|
528
|
+
confirm: (opts) =>
|
|
529
|
+
new Promise<boolean | symbol>((resolve) => {
|
|
530
|
+
const modal: Extract<TuiModalState, { kind: "confirm" }> = {
|
|
531
|
+
kind: "confirm",
|
|
532
|
+
message: opts.message,
|
|
533
|
+
value: opts.initialValue ?? false,
|
|
534
|
+
};
|
|
535
|
+
activeModal = modal;
|
|
536
|
+
onModalKey = (str, key) => {
|
|
537
|
+
if (key.name === "escape" || (key.ctrl && key.name === "c")) {
|
|
538
|
+
activeModal = null;
|
|
539
|
+
onModalKey = null;
|
|
540
|
+
resolve(TUI_CANCEL);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (key.name === "return" || key.name === "enter") {
|
|
544
|
+
const val = modal.value;
|
|
545
|
+
activeModal = null;
|
|
546
|
+
onModalKey = null;
|
|
547
|
+
resolve(val);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
if (str === "y" || str === "Y") {
|
|
551
|
+
activeModal = null;
|
|
552
|
+
onModalKey = null;
|
|
553
|
+
resolve(true);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (str === "n" || str === "N") {
|
|
557
|
+
activeModal = null;
|
|
558
|
+
onModalKey = null;
|
|
559
|
+
resolve(false);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (key.name === "left" || key.name === "right" || key.name === "tab" || str === "h" || str === "l") {
|
|
563
|
+
modal.value = !modal.value;
|
|
564
|
+
}
|
|
565
|
+
draw();
|
|
566
|
+
};
|
|
567
|
+
draw();
|
|
568
|
+
}),
|
|
254
569
|
};
|
|
255
570
|
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
571
|
+
const showErrorModal = (message: string) =>
|
|
572
|
+
new Promise<void>((resolve) => {
|
|
573
|
+
activeModal = { kind: "error", message };
|
|
574
|
+
onModalKey = () => {
|
|
575
|
+
activeModal = null;
|
|
576
|
+
onModalKey = null;
|
|
577
|
+
resolve();
|
|
578
|
+
};
|
|
579
|
+
draw();
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
const runAction = async (fn: (ui: TuiPromptUI) => Promise<string | void>) => {
|
|
583
|
+
if (busy) return;
|
|
584
|
+
busy = true;
|
|
259
585
|
try {
|
|
260
|
-
const msg = await fn();
|
|
586
|
+
const msg = await fn(tuiPrompt);
|
|
261
587
|
snapshot = await actions.loadSnapshot();
|
|
262
588
|
state = clampSelection(snapshot, { ...state, statusMessage: msg || undefined });
|
|
263
589
|
} catch (err) {
|
|
264
|
-
|
|
265
|
-
|
|
590
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
591
|
+
state = { ...state, statusMessage: `Error: ${message}` };
|
|
592
|
+
await showErrorModal(`Error: ${message}`);
|
|
593
|
+
} finally {
|
|
594
|
+
activeModal = null;
|
|
595
|
+
onModalKey = null;
|
|
596
|
+
busy = false;
|
|
597
|
+
draw();
|
|
266
598
|
}
|
|
267
|
-
enterAltScreen();
|
|
268
|
-
process.stdin.setRawMode(true);
|
|
269
|
-
process.stdin.resume();
|
|
270
|
-
draw();
|
|
271
599
|
};
|
|
272
600
|
|
|
273
601
|
try {
|
|
274
|
-
|
|
275
|
-
|
|
602
|
+
stdin.setRawMode(true);
|
|
603
|
+
stdin.resume();
|
|
276
604
|
draw();
|
|
277
605
|
|
|
278
606
|
await new Promise<void>((resolve) => {
|
|
279
607
|
const onResize = () => draw();
|
|
280
608
|
const cleanup = () => {
|
|
281
|
-
|
|
282
|
-
|
|
609
|
+
stdin.removeListener("keypress", onKeypress);
|
|
610
|
+
stdout.removeListener("resize", onResize);
|
|
283
611
|
};
|
|
284
612
|
|
|
285
613
|
const onKeypress = async (str: string, key: Key) => {
|
|
614
|
+
if (onModalKey) {
|
|
615
|
+
onModalKey(str, key);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (busy) return;
|
|
619
|
+
|
|
286
620
|
if ((key.ctrl && key.name === "c") || key.name === "escape" || str === "q") {
|
|
287
621
|
cleanup();
|
|
288
622
|
resolve();
|
|
@@ -301,36 +635,39 @@ export async function runServerConfigTui(actions: TuiActions): Promise<void> {
|
|
|
301
635
|
} else if (key.name === "down" || str === "j") {
|
|
302
636
|
state = clampSelection(snapshot, { ...state, selectedIndex: state.selectedIndex + 1 });
|
|
303
637
|
} else if (str === "r") {
|
|
304
|
-
|
|
305
|
-
|
|
638
|
+
await runAction(async () => "Refreshed.");
|
|
639
|
+
return;
|
|
306
640
|
} else if (state.tab === "endpoints") {
|
|
307
641
|
const current = snapshot.endpoints[state.selectedIndex];
|
|
308
|
-
if (str === "a") await
|
|
309
|
-
else if (str === "d") await
|
|
310
|
-
else if (str === "c") await
|
|
311
|
-
else if (str === "n") await
|
|
312
|
-
else if (str === "m") await
|
|
313
|
-
else if (str === "x") await
|
|
642
|
+
if (str === "a") await runAction((ui) => actions.addEndpoint(ui));
|
|
643
|
+
else if (str === "d") await runAction((ui) => actions.removeEndpoint(current, ui));
|
|
644
|
+
else if (str === "c") await runAction((ui) => actions.addCrud(current, ui));
|
|
645
|
+
else if (str === "n") await runAction((ui) => actions.addMethod(current, ui));
|
|
646
|
+
else if (str === "m") await runAction((ui) => actions.modifyMethod(current, ui));
|
|
647
|
+
else if (str === "x") await runAction((ui) => actions.removeMethod(current, ui));
|
|
648
|
+
return;
|
|
314
649
|
} else if (state.tab === "tables") {
|
|
315
650
|
const current = snapshot.tables[state.selectedIndex];
|
|
316
|
-
if (str === "a") await
|
|
317
|
-
else if (str === "m") await
|
|
318
|
-
else if (str === "c") await
|
|
319
|
-
else if (str === "d") await
|
|
651
|
+
if (str === "a") await runAction((ui) => actions.addTable(snapshot.tables, ui));
|
|
652
|
+
else if (str === "m") await runAction((ui) => actions.addJunctionTable(snapshot.tables, ui));
|
|
653
|
+
else if (str === "c") await runAction((ui) => actions.addColumn(current, snapshot.tables, ui));
|
|
654
|
+
else if (str === "d") await runAction((ui) => actions.removeTable(current, snapshot.tables, ui));
|
|
655
|
+
return;
|
|
320
656
|
} else if (state.tab === "doctor") {
|
|
321
|
-
if (str === "g") await
|
|
322
|
-
else if (str === "o") await
|
|
657
|
+
if (str === "g") await runAction(() => actions.generateClient());
|
|
658
|
+
else if (str === "o") await runAction(() => actions.generateDocs());
|
|
659
|
+
return;
|
|
323
660
|
}
|
|
324
661
|
|
|
325
662
|
draw();
|
|
326
663
|
};
|
|
327
664
|
|
|
328
|
-
|
|
329
|
-
|
|
665
|
+
stdin.on("keypress", onKeypress);
|
|
666
|
+
stdout.on("resize", onResize);
|
|
330
667
|
});
|
|
331
668
|
} finally {
|
|
332
|
-
if (
|
|
333
|
-
|
|
334
|
-
exitAltScreen();
|
|
669
|
+
if (stdin.isTTY) stdin.setRawMode(false);
|
|
670
|
+
stdin.pause();
|
|
671
|
+
exitAltScreen(stdout);
|
|
335
672
|
}
|
|
336
673
|
}
|