|
1 | | -import { createReadStream } from "node:fs"; |
2 | | -import { createInterface } from "node:readline"; |
3 | | - |
4 | | -function getInput(): NodeJS.ReadableStream { |
5 | | - // When piped (curl | bash), stdin is consumed by the pipe. |
6 | | - // Open /dev/tty directly to read from the actual terminal. |
7 | | - if (!process.stdin.isTTY) { |
8 | | - try { |
9 | | - return createReadStream("/dev/tty"); |
10 | | - } catch { |
11 | | - // Windows or no TTY available — fall back to stdin |
12 | | - } |
| 1 | +import { |
| 2 | + confirm as clackConfirm, |
| 3 | + intro, |
| 4 | + isCancel, |
| 5 | + log, |
| 6 | + note, |
| 7 | + outro, |
| 8 | + select, |
| 9 | + spinner, |
| 10 | +} from "@clack/prompts"; |
| 11 | + |
| 12 | +export { intro, log, note, outro, spinner }; |
| 13 | + |
| 14 | +function handleCancel(value: unknown): void { |
| 15 | + if (isCancel(value)) { |
| 16 | + log.warn("Setup cancelled."); |
| 17 | + process.exit(0); |
13 | 18 | } |
14 | | - return process.stdin; |
15 | | -} |
16 | | - |
17 | | -const rl = createInterface({ input: getInput(), output: process.stdout }); |
18 | | - |
19 | | -function ask(question: string): Promise<string> { |
20 | | - return new Promise((resolve) => { |
21 | | - rl.question(question, (answer) => resolve(answer.trim())); |
22 | | - }); |
23 | | -} |
24 | | - |
25 | | -export function closePrompts(): void { |
26 | | - rl.close(); |
27 | 19 | } |
28 | 20 |
|
29 | 21 | export async function confirm(message: string, defaultYes = true): Promise<boolean> { |
30 | | - const hint = defaultYes ? "Y/n" : "y/N"; |
31 | | - const answer = await ask(` ${message} [${hint}] `); |
32 | | - if (answer === "") return defaultYes; |
33 | | - return answer.toLowerCase().startsWith("y"); |
| 22 | + const result = await clackConfirm({ |
| 23 | + message, |
| 24 | + initialValue: defaultYes, |
| 25 | + }); |
| 26 | + handleCancel(result); |
| 27 | + return result as boolean; |
34 | 28 | } |
35 | 29 |
|
36 | 30 | export async function selectOne( |
37 | 31 | message: string, |
38 | 32 | options: { label: string; value: string; recommended?: boolean }[], |
39 | 33 | ): Promise<string> { |
40 | | - console.log(` ${message}`); |
41 | | - console.log(""); |
42 | | - for (let i = 0; i < options.length; i++) { |
43 | | - const opt = options[i]; |
44 | | - const rec = opt.recommended ? " (recommended)" : ""; |
45 | | - const num = `${i + 1}`.padStart(3); |
46 | | - console.log(` ${num}) ${opt.label}${rec}`); |
47 | | - } |
48 | | - console.log(""); |
49 | | - |
50 | | - while (true) { |
51 | | - const answer = await ask(" Enter number: "); |
52 | | - const idx = Number.parseInt(answer, 10) - 1; |
53 | | - if (idx >= 0 && idx < options.length) { |
54 | | - return options[idx].value; |
55 | | - } |
56 | | - console.log(" Invalid selection, try again."); |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -export async function selectMultiple( |
61 | | - message: string, |
62 | | - options: { label: string; value: string }[], |
63 | | -): Promise<string[]> { |
64 | | - console.log(` ${message}`); |
65 | | - console.log(""); |
66 | | - for (let i = 0; i < options.length; i++) { |
67 | | - const num = `${i + 1}`.padStart(3); |
68 | | - console.log(` ${num}) ${options[i].label}`); |
69 | | - } |
70 | | - console.log(""); |
71 | | - |
72 | | - const answer = await ask(" Enter numbers (comma-separated, or 'none'): "); |
73 | | - if (answer.toLowerCase() === "none" || answer === "") return []; |
74 | | - |
75 | | - const indices = answer.split(",").map((s) => Number.parseInt(s.trim(), 10) - 1); |
76 | | - return indices.filter((i) => i >= 0 && i < options.length).map((i) => options[i].value); |
| 34 | + const result = await select({ |
| 35 | + message, |
| 36 | + options: options.map((opt) => ({ |
| 37 | + label: opt.recommended ? `${opt.label} (recommended)` : opt.label, |
| 38 | + value: opt.value, |
| 39 | + hint: opt.recommended ? "recommended" : undefined, |
| 40 | + })), |
| 41 | + }); |
| 42 | + handleCancel(result); |
| 43 | + return result as string; |
77 | 44 | } |
0 commit comments