/
cli-utilities.ts
152 lines (138 loc) 路 3.53 KB
/
cli-utilities.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// @ts-ignore it's not worth writing a TS declaration file in this repo for a tiny module we use once like this
import termSize from "term-size";
import { error, prefix, success } from "@changesets/logger";
import { prompt } from "enquirer";
import { edit } from "external-editor";
import { symbols } from "ansi-colors";
// those types are not exported from `enquirer` so we extract them here
// so we can make type assertions using them because `enquirer` types do no support `prefix` right now
type PromptOptions = Extract<Parameters<typeof prompt>[0], { type: string }>;
type ArrayPromptOptions = Extract<
PromptOptions,
{
type:
| "autocomplete"
| "editable"
| "form"
| "multiselect"
| "select"
| "survey"
| "list"
| "scale";
}
>;
type BooleanPromptOptions = Extract<PromptOptions, { type: "confirm" }>;
type StringPromptOptions = Extract<
PromptOptions,
{ type: "input" | "invisible" | "list" | "password" | "text" }
>;
/* Notes on using inquirer:
* Each question needs a key, as inquirer is assembling an object behind-the-scenes.
* At each call, the entire responses object is returned, so we need a unique
* identifier for the name every time. This is why we are using serial IDs
*/
const serialId: () => number = (function() {
let id = 0;
return () => id++;
})();
const limit = Math.max(termSize().rows - 5, 10);
let cancelFlow = () => {
success("Cancelled... 馃憢 ");
process.exit();
};
async function askCheckboxPlus(
message: string,
choices: Array<any>,
format?: (arg: any) => any
): Promise<Array<string>> {
const name = `CheckboxPlus-${serialId()}`;
return prompt({
type: "autocomplete",
name,
message,
prefix,
multiple: true,
choices,
format,
limit,
onCancel: cancelFlow,
symbols: {
indicator: symbols.radioOff,
checked: symbols.radioOn
},
indicator(state: any, choice: any) {
return choice.enabled ? state.symbols.checked : state.symbols.indicator;
}
} as ArrayPromptOptions)
.then((responses: any) => responses[name])
.catch((err: unknown) => {
error(err);
});
}
async function askQuestion(message: string): Promise<string> {
const name = `Question-${serialId()}`;
return prompt([
{
type: "input",
message,
name,
prefix,
onCancel: cancelFlow
} as StringPromptOptions
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
error(err);
});
}
function askQuestionWithEditor(message: string): string {
const response = edit(message, { postfix: ".md" });
return response
.replace(/^#.*\n?/gm, "")
.replace(/\n+$/g, "")
.trim();
}
async function askConfirm(message: string): Promise<boolean> {
const name = `Confirm-${serialId()}`;
return prompt([
{
message,
name,
prefix,
type: "confirm",
initial: true,
onCancel: cancelFlow
} as BooleanPromptOptions
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
error(err);
});
}
async function askList<Choice extends string>(
message: string,
choices: Choice[]
): Promise<Choice> {
const name = `List-${serialId()}`;
return prompt([
{
choices,
message,
name,
prefix,
type: "select",
onCancel: cancelFlow
} as ArrayPromptOptions
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
error(err);
});
}
export {
askCheckboxPlus,
askQuestion,
askQuestionWithEditor,
askConfirm,
askList
};