-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathprompter.ts
More file actions
108 lines (97 loc) · 2.64 KB
/
Copy pathprompter.ts
File metadata and controls
108 lines (97 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as clack from '@clack/prompts'
import { exitWith } from './terminal.ts'
import { isRich, theme } from './theme.ts'
const SPINNER_FRAMES = ['◐', '◓', '◑', '◒']
function guardCancel<T>(value: T | symbol): T {
if (clack.isCancel(value)) {
clack.cancel('Setup cancelled.')
exitWith(130)
}
return value as T
}
export interface SelectOption<T extends string> {
value: T
label: string
hint?: string
}
export async function select<T extends string>(params: {
message: string
options: SelectOption<T>[]
initialValue?: T
}): Promise<T> {
return guardCancel(
await clack.select({
message: isRich() ? theme.accent(params.message) : params.message,
options: params.options.map((o) => ({
...o,
hint: o.hint && isRich() ? theme.muted(o.hint) : o.hint,
})),
initialValue: params.initialValue,
})
)
}
export async function multiselect<T extends string>(params: {
message: string
options: SelectOption<T>[]
initialValues?: T[]
}): Promise<T[]> {
return guardCancel(
await clack.multiselect({
message: isRich() ? theme.accent(params.message) : params.message,
options: params.options,
initialValues: params.initialValues,
required: false,
})
)
}
export async function text(params: {
message: string
placeholder?: string
initialValue?: string
defaultValue?: string
validate?: (value: string) => string | undefined
}): Promise<string> {
return guardCancel(
await clack.text({
message: isRich() ? theme.accent(params.message) : params.message,
placeholder: params.placeholder,
initialValue: params.initialValue,
defaultValue: params.defaultValue,
validate: params.validate,
})
)
}
export async function password(params: {
message: string
validate?: (value: string) => string | undefined
}): Promise<string> {
return guardCancel(
await clack.password({
message: isRich() ? theme.accent(params.message) : params.message,
validate: params.validate,
})
)
}
export async function confirm(params: {
message: string
initialValue?: boolean
}): Promise<boolean> {
return guardCancel(
await clack.confirm({
message: isRich() ? theme.accent(params.message) : params.message,
initialValue: params.initialValue,
})
)
}
export function spinner() {
if (!isRich() || !process.stdout.isTTY) return clack.spinner()
return clack.spinner({
frames: SPINNER_FRAMES,
delay: 90,
})
}
export function note(message: string, title?: string): void {
clack.note(message, title && isRich() ? theme.heading(title) : title)
}
export const outro = clack.outro
export const log = clack.log