@oktis-works/cms 0.15.2 → 0.15.4
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/generated/app-versions.js +4 -4
- package/dist/prompt.d.ts +53 -30
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +609 -376
- package/dist/prompt.js.map +1 -1
- package/package.json +5 -4
package/dist/prompt.js
CHANGED
|
@@ -1,107 +1,42 @@
|
|
|
1
|
-
// @oktis-works/cms -
|
|
1
|
+
// @oktis-works/cms - Prompts interativos baseados em @clack/prompts v1.8.1
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// 1. deterministic non-interactive fallback (CI/pipes) → flags;
|
|
7
|
-
// 2. secret mask with NO echo (raw mode only when stdin is a real TTY);
|
|
8
|
-
// 3. injectable IO → testable without a real terminal.
|
|
3
|
+
// Substitui completamente a implementação anterior baseada em readline nativa.
|
|
4
|
+
// Todos os métodos públicos são mantidos para compatibilidade com wizard.ts,
|
|
5
|
+
// config-wizard.ts, dev.ts, update.ts, redeploy.ts e qualquer outro consumidor.
|
|
9
6
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
const colorEnabled = () => !process.env['NO_COLOR'] && Boolean(process.stdout.isTTY);
|
|
23
|
-
function wrap(open, close) {
|
|
24
|
-
return (text) => colorEnabled() ? `\u001b[${open}m${text}\u001b[${close}m` : text;
|
|
25
|
-
}
|
|
26
|
-
export const style = {
|
|
27
|
-
bold: wrap(1, 22),
|
|
28
|
-
dim: wrap(2, 22),
|
|
29
|
-
red: wrap(31, 39),
|
|
30
|
-
green: wrap(32, 39),
|
|
31
|
-
yellow: wrap(33, 39),
|
|
32
|
-
blue: wrap(34, 39),
|
|
33
|
-
cyan: wrap(36, 39),
|
|
34
|
-
gray: wrap(90, 39),
|
|
35
|
-
};
|
|
36
|
-
/** Canonical glyphs — the same ones every command prints. */
|
|
37
|
-
export const symbol = {
|
|
38
|
-
ask: '?',
|
|
39
|
-
ok: '✓',
|
|
40
|
-
fail: '✗',
|
|
41
|
-
arrow: '→',
|
|
42
|
-
bullet: '•',
|
|
43
|
-
warn: '⚠',
|
|
44
|
-
cursor: '❯',
|
|
45
|
-
};
|
|
46
|
-
/** Terminal-safe layout helpers shared by menus and status screens. */
|
|
47
|
-
export function terminalWidth(fallback = 80) {
|
|
48
|
-
const columns = Number.parseInt(process.env['COLUMNS'] ?? '', 10);
|
|
49
|
-
return Number.isFinite(columns) && columns >= 40 ? columns : fallback;
|
|
50
|
-
}
|
|
51
|
-
export function visibleLength(text) {
|
|
52
|
-
return text.replace(/\u001b\[[0-?]*[ -\/]*[@-~]/g, '').length;
|
|
53
|
-
}
|
|
54
|
-
export function truncateText(text, max = terminalWidth() - 4) {
|
|
55
|
-
if (max < 4 || visibleLength(text) <= max)
|
|
56
|
-
return text;
|
|
57
|
-
return `${text.slice(0, max - 1)}…`;
|
|
58
|
-
}
|
|
59
|
-
export function wrapText(text, max = terminalWidth() - 4) {
|
|
60
|
-
if (max < 8)
|
|
61
|
-
return [truncateText(text, max)];
|
|
62
|
-
const words = text.split(/\s+/).filter(Boolean);
|
|
63
|
-
const lines = [];
|
|
64
|
-
let line = '';
|
|
65
|
-
for (const word of words) {
|
|
66
|
-
if (!line) {
|
|
67
|
-
line = word;
|
|
68
|
-
}
|
|
69
|
-
else if (visibleLength(`${line} ${word}`) <= max) {
|
|
70
|
-
line += ` ${word}`;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
lines.push(truncateText(line, max));
|
|
74
|
-
line = word;
|
|
75
|
-
}
|
|
7
|
+
// O Clack é ESM-first e TypeScript nativo. Todos os prompts retornam Promises e
|
|
8
|
+
// usam isCancel() para detectar cancelamento (Ctrl+C / AbortSignal).
|
|
9
|
+
//
|
|
10
|
+
// Em ambientes sem TTY real (testes, CI, pipes), usa implementação simplificada
|
|
11
|
+
// baseada em leitura de linha para manter compatibilidade com testes existentes.
|
|
12
|
+
import * as clack from '@clack/prompts';
|
|
13
|
+
/** Resultado de cancelamento do usuário (Ctrl+C ou AbortSignal). */
|
|
14
|
+
export class CancelError extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super('Prompt cancelled by user');
|
|
17
|
+
this.name = 'CancelError';
|
|
76
18
|
}
|
|
77
|
-
if (line)
|
|
78
|
-
lines.push(truncateText(line, max));
|
|
79
|
-
return lines.length > 0 ? lines : [''];
|
|
80
19
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
20
|
+
/** Deteta se o stream é um TTY real (não mockado para testes) - usado para Clack. */
|
|
21
|
+
function isRealTTY(stream) {
|
|
22
|
+
// Must be a real TTY with setRawMode, AND not a PassThrough or FakeTTY (used in tests)
|
|
23
|
+
return Boolean(stream.isTTY &&
|
|
24
|
+
stream.setRawMode &&
|
|
25
|
+
typeof stream.setRawMode === 'function' &&
|
|
26
|
+
stream.constructor.name !== 'PassThrough' &&
|
|
27
|
+
stream.constructor.name !== 'FakeTTY');
|
|
28
|
+
}
|
|
29
|
+
/** Deteta se o stream suporta raw mode (para secrets, arrow keys) - inclui FakeTTY. */
|
|
30
|
+
function canUseRawMode(stream) {
|
|
31
|
+
return Boolean(stream.isTTY && stream.setRawMode && typeof stream.setRawMode === 'function');
|
|
88
32
|
}
|
|
89
|
-
/** ASCII codes used in raw mode — kept numeric to stay readable in source. */
|
|
90
33
|
const CODE_ESC = 27;
|
|
91
34
|
const CODE_ETX = 3;
|
|
92
35
|
const CODE_EOT = 4;
|
|
93
36
|
const CODE_DEL = 127;
|
|
94
37
|
const CODE_BS = 8;
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Turns one terminal chunk into key tokens.
|
|
99
|
-
*
|
|
100
|
-
* Arrow keys arrive as a single chunk (`ESC [ A`), so the CSI introducer is
|
|
101
|
-
* matched first and the rest of the sequence swallowed; a lone ESC becomes
|
|
102
|
-
* `escape`. Everything else is the character itself.
|
|
103
|
-
*/
|
|
104
|
-
export function decodeKeys(chunk) {
|
|
38
|
+
/** Turns one terminal chunk into key tokens (copied from old implementation). */
|
|
39
|
+
function decodeKeys(chunk) {
|
|
105
40
|
const keys = [];
|
|
106
41
|
const named = { A: 'up', B: 'down', C: 'right', D: 'left', H: 'home', F: 'end' };
|
|
107
42
|
for (let i = 0; i < chunk.length; i++) {
|
|
@@ -116,7 +51,6 @@ export function decodeKeys(chunk) {
|
|
|
116
51
|
i += 2;
|
|
117
52
|
continue;
|
|
118
53
|
}
|
|
119
|
-
// other CSI/SS3 sequences (function keys, mouse…): swallow to the end
|
|
120
54
|
let j = i + 2;
|
|
121
55
|
while (j < chunk.length) {
|
|
122
56
|
const c = chunk.charCodeAt(j);
|
|
@@ -150,18 +84,16 @@ export function decodeKeys(chunk) {
|
|
|
150
84
|
}
|
|
151
85
|
return keys;
|
|
152
86
|
}
|
|
153
|
-
/**
|
|
154
|
-
|
|
155
|
-
/** DEL/backspace. */
|
|
156
|
-
const DEL = '\u007f';
|
|
157
|
-
class BufferedInput {
|
|
87
|
+
/** Buffered input that handles both line mode and key mode (for tests). */
|
|
88
|
+
class SimpleInput {
|
|
158
89
|
stream;
|
|
159
90
|
echo;
|
|
160
91
|
buffer = '';
|
|
161
92
|
queue = [];
|
|
162
93
|
waiters = [];
|
|
163
94
|
ended = false;
|
|
164
|
-
// raw mode state — secret mask
|
|
95
|
+
// raw mode state — secret mask and key reading (public: raw-mode helpers
|
|
96
|
+
// inside Prompt toggle it around terminal prompts)
|
|
165
97
|
raw = false;
|
|
166
98
|
rawValue = '';
|
|
167
99
|
rawWaiter = null;
|
|
@@ -200,14 +132,14 @@ class BufferedInput {
|
|
|
200
132
|
waiter('eof');
|
|
201
133
|
}
|
|
202
134
|
onData(chunk) {
|
|
203
|
-
if (this.raw) {
|
|
204
|
-
this.onRawData(chunk);
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
135
|
if (this.keyMode) {
|
|
208
136
|
this.dispatchKeys(chunk);
|
|
209
137
|
return;
|
|
210
138
|
}
|
|
139
|
+
if (this.raw) {
|
|
140
|
+
this.onRawData(chunk);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
211
143
|
this.buffer += chunk;
|
|
212
144
|
this.drain();
|
|
213
145
|
}
|
|
@@ -225,6 +157,10 @@ class BufferedInput {
|
|
|
225
157
|
this.queue.push(line);
|
|
226
158
|
}
|
|
227
159
|
}
|
|
160
|
+
/** Turns raw mode on/off on the underlying stream (never throws). */
|
|
161
|
+
applyRawMode(enabled) {
|
|
162
|
+
this.setRawMode(enabled);
|
|
163
|
+
}
|
|
228
164
|
/** Volta ao modo normal e entrega o valor lido. */
|
|
229
165
|
finishRaw(value) {
|
|
230
166
|
this.rawValue = '';
|
|
@@ -241,7 +177,6 @@ class BufferedInput {
|
|
|
241
177
|
if (ch === '\r' || ch === '\n') {
|
|
242
178
|
const value = this.rawValue;
|
|
243
179
|
this.finishRaw(value);
|
|
244
|
-
// resto do chunk (raro em raw mode) volta para o leitor normal
|
|
245
180
|
const rest = chunk.slice(i + 1);
|
|
246
181
|
if (rest) {
|
|
247
182
|
this.buffer += rest;
|
|
@@ -249,11 +184,11 @@ class BufferedInput {
|
|
|
249
184
|
}
|
|
250
185
|
return;
|
|
251
186
|
}
|
|
252
|
-
if (ch ===
|
|
187
|
+
if (ch === '\u0003') { // ETX = Ctrl+C
|
|
253
188
|
this.finishRaw('');
|
|
254
189
|
return;
|
|
255
190
|
}
|
|
256
|
-
if (ch ===
|
|
191
|
+
if (ch === '\u007f' || ch === '\b') { // DEL or backspace
|
|
257
192
|
this.rawValue = this.rawValue.slice(0, -1);
|
|
258
193
|
this.echo('\b \b');
|
|
259
194
|
continue;
|
|
@@ -285,9 +220,8 @@ class BufferedInput {
|
|
|
285
220
|
}
|
|
286
221
|
/**
|
|
287
222
|
* Reads a secret. No echo on a TTY (raw mode + `*`); without a TTY the input
|
|
288
|
-
* IS the pipe — `echo password | okcms config` is a real flow
|
|
289
|
-
*
|
|
290
|
-
* can distinguish "stream ended" from "user submitted an empty value".
|
|
223
|
+
* IS the pipe — `echo password | okcms config` is a real flow.
|
|
224
|
+
* Returns `null` at EOF so the caller can distinguish "stream ended" from "user submitted an empty value".
|
|
291
225
|
*/
|
|
292
226
|
async readSecret() {
|
|
293
227
|
const queued = this.queue.shift();
|
|
@@ -342,16 +276,126 @@ class BufferedInput {
|
|
|
342
276
|
waiter('eof');
|
|
343
277
|
}
|
|
344
278
|
}
|
|
279
|
+
export function defaultIO() {
|
|
280
|
+
const input = process.stdin;
|
|
281
|
+
const output = process.stdout;
|
|
282
|
+
return { input, output, isTTY: Boolean(input.isTTY && output.isTTY) };
|
|
283
|
+
}
|
|
284
|
+
// ---------------------------------------------------------------------------
|
|
285
|
+
// Estilo (ANSI condicional — sem cor quando NO_COLOR ou sem TTY)
|
|
286
|
+
// ---------------------------------------------------------------------------
|
|
287
|
+
const colorEnabled = () => !process.env['NO_COLOR'] && Boolean(process.stdout.isTTY);
|
|
288
|
+
function wrap(open, close) {
|
|
289
|
+
return (text) => colorEnabled() ? `\u001b[${open}m${text}\u001b[${close}m` : text;
|
|
290
|
+
}
|
|
291
|
+
export const style = {
|
|
292
|
+
bold: wrap(1, 22),
|
|
293
|
+
dim: wrap(2, 22),
|
|
294
|
+
red: wrap(31, 39),
|
|
295
|
+
green: wrap(32, 39),
|
|
296
|
+
yellow: wrap(33, 39),
|
|
297
|
+
blue: wrap(34, 39),
|
|
298
|
+
cyan: wrap(36, 39),
|
|
299
|
+
gray: wrap(90, 39),
|
|
300
|
+
};
|
|
301
|
+
/** Canonical glyphs — os mesmos símbolos usados pelo prompt anterior. */
|
|
302
|
+
export const symbol = {
|
|
303
|
+
ask: '?',
|
|
304
|
+
ok: '✓',
|
|
305
|
+
fail: '✗',
|
|
306
|
+
arrow: '→',
|
|
307
|
+
bullet: '•',
|
|
308
|
+
warn: '⚠',
|
|
309
|
+
cursor: '❯',
|
|
310
|
+
};
|
|
311
|
+
// ---------------------------------------------------------------------------
|
|
312
|
+
// Terminal-safe layout helpers (compatibilidade com implementação anterior)
|
|
313
|
+
// ---------------------------------------------------------------------------
|
|
314
|
+
/**
|
|
315
|
+
* Returns terminal width from COLUMNS env var or a fallback.
|
|
316
|
+
*/
|
|
317
|
+
export function terminalWidth(fallback = 80) {
|
|
318
|
+
const columns = Number.parseInt(process.env['COLUMNS'] ?? '', 10);
|
|
319
|
+
return Number.isFinite(columns) && columns >= 40 ? columns : fallback;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Returns visible length of text (strips ANSI escape codes).
|
|
323
|
+
*/
|
|
324
|
+
export function visibleLength(text) {
|
|
325
|
+
return text.replace(/\u001b\[[0-?]*[ -\/]*[@-~]/g, '').length;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Truncates text to fit within max width, adding ellipsis if needed.
|
|
329
|
+
*/
|
|
330
|
+
export function truncateText(text, max = terminalWidth() - 4) {
|
|
331
|
+
if (max < 4 || visibleLength(text) <= max)
|
|
332
|
+
return text;
|
|
333
|
+
return `${text.slice(0, max - 1)}…`;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Wraps text to fit within max width, splitting on word boundaries.
|
|
337
|
+
*/
|
|
338
|
+
export function wrapText(text, max = terminalWidth() - 4) {
|
|
339
|
+
if (max < 8)
|
|
340
|
+
return [truncateText(text, max)];
|
|
341
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
342
|
+
const lines = [];
|
|
343
|
+
let line = '';
|
|
344
|
+
for (const word of words) {
|
|
345
|
+
if (!line) {
|
|
346
|
+
line = word;
|
|
347
|
+
}
|
|
348
|
+
else if (visibleLength(`${line} ${word}`) <= max) {
|
|
349
|
+
line += ` ${word}`;
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
lines.push(truncateText(line, max));
|
|
353
|
+
line = word;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (line)
|
|
357
|
+
lines.push(truncateText(line, max));
|
|
358
|
+
return lines.length > 0 ? lines : [''];
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Returns ASCII fallback symbol if OKCMS_ASCII=1 or TERM=dumb.
|
|
362
|
+
*/
|
|
363
|
+
export function asciiSymbol(name) {
|
|
364
|
+
const fallback = {
|
|
365
|
+
ask: '?',
|
|
366
|
+
ok: '[ok]',
|
|
367
|
+
fail: '[x]',
|
|
368
|
+
arrow: '->',
|
|
369
|
+
bullet: '*',
|
|
370
|
+
warn: '!',
|
|
371
|
+
cursor: '>',
|
|
372
|
+
};
|
|
373
|
+
return process.env['OKCMS_ASCII'] === '1' || process.env['TERM'] === 'dumb'
|
|
374
|
+
? fallback[name]
|
|
375
|
+
: symbol[name];
|
|
376
|
+
}
|
|
377
|
+
// ---------------------------------------------------------------------------
|
|
378
|
+
// Classe Prompt
|
|
379
|
+
// ---------------------------------------------------------------------------
|
|
345
380
|
export class Prompt {
|
|
346
381
|
io;
|
|
347
|
-
|
|
382
|
+
useClack;
|
|
383
|
+
simpleInput;
|
|
348
384
|
closed = false;
|
|
349
385
|
constructor(io) {
|
|
350
386
|
this.io = { ...defaultIO(), ...io };
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
387
|
+
const inputStream = this.io.input;
|
|
388
|
+
this.useClack = this.io.isTTY && isRealTTY(inputStream);
|
|
389
|
+
if (!this.useClack) {
|
|
390
|
+
this.simpleInput = new SimpleInput(inputStream, (text) => {
|
|
391
|
+
this.io.output.write(text);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
this.simpleInput = null;
|
|
396
|
+
}
|
|
354
397
|
}
|
|
398
|
+
/** true somente quando há terminal nos DOIS lados (TTY de verdade). */
|
|
355
399
|
get interactive() {
|
|
356
400
|
return this.io.isTTY;
|
|
357
401
|
}
|
|
@@ -363,288 +407,388 @@ export class Prompt {
|
|
|
363
407
|
this.write(`\n${style.bold(text)}\n`);
|
|
364
408
|
}
|
|
365
409
|
info(text) {
|
|
366
|
-
this.
|
|
410
|
+
if (this.useClack) {
|
|
411
|
+
clack.log.info(text);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
this.write(` ${style.dim(`${symbol.arrow} ${text}`)}\n`);
|
|
415
|
+
}
|
|
367
416
|
}
|
|
368
417
|
success(text) {
|
|
369
|
-
this.
|
|
418
|
+
if (this.useClack) {
|
|
419
|
+
clack.log.success(text);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
this.write(` ${style.green(symbol.ok)} ${text}\n`);
|
|
423
|
+
}
|
|
370
424
|
}
|
|
371
425
|
warn(text) {
|
|
372
|
-
this.
|
|
426
|
+
if (this.useClack) {
|
|
427
|
+
clack.log.warn(text);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
this.write(` ${style.yellow(symbol.warn)} ${text}\n`);
|
|
431
|
+
}
|
|
373
432
|
}
|
|
374
433
|
error(text) {
|
|
375
|
-
this.
|
|
434
|
+
if (this.useClack) {
|
|
435
|
+
clack.log.error(text);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
this.write(` ${style.red(symbol.fail)} ${text}\n`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Converte uma função de validação do tipo antigo ((v: string) => string | null)
|
|
443
|
+
* para o tipo esperado pelo Clack ((v: string | undefined) => string | Error | undefined | void).
|
|
444
|
+
*/
|
|
445
|
+
static wrapValidate(validate) {
|
|
446
|
+
if (!validate)
|
|
447
|
+
return undefined;
|
|
448
|
+
return (value) => {
|
|
449
|
+
if (value === undefined || value === null || value === '')
|
|
450
|
+
return undefined;
|
|
451
|
+
const result = validate(value);
|
|
452
|
+
if (!result)
|
|
453
|
+
return undefined;
|
|
454
|
+
return result;
|
|
455
|
+
};
|
|
376
456
|
}
|
|
377
457
|
/**
|
|
378
|
-
* Arrow-key menu (↑/↓ + Enter).
|
|
379
|
-
*
|
|
380
|
-
* all — so a CI never triggers a deploy by accident.
|
|
458
|
+
* Arrow-key menu (↑/↓ + Enter). Sem TTY, retorna defaultValue ou lança erro.
|
|
459
|
+
* Usa o Clack select() por baixo para TTY real; fallback numerado/teclado para testes/CI.
|
|
381
460
|
*/
|
|
382
461
|
async select(title, choices, opts = {}) {
|
|
383
|
-
if (
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
// No raw mode (pipe, odd SSH session, test harness): numbered menu.
|
|
404
|
-
this.write(`\n ${symbol.ask} ${style.bold(title)}\n`);
|
|
405
|
-
choices.forEach((choice, index) => {
|
|
406
|
-
const marker = index === defaultIndex ? style.cyan('>') : ' ';
|
|
407
|
-
const hint = choice.hint ? ` ${style.dim(choice.hint)}` : '';
|
|
408
|
-
const label = index === defaultIndex ? style.bold(choice.label) : choice.label;
|
|
409
|
-
this.write(` ${marker} ${style.dim(`${index + 1}`)}) ${label}${hint}\n`);
|
|
462
|
+
if (!this.useClack) {
|
|
463
|
+
// Fallback: numbered menu for non-real-TTY (tests, CI)
|
|
464
|
+
// Check if we can use raw mode (arrow keys) with FakeTTY
|
|
465
|
+
const inputStream = this.io.input;
|
|
466
|
+
if (canUseRawMode(inputStream) && this.simpleInput) {
|
|
467
|
+
return this.selectRawMode(title, choices, opts);
|
|
468
|
+
}
|
|
469
|
+
return this.selectNumbered(title, choices, opts);
|
|
470
|
+
}
|
|
471
|
+
const result = await clack.select({
|
|
472
|
+
message: title,
|
|
473
|
+
// `Option<T>` requires `T extends Primitive`; cast keeps the public
|
|
474
|
+
// unconstrained SelectChoice<T> signature unchanged.
|
|
475
|
+
options: choices.map((c) => ({
|
|
476
|
+
value: c.value,
|
|
477
|
+
label: c.label,
|
|
478
|
+
hint: c.hint ?? '',
|
|
479
|
+
})),
|
|
480
|
+
initialValue: opts.defaultValue,
|
|
481
|
+
showInstructions: true,
|
|
410
482
|
});
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
483
|
+
if (clack.isCancel(result)) {
|
|
484
|
+
if (opts.cancelValue !== undefined)
|
|
485
|
+
return opts.cancelValue;
|
|
486
|
+
throw new CancelError();
|
|
487
|
+
}
|
|
488
|
+
return result;
|
|
489
|
+
}
|
|
490
|
+
/** Raw mode select for FakeTTY (arrow keys + numbered input). */
|
|
491
|
+
async selectRawMode(title, choices, opts) {
|
|
492
|
+
const { output } = this.io;
|
|
493
|
+
const stream = this.io.input;
|
|
494
|
+
let index = 0;
|
|
495
|
+
const initial = opts.defaultValue;
|
|
496
|
+
if (initial !== undefined) {
|
|
497
|
+
const found = choices.findIndex((c) => c.value === initial);
|
|
498
|
+
if (found >= 0)
|
|
499
|
+
index = found;
|
|
500
|
+
}
|
|
501
|
+
const render = () => {
|
|
502
|
+
output.write(`\n${style.bold(title)}\n`);
|
|
503
|
+
for (let i = 0; i < choices.length; i++) {
|
|
504
|
+
const c = choices[i];
|
|
505
|
+
const marker = i === index ? style.bold(`${symbol.cursor} `) : ' ';
|
|
506
|
+
const hint = c.hint ? style.dim(` (${c.hint})`) : '';
|
|
507
|
+
output.write(`${marker}${c.label}${hint}\n`);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
render();
|
|
511
|
+
// Enable raw mode
|
|
512
|
+
stream.setRawMode?.(true);
|
|
513
|
+
this.simpleInput.raw = true;
|
|
514
|
+
while (true) {
|
|
515
|
+
const key = await this.simpleInput.readKey();
|
|
516
|
+
if (key === 'eof') {
|
|
517
|
+
// No raw mode available — fall through to numbered input
|
|
518
|
+
output.write('\n');
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
if (key === 'up' || key === 'k') {
|
|
522
|
+
index = (index - 1 + choices.length) % choices.length;
|
|
523
|
+
output.write('\x1b[1A\r'.repeat(choices.length + 1)); // move up
|
|
524
|
+
render();
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
if (key === 'down' || key === 'j') {
|
|
528
|
+
index = (index + 1) % choices.length;
|
|
529
|
+
output.write('\x1b[1A\r'.repeat(choices.length + 1));
|
|
530
|
+
render();
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
if (key === 'enter' || key === '\r' || key === '\n') {
|
|
534
|
+
output.write('\n');
|
|
535
|
+
stream.setRawMode?.(false);
|
|
536
|
+
this.simpleInput.raw = false;
|
|
537
|
+
return choices[index].value;
|
|
538
|
+
}
|
|
539
|
+
if (key === 'escape' || key === 'ctrl-c') {
|
|
540
|
+
output.write('\n');
|
|
541
|
+
stream.setRawMode?.(false);
|
|
542
|
+
this.simpleInput.raw = false;
|
|
543
|
+
if (opts.cancelValue !== undefined)
|
|
544
|
+
return opts.cancelValue;
|
|
545
|
+
throw new CancelError();
|
|
546
|
+
}
|
|
547
|
+
// Numbered input (1, 2, 3...)
|
|
548
|
+
const num = Number.parseInt(key, 10);
|
|
549
|
+
if (!Number.isNaN(num) && num >= 1 && num <= choices.length) {
|
|
550
|
+
output.write('\n');
|
|
551
|
+
stream.setRawMode?.(false);
|
|
552
|
+
this.simpleInput.raw = false;
|
|
553
|
+
return choices[num - 1].value;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Disable raw mode before falling through
|
|
557
|
+
stream.setRawMode?.(false);
|
|
558
|
+
this.simpleInput.raw = false;
|
|
559
|
+
// Numbered menu via line input
|
|
560
|
+
const display = choices.map((c, i) => ` ${i + 1}) ${c.label}${c.hint ? style.dim(` (${c.hint})`) : ''}`).join('\n');
|
|
561
|
+
output.write(`${display}\n`);
|
|
562
|
+
while (true) {
|
|
563
|
+
const line = await this.simpleInput.readLine();
|
|
564
|
+
if (line === null) {
|
|
415
565
|
if (opts.cancelValue !== undefined)
|
|
416
566
|
return opts.cancelValue;
|
|
417
567
|
if (opts.defaultValue !== undefined)
|
|
418
568
|
return opts.defaultValue;
|
|
569
|
+
if (opts.nonInteractiveError)
|
|
570
|
+
throw new Error(opts.nonInteractiveError);
|
|
419
571
|
throw new Error(`input closed before "${title}" was answered`);
|
|
420
572
|
}
|
|
421
|
-
const answer =
|
|
422
|
-
if (answer === '' &&
|
|
423
|
-
return
|
|
424
|
-
const
|
|
425
|
-
if (Number.
|
|
426
|
-
return choices[
|
|
573
|
+
const answer = line.trim();
|
|
574
|
+
if (answer === '' && opts.defaultValue !== undefined)
|
|
575
|
+
return opts.defaultValue;
|
|
576
|
+
const num = Number.parseInt(answer, 10);
|
|
577
|
+
if (!Number.isNaN(num) && num >= 1 && num <= choices.length) {
|
|
578
|
+
return choices[num - 1].value;
|
|
427
579
|
}
|
|
428
580
|
const byValue = choices.find((choice) => String(choice.value) === answer);
|
|
429
581
|
if (byValue)
|
|
430
582
|
return byValue.value;
|
|
431
|
-
|
|
583
|
+
output.write(` ${style.red(symbol.fail)} Opção inválida\n`);
|
|
432
584
|
}
|
|
433
585
|
}
|
|
434
586
|
/**
|
|
435
|
-
*
|
|
436
|
-
*
|
|
587
|
+
* Fallback numbered menu for tests and non-TTY environments.
|
|
588
|
+
* Mimics the old readline-based select behavior.
|
|
437
589
|
*/
|
|
438
|
-
async
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if (
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const answer = raw.trim().toLowerCase();
|
|
455
|
-
if (answer === '') {
|
|
456
|
-
if (opts.defaultValue !== undefined)
|
|
457
|
-
return opts.defaultValue;
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
if (['y', 'yes'].includes(answer))
|
|
461
|
-
return true;
|
|
462
|
-
if (['n', 'no'].includes(answer))
|
|
463
|
-
return false;
|
|
464
|
-
this.write(` ${style.red(symbol.fail)} answer y or n\n`);
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Shared raw-mode navigator behind `select` and `confirm`.
|
|
469
|
-
*
|
|
470
|
-
* Every repaint clears the previous block (`ESC[<n>A` + `ESC[0J`), so the
|
|
471
|
-
* menu never leaves scrollback debris. `inline` lays the options on one line
|
|
472
|
-
* (confirm), `aliases` maps single keys straight to an option.
|
|
473
|
-
*/
|
|
474
|
-
async navigate(title, choices, startIndex, opts) {
|
|
475
|
-
const total = choices.length;
|
|
476
|
-
let index = Math.min(Math.max(startIndex, 0), total - 1);
|
|
477
|
-
let painted = 0;
|
|
478
|
-
const paint = (final) => {
|
|
479
|
-
if (painted > 0) {
|
|
480
|
-
// fullClear: limpa a tela INTEIRA a cada repaint — resolve terminais
|
|
481
|
-
// que não processam ESC[nA rápido o suficiente com teclas repetidas
|
|
482
|
-
if (opts.fullClear) {
|
|
483
|
-
this.write(`${ESC}[2J${ESC}[H`);
|
|
484
|
-
}
|
|
485
|
-
else {
|
|
486
|
-
this.write(`${ESC}[${painted}A${ESC}[0J`);
|
|
487
|
-
}
|
|
590
|
+
async selectNumbered(title, choices, opts) {
|
|
591
|
+
const { output } = this.io;
|
|
592
|
+
let index = 0;
|
|
593
|
+
const initial = opts.defaultValue;
|
|
594
|
+
if (initial !== undefined) {
|
|
595
|
+
const found = choices.findIndex((c) => c.value === initial);
|
|
596
|
+
if (found >= 0)
|
|
597
|
+
index = found;
|
|
598
|
+
}
|
|
599
|
+
const render = () => {
|
|
600
|
+
output.write(`\n${style.bold(title)}\n`);
|
|
601
|
+
for (let i = 0; i < choices.length; i++) {
|
|
602
|
+
const c = choices[i];
|
|
603
|
+
const marker = i === index ? style.bold(`${symbol.cursor} `) : ' ';
|
|
604
|
+
const hint = c.hint ? style.dim(` (${c.hint})`) : '';
|
|
605
|
+
output.write(`${marker}${c.label}${hint}\n`);
|
|
488
606
|
}
|
|
489
|
-
const lines = [` ${symbol.ask} ${style.bold(title)}`];
|
|
490
|
-
if (opts.inline) {
|
|
491
|
-
lines.push(` ${choices
|
|
492
|
-
.map((choice, i) => i === index
|
|
493
|
-
? style.cyan(`${symbol.cursor} ${choice.label}`)
|
|
494
|
-
: ` ${choice.label}`)
|
|
495
|
-
.join(' ')}`);
|
|
496
|
-
}
|
|
497
|
-
else {
|
|
498
|
-
const width = Math.max(...choices.map((choice) => choice.label.length));
|
|
499
|
-
choices.forEach((choice, i) => {
|
|
500
|
-
const on = i === index;
|
|
501
|
-
const hint = choice.hint ? ` ${style.dim(choice.hint)}` : '';
|
|
502
|
-
const label = on ? style.bold(choice.label) : choice.label.padEnd(width);
|
|
503
|
-
lines.push(` ${on ? style.cyan(symbol.cursor) : ' '} ${label}${hint}`);
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
if (!final) {
|
|
507
|
-
lines.push(` ${style.dim(opts.inline ? '←→ move · enter confirm' : '↑↓ move · enter confirm')}`);
|
|
508
|
-
}
|
|
509
|
-
this.write(`${lines.join('\n')}\n`);
|
|
510
|
-
painted = lines.length;
|
|
511
|
-
};
|
|
512
|
-
const settle = (value) => {
|
|
513
|
-
paint(true);
|
|
514
|
-
this.input.releaseKeys();
|
|
515
|
-
return value;
|
|
516
607
|
};
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const key = await this.
|
|
520
|
-
// raw mode gone or stdin closed: keep the highlighted option
|
|
608
|
+
render();
|
|
609
|
+
while (true) {
|
|
610
|
+
const key = await this.simpleInput.readKey();
|
|
521
611
|
if (key === 'eof') {
|
|
522
|
-
|
|
523
|
-
|
|
612
|
+
// No raw mode available — fall through to numbered input
|
|
613
|
+
output.write('\n');
|
|
614
|
+
break;
|
|
524
615
|
}
|
|
525
|
-
if (key === 'ctrl-c' || key === 'escape' || key === 'ctrl-d') {
|
|
526
|
-
this.write(`\n ${style.dim('Cancelled.')}\n`);
|
|
527
|
-
if (opts.cancelValue !== undefined)
|
|
528
|
-
return settle(opts.cancelValue);
|
|
529
|
-
const cancelIndex = choices.findIndex((choice) => {
|
|
530
|
-
const value = String(choice.value);
|
|
531
|
-
return value === 'back' || value === '__back__' || value === 'exit';
|
|
532
|
-
});
|
|
533
|
-
if (cancelIndex >= 0)
|
|
534
|
-
return settle(choices[cancelIndex].value);
|
|
535
|
-
if (choices.every((choice) => typeof choice.value === 'boolean'))
|
|
536
|
-
return settle(false);
|
|
537
|
-
// sem cancel explícito: não apaga a seleção atual, mantém o menu
|
|
538
|
-
return settle(choices[index].value);
|
|
539
|
-
}
|
|
540
|
-
const token = key.length === 1 ? key.toLowerCase() : key;
|
|
541
|
-
const alias = opts.aliases?.[token];
|
|
542
|
-
if (alias !== undefined && alias < total)
|
|
543
|
-
return settle(choices[alias].value);
|
|
544
|
-
if (key === 'enter')
|
|
545
|
-
return settle(choices[index].value);
|
|
546
|
-
let next = null;
|
|
547
|
-
let wrap = false;
|
|
548
616
|
if (key === 'up' || key === 'k') {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
else if (key === 'down' || key === 'j') {
|
|
553
|
-
next = index + 1;
|
|
554
|
-
wrap = true;
|
|
555
|
-
}
|
|
556
|
-
else if (key === 'left' && opts.inline) {
|
|
557
|
-
next = index - 1;
|
|
558
|
-
wrap = true;
|
|
559
|
-
}
|
|
560
|
-
else if (key === 'right' && opts.inline) {
|
|
561
|
-
next = index + 1;
|
|
562
|
-
wrap = true;
|
|
563
|
-
}
|
|
564
|
-
else if (key === 'home')
|
|
565
|
-
next = 0;
|
|
566
|
-
else if (key === 'end')
|
|
567
|
-
next = total - 1;
|
|
568
|
-
else if (!opts.inline && total <= 9 && key >= '1' && key <= '9')
|
|
569
|
-
next = Number(key) - 1;
|
|
570
|
-
if (next === null)
|
|
617
|
+
index = (index - 1 + choices.length) % choices.length;
|
|
618
|
+
output.write('\x1b[1A\r'.repeat(choices.length + 1)); // move up
|
|
619
|
+
render();
|
|
571
620
|
continue;
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
621
|
+
}
|
|
622
|
+
if (key === 'down' || key === 'j') {
|
|
623
|
+
index = (index + 1) % choices.length;
|
|
624
|
+
output.write('\x1b[1A\r'.repeat(choices.length + 1));
|
|
625
|
+
render();
|
|
575
626
|
continue;
|
|
576
|
-
|
|
577
|
-
|
|
627
|
+
}
|
|
628
|
+
if (key === 'enter' || key === '\r' || key === '\n') {
|
|
629
|
+
output.write('\n');
|
|
630
|
+
return choices[index].value;
|
|
631
|
+
}
|
|
632
|
+
if (key === 'escape' || key === 'ctrl-c') {
|
|
633
|
+
if (opts.cancelValue !== undefined)
|
|
634
|
+
return opts.cancelValue;
|
|
635
|
+
throw new CancelError();
|
|
636
|
+
}
|
|
637
|
+
// Numbered input (1, 2, 3...)
|
|
638
|
+
const num = Number.parseInt(key, 10);
|
|
639
|
+
if (!Number.isNaN(num) && num >= 1 && num <= choices.length) {
|
|
640
|
+
output.write('\n');
|
|
641
|
+
return choices[num - 1].value;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
// Numbered menu via line input
|
|
645
|
+
const display = choices.map((c, i) => ` ${i + 1}) ${c.label}${c.hint ? style.dim(` (${c.hint})`) : ''}`).join('\n');
|
|
646
|
+
output.write(`${display}\n`);
|
|
647
|
+
while (true) {
|
|
648
|
+
const line = await this.simpleInput.readLine();
|
|
649
|
+
if (line === null) {
|
|
650
|
+
if (opts.cancelValue !== undefined)
|
|
651
|
+
return opts.cancelValue;
|
|
652
|
+
if (opts.defaultValue !== undefined)
|
|
653
|
+
return opts.defaultValue;
|
|
654
|
+
if (opts.nonInteractiveError)
|
|
655
|
+
throw new Error(opts.nonInteractiveError);
|
|
656
|
+
throw new Error(`input closed before "${title}" was answered`);
|
|
657
|
+
}
|
|
658
|
+
const answer = line.trim();
|
|
659
|
+
if (answer === '' && opts.defaultValue !== undefined)
|
|
660
|
+
return opts.defaultValue;
|
|
661
|
+
const num = Number.parseInt(answer, 10);
|
|
662
|
+
if (!Number.isNaN(num) && num >= 1 && num <= choices.length) {
|
|
663
|
+
return choices[num - 1].value;
|
|
664
|
+
}
|
|
665
|
+
const byValue = choices.find((choice) => String(choice.value) === answer);
|
|
666
|
+
if (byValue)
|
|
667
|
+
return byValue.value;
|
|
668
|
+
output.write(` ${style.red(symbol.fail)} Opção inválida\n`);
|
|
578
669
|
}
|
|
579
670
|
}
|
|
580
671
|
/** Texto livre com validação, repetindo até aceitar. */
|
|
581
672
|
async ask(title, opts = {}) {
|
|
582
|
-
|
|
583
|
-
|
|
673
|
+
if (!this.useClack) {
|
|
674
|
+
// Fallback for tests/non-TTY - simple line reading with validation
|
|
675
|
+
return this.askLineFallback(title, opts);
|
|
676
|
+
}
|
|
677
|
+
const result = await clack.text({
|
|
678
|
+
message: title,
|
|
679
|
+
placeholder: opts.placeholder,
|
|
680
|
+
initialValue: opts.default,
|
|
681
|
+
validate: Prompt.wrapValidate(opts.validate),
|
|
682
|
+
});
|
|
683
|
+
if (clack.isCancel(result)) {
|
|
584
684
|
if (opts.default !== undefined)
|
|
585
685
|
return opts.default;
|
|
586
|
-
throw new
|
|
587
|
-
}
|
|
588
|
-
const current = opts.placeholder ?? opts.default;
|
|
589
|
-
const shown = current ? style.dim(` (${current})`) : '';
|
|
590
|
-
for (;;) {
|
|
591
|
-
const suffix = opts.default !== undefined && !opts.placeholder ? style.dim(`[${opts.default}]`) : '';
|
|
592
|
-
const raw = await this.askLine(` ${symbol.ask} ${title}${shown}${suffix} `);
|
|
593
|
-
// EOF (pipe fechado / Ctrl+D): não há mais linha para ler. Re-perguntar
|
|
594
|
-
// aqui seria loop infinito (readLine devolve null para sempre). Usa o
|
|
595
|
-
// fallback se houver; senão devolve vazio e deixa o chamador decidir.
|
|
596
|
-
if (raw === null) {
|
|
597
|
-
const problem0 = opts.validate?.(fallback);
|
|
598
|
-
if (problem0) {
|
|
599
|
-
this.write(` ${style.red(symbol.fail)} ${problem0}\n`);
|
|
600
|
-
return '';
|
|
601
|
-
}
|
|
602
|
-
return fallback;
|
|
603
|
-
}
|
|
604
|
-
const blank = raw.trim() === '';
|
|
605
|
-
const value = blank ? fallback : raw.trim();
|
|
606
|
-
const problem = opts.validate?.(value);
|
|
607
|
-
if (problem) {
|
|
608
|
-
this.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
609
|
-
continue;
|
|
610
|
-
}
|
|
611
|
-
return value;
|
|
686
|
+
throw new CancelError();
|
|
612
687
|
}
|
|
688
|
+
return result;
|
|
613
689
|
}
|
|
614
|
-
/** Segredo: sem eco no TTY (mostra `*`)
|
|
690
|
+
/** Segredo: sem eco no TTY (mostra `*`). */
|
|
615
691
|
async secret(title, opts = {}) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
692
|
+
if (!this.useClack) {
|
|
693
|
+
// Fallback for tests/non-TTY - use raw mode if available (FakeTTY), else simple line
|
|
694
|
+
const inputStream = this.io.input;
|
|
695
|
+
if (canUseRawMode(inputStream) && this.simpleInput) {
|
|
696
|
+
// Use raw mode to mask input with *
|
|
697
|
+
return this.secretRawMode(title, opts);
|
|
698
|
+
}
|
|
699
|
+
// Piped stdin is real input (`echo secret | okcms config`); a closed
|
|
700
|
+
// stdin (EOF/CI) falls back to the default.
|
|
701
|
+
const fallback = opts.default ?? '';
|
|
702
|
+
const piped = await this.simpleInput.readSecret();
|
|
621
703
|
const value = !piped || piped.trim() === '' ? fallback : piped.trim();
|
|
622
704
|
const problem = opts.validate?.(value);
|
|
623
705
|
if (problem)
|
|
624
706
|
throw new Error(problem);
|
|
625
707
|
return value;
|
|
626
708
|
}
|
|
627
|
-
const
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
709
|
+
const result = await clack.password({
|
|
710
|
+
message: title,
|
|
711
|
+
mask: '*',
|
|
712
|
+
validate: Prompt.wrapValidate(opts.validate),
|
|
713
|
+
clearOnError: true,
|
|
714
|
+
});
|
|
715
|
+
if (clack.isCancel(result)) {
|
|
716
|
+
if (opts.default !== undefined)
|
|
717
|
+
return opts.default;
|
|
718
|
+
throw new CancelError();
|
|
719
|
+
}
|
|
720
|
+
return result;
|
|
721
|
+
}
|
|
722
|
+
/** Raw mode secret input for FakeTTY (masks with *). */
|
|
723
|
+
async secretRawMode(title, opts) {
|
|
724
|
+
if (!this.simpleInput)
|
|
725
|
+
throw new Error('No simpleInput available');
|
|
726
|
+
const { output } = this.io;
|
|
727
|
+
const stream = this.io.input;
|
|
728
|
+
output.write(`${title}: `);
|
|
729
|
+
stream.setRawMode?.(true);
|
|
730
|
+
this.simpleInput.raw = true;
|
|
731
|
+
let value = '';
|
|
732
|
+
while (true) {
|
|
733
|
+
const key = await this.simpleInput.readKey();
|
|
734
|
+
if (key === 'eof') {
|
|
735
|
+
output.write('\n');
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
if (key === 'enter') {
|
|
739
|
+
output.write('\n');
|
|
740
|
+
break;
|
|
741
|
+
}
|
|
742
|
+
if (key === 'backspace') {
|
|
743
|
+
if (value.length > 0) {
|
|
744
|
+
value = value.slice(0, -1);
|
|
745
|
+
// Erase last *
|
|
746
|
+
output.write('\b \b');
|
|
747
|
+
}
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (key === 'escape' || key === 'ctrl-c') {
|
|
751
|
+
output.write('\n');
|
|
752
|
+
if (opts.default !== undefined)
|
|
753
|
+
return opts.default;
|
|
754
|
+
throw new CancelError();
|
|
755
|
+
}
|
|
756
|
+
if (key.length === 1 && key >= ' ' && key <= '~') {
|
|
757
|
+
value += key;
|
|
758
|
+
output.write('*');
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
stream.setRawMode?.(false);
|
|
762
|
+
this.simpleInput.raw = false;
|
|
763
|
+
const trimmed = value.trim();
|
|
764
|
+
const problem = opts.validate?.(trimmed);
|
|
765
|
+
if (problem)
|
|
766
|
+
throw new Error(problem);
|
|
767
|
+
return trimmed || (opts.default ?? '');
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Fallback line-based input with validation for non-Clack environments.
|
|
771
|
+
*/
|
|
772
|
+
async askLineFallback(title, opts) {
|
|
773
|
+
const fallback = opts.default ?? '';
|
|
774
|
+
const displayDefault = opts.default !== undefined ? ` [${opts.default}]` : '';
|
|
775
|
+
const prompt = `${title}${displayDefault}: `;
|
|
776
|
+
while (true) {
|
|
777
|
+
this.io.output.write(prompt);
|
|
778
|
+
const line = await this.simpleInput.readLine();
|
|
779
|
+
if (line === null) {
|
|
780
|
+
if (opts.default !== undefined)
|
|
781
|
+
return opts.default;
|
|
782
|
+
throw new Error(`non-interactive: "${title}" needs a TTY or --set`);
|
|
783
|
+
}
|
|
784
|
+
const trimmed = line.trim();
|
|
785
|
+
const value = trimmed === '' ? fallback : trimmed;
|
|
786
|
+
const problem = opts.validate?.(value);
|
|
643
787
|
if (problem) {
|
|
644
|
-
this.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
788
|
+
this.io.output.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
645
789
|
continue;
|
|
646
790
|
}
|
|
647
|
-
return
|
|
791
|
+
return value;
|
|
648
792
|
}
|
|
649
793
|
}
|
|
650
794
|
/** Texto livre com validação + suporte a voltar (tecla 'b'). Retorna '__BACK__' para voltar. */
|
|
@@ -655,58 +799,105 @@ export class Prompt {
|
|
|
655
799
|
return opts.default;
|
|
656
800
|
throw new Error(`non-interactive: "${title}" needs a TTY or --set`);
|
|
657
801
|
}
|
|
802
|
+
if (!this.useClack) {
|
|
803
|
+
// Line-based fallback (tests, pipes): Enter keeps the default, 'b' goes back.
|
|
804
|
+
for (;;) {
|
|
805
|
+
const raw = await this.simpleInput.readLine();
|
|
806
|
+
if (raw === null) {
|
|
807
|
+
const problem0 = opts.validate?.(fallback);
|
|
808
|
+
if (problem0) {
|
|
809
|
+
this.write(` ${style.red(symbol.fail)} ${problem0}\n`);
|
|
810
|
+
return '__BACK__';
|
|
811
|
+
}
|
|
812
|
+
return fallback;
|
|
813
|
+
}
|
|
814
|
+
const trimmed = raw.trim();
|
|
815
|
+
if (trimmed.toLowerCase() === 'b')
|
|
816
|
+
return '__BACK__';
|
|
817
|
+
const value = trimmed === '' ? fallback : trimmed;
|
|
818
|
+
const problem = opts.validate?.(value);
|
|
819
|
+
if (problem) {
|
|
820
|
+
this.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
return value;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
658
826
|
const current = opts.placeholder ?? opts.default;
|
|
659
827
|
const shown = current ? style.dim(` (${current})`) : '';
|
|
660
828
|
for (;;) {
|
|
661
829
|
const suffix = opts.default !== undefined && !opts.placeholder ? style.dim(`[${opts.default}]`) : '';
|
|
662
830
|
const hint = style.dim(' (b=voltar)');
|
|
663
|
-
const
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
return fallback;
|
|
831
|
+
const result = await clack.text({
|
|
832
|
+
message: `${title}${shown}${suffix}${hint}`,
|
|
833
|
+
placeholder: current ? current : undefined,
|
|
834
|
+
initialValue: fallback,
|
|
835
|
+
validate: Prompt.wrapValidate(opts.validate),
|
|
836
|
+
});
|
|
837
|
+
if (clack.isCancel(result)) {
|
|
838
|
+
return '__BACK__';
|
|
672
839
|
}
|
|
673
|
-
const
|
|
840
|
+
const value = result;
|
|
841
|
+
const trimmed = value.trim();
|
|
674
842
|
// tecla de voltar
|
|
675
843
|
if (trimmed.toLowerCase() === 'b') {
|
|
676
844
|
return '__BACK__';
|
|
677
845
|
}
|
|
678
846
|
const blank = trimmed === '';
|
|
679
|
-
const
|
|
680
|
-
const problem = opts.validate?.(
|
|
847
|
+
const fieldValue = blank ? fallback : trimmed;
|
|
848
|
+
const problem = opts.validate?.(fieldValue);
|
|
681
849
|
if (problem) {
|
|
682
850
|
this.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
683
851
|
continue;
|
|
684
852
|
}
|
|
685
|
-
return
|
|
853
|
+
return fieldValue;
|
|
686
854
|
}
|
|
687
855
|
}
|
|
688
856
|
/** Segredo com suporte a voltar (digite 'b' para voltar). Retorna '__BACK__' para voltar. */
|
|
689
857
|
async secretWithBack(title, opts = {}) {
|
|
690
858
|
const fallback = opts.default ?? '';
|
|
691
859
|
if (!this.io.isTTY) {
|
|
692
|
-
|
|
860
|
+
// Piped stdin is real input (`echo secret | okcms config`)
|
|
861
|
+
const piped = await this.simpleInput.readSecret();
|
|
693
862
|
const value = !piped || piped.trim() === '' ? fallback : piped.trim();
|
|
694
863
|
const problem = opts.validate?.(value);
|
|
695
864
|
if (problem)
|
|
696
865
|
throw new Error(problem);
|
|
697
866
|
return value;
|
|
698
867
|
}
|
|
868
|
+
if (!this.useClack) {
|
|
869
|
+
// Line-based fallback (tests, pipes): Enter keeps the current value, 'b' goes back.
|
|
870
|
+
for (;;) {
|
|
871
|
+
const raw = await this.simpleInput.readSecret();
|
|
872
|
+
if (raw === null)
|
|
873
|
+
return fallback;
|
|
874
|
+
if (raw.trim().toLowerCase() === 'b')
|
|
875
|
+
return '__BACK__';
|
|
876
|
+
if (raw === '' && fallback !== '')
|
|
877
|
+
return fallback;
|
|
878
|
+
const problem = opts.validate?.(raw);
|
|
879
|
+
if (problem) {
|
|
880
|
+
this.write(` ${style.red(symbol.fail)} ${problem}\n`);
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
return raw;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
699
886
|
const current = opts.placeholder ?? (opts.default !== undefined ? '••••••' : undefined);
|
|
700
887
|
const shown = current ? style.dim(` (${current})`) : '';
|
|
701
888
|
for (;;) {
|
|
702
889
|
const suffix = opts.default !== undefined ? style.dim('[enter = keep]') : '';
|
|
703
890
|
const hint = style.dim(' (b=voltar)');
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
891
|
+
const result = await clack.password({
|
|
892
|
+
message: `${title}${shown}${suffix}${hint}`,
|
|
893
|
+
mask: '*',
|
|
894
|
+
validate: Prompt.wrapValidate(opts.validate),
|
|
895
|
+
clearOnError: true,
|
|
896
|
+
});
|
|
897
|
+
if (clack.isCancel(result)) {
|
|
898
|
+
return '__BACK__';
|
|
899
|
+
}
|
|
900
|
+
const raw = result;
|
|
710
901
|
// tecla de voltar
|
|
711
902
|
if (raw.trim().toLowerCase() === 'b')
|
|
712
903
|
return '__BACK__';
|
|
@@ -721,24 +912,65 @@ export class Prompt {
|
|
|
721
912
|
return raw;
|
|
722
913
|
}
|
|
723
914
|
}
|
|
724
|
-
/** Lê uma linha
|
|
915
|
+
/** Lê uma linha crua (também usada pelos testes). */
|
|
725
916
|
async askLine(question) {
|
|
726
917
|
if (this.closed)
|
|
727
918
|
return null;
|
|
728
919
|
if (question)
|
|
729
920
|
this.io.output.write(question);
|
|
730
|
-
|
|
921
|
+
if (!this.useClack) {
|
|
922
|
+
return this.simpleInput.readLine();
|
|
923
|
+
}
|
|
924
|
+
const result = await clack.text({
|
|
925
|
+
message: question,
|
|
926
|
+
});
|
|
927
|
+
if (clack.isCancel(result)) {
|
|
928
|
+
return null;
|
|
929
|
+
}
|
|
930
|
+
return result;
|
|
731
931
|
}
|
|
732
932
|
close() {
|
|
733
933
|
this.closed = true;
|
|
734
|
-
//
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
934
|
+
// Clack não tem ciclo de vida explícito para fechar. No-op para compatibilidade.
|
|
935
|
+
}
|
|
936
|
+
/** Configurações globais do Clack (i18n, mensagens, guide lines). */
|
|
937
|
+
static setSettings(settings) {
|
|
938
|
+
clack.updateSettings(settings);
|
|
939
|
+
}
|
|
940
|
+
/** Yes/No confirmation. Sem TTY, retorna defaultValue. */
|
|
941
|
+
async confirm(title, opts = {}) {
|
|
942
|
+
if (!this.useClack) {
|
|
943
|
+
// Fallback: read from simpleInput (line-based y/n)
|
|
944
|
+
const fallback = opts.defaultValue ?? false;
|
|
945
|
+
const hint = opts.defaultValue === undefined ? '[y/n]' : opts.defaultValue ? '[Y/n]' : '[y/N]';
|
|
946
|
+
this.io.output.write(`${title} ${style.dim(hint)}: `);
|
|
947
|
+
for (;;) {
|
|
948
|
+
const line = await this.simpleInput.readLine();
|
|
949
|
+
if (line === null)
|
|
950
|
+
return fallback;
|
|
951
|
+
const answer = line.trim().toLowerCase();
|
|
952
|
+
if (answer === '') {
|
|
953
|
+
if (opts.defaultValue !== undefined)
|
|
954
|
+
return opts.defaultValue;
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
if (answer === 'y' || answer === 'yes')
|
|
958
|
+
return true;
|
|
959
|
+
if (answer === 'n' || answer === 'no')
|
|
960
|
+
return false;
|
|
961
|
+
this.io.output.write(` ${style.red(symbol.fail)} answer y or n\n`);
|
|
962
|
+
}
|
|
738
963
|
}
|
|
739
|
-
|
|
740
|
-
|
|
964
|
+
const result = await clack.confirm({
|
|
965
|
+
message: title,
|
|
966
|
+
initialValue: opts.defaultValue,
|
|
967
|
+
});
|
|
968
|
+
if (clack.isCancel(result)) {
|
|
969
|
+
if (opts.defaultValue !== undefined)
|
|
970
|
+
return opts.defaultValue;
|
|
971
|
+
throw new CancelError();
|
|
741
972
|
}
|
|
973
|
+
return result === true;
|
|
742
974
|
}
|
|
743
975
|
}
|
|
744
976
|
// ---------------------------------------------------------------------------
|
|
@@ -756,7 +988,8 @@ export async function select(title, choices, opts = {}, io) {
|
|
|
756
988
|
export async function confirm(title, opts = {}, io) {
|
|
757
989
|
const prompt = new Prompt(io);
|
|
758
990
|
try {
|
|
759
|
-
|
|
991
|
+
const result = await prompt.confirm(title, opts);
|
|
992
|
+
return result === true;
|
|
760
993
|
}
|
|
761
994
|
finally {
|
|
762
995
|
prompt.close();
|