-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcli.js
More file actions
191 lines (159 loc) · 4.45 KB
/
cli.js
File metadata and controls
191 lines (159 loc) · 4.45 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import ora from 'ora';
import chalk from 'chalk';
import * as inquirer from '@inquirer/prompts';
import { warning, error, info, success } from './figures.js';
const SPINNER_STATUS = {
SUCCESS: 'success',
FAILED: 'failed',
WARN: 'warn',
INFO: 'info'
};
const { SUCCESS, FAILED, WARN, INFO } = SPINNER_STATUS;
const QUESTION_TYPE = {
INPUT: 'input',
NUMBER: 'number',
CONFIRM: 'confirm'
};
const formatter = new Intl.ListFormat('en', { type: 'disjunction' });
function head(text, length = 11) {
return chalk.bold(text.padEnd(length));
}
export default class CLI {
constructor(stream, options = {}) {
const spinnerOptions = options.spinner ?? {};
this.stream = stream || process.stderr;
this.spinner = ora({ stream: this.stream, ...spinnerOptions });
this.SPINNER_STATUS = SPINNER_STATUS;
this.QUESTION_TYPE = QUESTION_TYPE;
this.figureIndent = ' ';
this.assumeYes = false;
}
get eolIndent() {
return `\n${this.figureIndent}`;
}
setFigureIndent(indent) {
this.figureIndent = ' '.repeat(indent);
}
async prompt(question, opts = {
defaultAnswer: true,
noSeparator: false,
questionType: 'confirm'
}) {
if (!opts.noSeparator) {
this.separator();
}
const questionType = opts.questionType || QUESTION_TYPE.CONFIRM;
const availableTypes = Object.values(QUESTION_TYPE);
if (!availableTypes.includes(questionType)) {
throw new Error(
`${questionType} must be one of ${formatter.format(availableTypes)}`);
}
const defaultAnswer = (opts.defaultAnswer === undefined) ||
opts.defaultAnswer;
if (typeof defaultAnswer === 'boolean' &&
questionType !== QUESTION_TYPE.CONFIRM) {
throw new Error(
'defaultAnswer must be provided for non-confirmation prompts');
}
const { isSpinning, text: spinningMessage } = this.spinner;
if (isSpinning) {
this.spinner.stop();
}
if (this.assumeYes) {
if (isSpinning) {
this.spinner.start(spinningMessage);
}
return defaultAnswer;
}
// eslint-disable-next-line import/namespace -- Plugin can't validate computed reference here
const answer = await inquirer[questionType]({
message: question,
default: defaultAnswer
});
if (isSpinning) {
this.spinner.start(spinningMessage);
}
return answer;
}
setAssumeYes() {
this.assumeYes = true;
}
startSpinner(text) {
this.spinner.text = text;
this.spinner.start();
}
updateSpinner(text) {
this.spinner.text = text;
}
stopSpinner(rawText, status = SUCCESS) {
let symbol;
switch (status) {
case SUCCESS:
symbol = success;
break;
case FAILED:
symbol = error;
break;
case WARN:
symbol = warning;
break;
case INFO:
symbol = info;
}
const text = ' ' + rawText;
this.spinner.stopAndPersist({
symbol, text
});
}
write(text) {
this.stream.write(text);
}
log(text) {
this.write(text + '\n');
}
table(first, second = '', length = 11) {
this.log(head(first, length) + second);
}
separator(text = '', length = 80, sep = '-') {
if (!text) {
this.log(sep.repeat(length));
return;
}
const rest = (length - text.length - 2);
const half = sep.repeat(Math.abs(Math.floor(rest / 2)));
if (rest % 2 === 0) {
this.log(`${half} ${chalk.bold(text)} ${half}`);
} else {
this.log(`${half} ${chalk.bold(text)} ${sep}${half}`);
}
}
ok(text, options = {}) {
const prefix = options.newline ? this.eolIndent : this.figureIndent;
this.log(`${prefix}${success} ${text}`);
}
warn(text, options = {}) {
const prefix = options.newline ? this.eolIndent : this.figureIndent;
this.log(prefix + chalk.bold(`${warning} ${text}`));
}
info(text, options = {}) {
const prefix = options.newline ? this.eolIndent : this.figureIndent;
this.log(`${prefix}${info} ${text}`);
}
error(obj, options = {}) {
const prefix = options.newline ? this.eolIndent : this.figureIndent;
if (obj instanceof Error) {
this.log(`${prefix}${error} ${obj.message}`);
this.log(obj.stack);
if (obj.data) {
this.log(JSON.stringify(obj.data, null, 2));
}
} else {
this.log(prefix + chalk.bold(`${error} ${obj}`));
}
}
setExitCode(statusCode) {
process.exitCode = statusCode;
}
};
CLI.SPINNER_STATUS = SPINNER_STATUS;
CLI.QUESTION_TYPE = QUESTION_TYPE;