@reliverse/rempts 1.6.0
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/LICENSE +21 -0
- package/README.md +408 -0
- package/bin/core-impl/anykey/anykey-mod.d.ts +12 -0
- package/bin/core-impl/anykey/anykey-mod.js +125 -0
- package/bin/core-impl/date/date.d.ts +2 -0
- package/bin/core-impl/date/date.js +236 -0
- package/bin/core-impl/editor/editor-mod.d.ts +25 -0
- package/bin/core-impl/editor/editor-mod.js +897 -0
- package/bin/core-impl/figures/figures-mod.d.ts +462 -0
- package/bin/core-impl/figures/figures-mod.js +286 -0
- package/bin/core-impl/figures/figures.test.d.ts +1 -0
- package/bin/core-impl/figures/figures.test.js +474 -0
- package/bin/core-impl/input/confirm-prompt.d.ts +5 -0
- package/bin/core-impl/input/confirm-prompt.js +173 -0
- package/bin/core-impl/input/input-prompt.d.ts +16 -0
- package/bin/core-impl/input/input-prompt.js +370 -0
- package/bin/core-impl/launcher/deprecated/_parser.ts.txt +167 -0
- package/bin/core-impl/launcher/deprecated/_utils.ts.txt +41 -0
- package/bin/core-impl/launcher/deprecated/args.ts.txt +108 -0
- package/bin/core-impl/launcher/deprecated/command.ts.txt +95 -0
- package/bin/core-impl/launcher/deprecated/launcher-mod.ts.txt +50 -0
- package/bin/core-impl/launcher/deprecated/usage.ts.txt +157 -0
- package/bin/core-impl/launcher/launcher-mod.d.ts +87 -0
- package/bin/core-impl/launcher/launcher-mod.js +364 -0
- package/bin/core-impl/msg-fmt/colors.d.ts +30 -0
- package/bin/core-impl/msg-fmt/colors.js +42 -0
- package/bin/core-impl/msg-fmt/logger.d.ts +17 -0
- package/bin/core-impl/msg-fmt/logger.js +106 -0
- package/bin/core-impl/msg-fmt/mapping.d.ts +3 -0
- package/bin/core-impl/msg-fmt/mapping.js +49 -0
- package/bin/core-impl/msg-fmt/messages.d.ts +35 -0
- package/bin/core-impl/msg-fmt/messages.js +316 -0
- package/bin/core-impl/msg-fmt/terminal.d.ts +15 -0
- package/bin/core-impl/msg-fmt/terminal.js +60 -0
- package/bin/core-impl/msg-fmt/variants.d.ts +11 -0
- package/bin/core-impl/msg-fmt/variants.js +52 -0
- package/bin/core-impl/next-steps/next-steps.d.ts +14 -0
- package/bin/core-impl/next-steps/next-steps.js +24 -0
- package/bin/core-impl/number/number-mod.d.ts +28 -0
- package/bin/core-impl/number/number-mod.js +197 -0
- package/bin/core-impl/results/results.d.ts +7 -0
- package/bin/core-impl/results/results.js +27 -0
- package/bin/core-impl/select/multiselect-prompt.d.ts +2 -0
- package/bin/core-impl/select/multiselect-prompt.js +342 -0
- package/bin/core-impl/select/nummultiselect-prompt.d.ts +6 -0
- package/bin/core-impl/select/nummultiselect-prompt.js +105 -0
- package/bin/core-impl/select/numselect-prompt.d.ts +7 -0
- package/bin/core-impl/select/numselect-prompt.js +115 -0
- package/bin/core-impl/select/select-prompt.d.ts +33 -0
- package/bin/core-impl/select/select-prompt.js +303 -0
- package/bin/core-impl/select/toggle-prompt.d.ts +5 -0
- package/bin/core-impl/select/toggle-prompt.js +209 -0
- package/bin/core-impl/st-end/end.d.ts +2 -0
- package/bin/core-impl/st-end/end.js +42 -0
- package/bin/core-impl/st-end/start.d.ts +17 -0
- package/bin/core-impl/st-end/start.js +67 -0
- package/bin/core-impl/task/progress.d.ts +2 -0
- package/bin/core-impl/task/progress.js +57 -0
- package/bin/core-impl/task/spinner.d.ts +15 -0
- package/bin/core-impl/task/spinner.js +110 -0
- package/bin/core-impl/utils/colorize.d.ts +2 -0
- package/bin/core-impl/utils/colorize.js +135 -0
- package/bin/core-impl/utils/errors.d.ts +1 -0
- package/bin/core-impl/utils/errors.js +17 -0
- package/bin/core-impl/utils/prevent.d.ts +8 -0
- package/bin/core-impl/utils/prevent.js +65 -0
- package/bin/core-impl/utils/prompt-end.d.ts +8 -0
- package/bin/core-impl/utils/prompt-end.js +34 -0
- package/bin/core-impl/utils/stream-text.d.ts +18 -0
- package/bin/core-impl/utils/stream-text.js +136 -0
- package/bin/core-impl/utils/system.d.ts +6 -0
- package/bin/core-impl/utils/system.js +7 -0
- package/bin/core-impl/utils/validate.d.ts +21 -0
- package/bin/core-impl/utils/validate.js +17 -0
- package/bin/core-impl/visual/animate/animate.d.ts +14 -0
- package/bin/core-impl/visual/animate/animate.js +65 -0
- package/bin/core-impl/visual/ascii-art/ascii-art.d.ts +6 -0
- package/bin/core-impl/visual/ascii-art/ascii-art.js +13 -0
- package/bin/core-types.d.ts +334 -0
- package/bin/core-types.js +0 -0
- package/bin/main.d.ts +36 -0
- package/bin/main.js +86 -0
- package/package.json +58 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { re } from "@reliverse/relico";
|
|
2
|
+
import { isUnicodeSupported } from "@reliverse/runtime";
|
|
3
|
+
import readline from "node:readline/promises";
|
|
4
|
+
import {
|
|
5
|
+
bar,
|
|
6
|
+
msg,
|
|
7
|
+
msgUndoAll
|
|
8
|
+
} from "../msg-fmt/messages.js";
|
|
9
|
+
import { deleteLastLine } from "../msg-fmt/terminal.js";
|
|
10
|
+
import { completePrompt } from "../utils/prompt-end.js";
|
|
11
|
+
import { streamText } from "../utils/stream-text.js";
|
|
12
|
+
const unicode = isUnicodeSupported();
|
|
13
|
+
const S_MASK = unicode ? "\u258B" : "*";
|
|
14
|
+
function getMaskChar(customMask) {
|
|
15
|
+
if (!unicode) return "*";
|
|
16
|
+
return customMask ?? S_MASK;
|
|
17
|
+
}
|
|
18
|
+
async function ask(terminal, prompt, mode, mask) {
|
|
19
|
+
if (mode === "password") {
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
let buffer = "";
|
|
22
|
+
const maskChar = getMaskChar(mask);
|
|
23
|
+
process.stdout.write(prompt);
|
|
24
|
+
const onData = (data) => {
|
|
25
|
+
const str = data.toString("utf-8");
|
|
26
|
+
for (const char of str) {
|
|
27
|
+
if (char === "\n" || char === "\r") {
|
|
28
|
+
process.stdout.write("\n");
|
|
29
|
+
cleanup();
|
|
30
|
+
resolve(buffer);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (char === "") {
|
|
34
|
+
cleanup();
|
|
35
|
+
resolve(null);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (char === "\x7F" || char === "\b") {
|
|
39
|
+
if (buffer.length > 0) {
|
|
40
|
+
buffer = buffer.slice(0, -1);
|
|
41
|
+
}
|
|
42
|
+
redrawPrompt(buffer, prompt);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
buffer += char;
|
|
46
|
+
redrawPrompt(buffer, prompt);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
process.stdin.on("data", onData);
|
|
50
|
+
const cleanup = () => {
|
|
51
|
+
process.stdin.removeListener("data", onData);
|
|
52
|
+
};
|
|
53
|
+
const redrawPrompt = (maskedBuffer, textPrompt) => {
|
|
54
|
+
process.stdout.clearLine(0);
|
|
55
|
+
process.stdout.cursorTo(0);
|
|
56
|
+
process.stdout.write(textPrompt + maskChar.repeat(maskedBuffer.length));
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return terminal.question(prompt);
|
|
61
|
+
}
|
|
62
|
+
function renderPromptUI(params) {
|
|
63
|
+
const {
|
|
64
|
+
title,
|
|
65
|
+
hint = "",
|
|
66
|
+
hintPlaceholderColor = "blue",
|
|
67
|
+
content = "",
|
|
68
|
+
contentColor = "dim",
|
|
69
|
+
contentTypography = "italic",
|
|
70
|
+
contentVariant = "none",
|
|
71
|
+
titleColor = "cyan",
|
|
72
|
+
titleTypography = "none",
|
|
73
|
+
titleVariant = "none",
|
|
74
|
+
borderColor = "dim",
|
|
75
|
+
placeholder = "",
|
|
76
|
+
userInput,
|
|
77
|
+
errorMessage,
|
|
78
|
+
symbol = "step_active",
|
|
79
|
+
customSymbol = "",
|
|
80
|
+
symbolColor = "cyan",
|
|
81
|
+
shouldStream = false,
|
|
82
|
+
streamDelay = 30
|
|
83
|
+
} = params;
|
|
84
|
+
const type = errorMessage !== "" ? "M_ERROR" : "M_GENERAL";
|
|
85
|
+
if (shouldStream) {
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
msg({
|
|
88
|
+
type,
|
|
89
|
+
title: "",
|
|
90
|
+
titleColor,
|
|
91
|
+
titleTypography,
|
|
92
|
+
titleVariant,
|
|
93
|
+
content: "",
|
|
94
|
+
contentColor,
|
|
95
|
+
contentTypography,
|
|
96
|
+
contentVariant,
|
|
97
|
+
borderColor,
|
|
98
|
+
hint,
|
|
99
|
+
hintPlaceholderColor,
|
|
100
|
+
placeholder: userInput === "" ? placeholder : "",
|
|
101
|
+
errorMessage,
|
|
102
|
+
symbol,
|
|
103
|
+
customSymbol,
|
|
104
|
+
symbolColor
|
|
105
|
+
});
|
|
106
|
+
void streamText({
|
|
107
|
+
text: title,
|
|
108
|
+
delay: streamDelay,
|
|
109
|
+
color: titleColor,
|
|
110
|
+
newline: false
|
|
111
|
+
}).then(async () => {
|
|
112
|
+
msgUndoAll();
|
|
113
|
+
msg({
|
|
114
|
+
type,
|
|
115
|
+
title,
|
|
116
|
+
titleColor,
|
|
117
|
+
titleTypography,
|
|
118
|
+
titleVariant,
|
|
119
|
+
content: "",
|
|
120
|
+
contentColor,
|
|
121
|
+
contentTypography,
|
|
122
|
+
contentVariant,
|
|
123
|
+
borderColor,
|
|
124
|
+
hint,
|
|
125
|
+
hintPlaceholderColor,
|
|
126
|
+
placeholder: userInput === "" ? placeholder : "",
|
|
127
|
+
errorMessage,
|
|
128
|
+
symbol,
|
|
129
|
+
customSymbol,
|
|
130
|
+
symbolColor
|
|
131
|
+
});
|
|
132
|
+
if (content) {
|
|
133
|
+
await streamText({
|
|
134
|
+
text: content,
|
|
135
|
+
delay: streamDelay,
|
|
136
|
+
color: contentColor,
|
|
137
|
+
newline: false
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
msgUndoAll();
|
|
141
|
+
deleteLastLine();
|
|
142
|
+
msg({
|
|
143
|
+
type,
|
|
144
|
+
title,
|
|
145
|
+
titleColor,
|
|
146
|
+
titleTypography,
|
|
147
|
+
titleVariant,
|
|
148
|
+
content,
|
|
149
|
+
contentColor,
|
|
150
|
+
contentTypography,
|
|
151
|
+
contentVariant,
|
|
152
|
+
borderColor,
|
|
153
|
+
hint,
|
|
154
|
+
hintPlaceholderColor,
|
|
155
|
+
placeholder: userInput === "" ? placeholder : "",
|
|
156
|
+
errorMessage,
|
|
157
|
+
symbol,
|
|
158
|
+
customSymbol,
|
|
159
|
+
symbolColor
|
|
160
|
+
});
|
|
161
|
+
resolve();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
msg({
|
|
166
|
+
type,
|
|
167
|
+
title,
|
|
168
|
+
titleColor,
|
|
169
|
+
titleTypography,
|
|
170
|
+
titleVariant,
|
|
171
|
+
content,
|
|
172
|
+
contentColor,
|
|
173
|
+
contentTypography,
|
|
174
|
+
contentVariant,
|
|
175
|
+
borderColor,
|
|
176
|
+
hint,
|
|
177
|
+
hintPlaceholderColor,
|
|
178
|
+
placeholder: userInput === "" ? placeholder : "",
|
|
179
|
+
errorMessage,
|
|
180
|
+
symbol,
|
|
181
|
+
customSymbol,
|
|
182
|
+
symbolColor
|
|
183
|
+
});
|
|
184
|
+
if (userInput !== "") {
|
|
185
|
+
msg({ type: "M_MIDDLE", title: ` ${userInput}` });
|
|
186
|
+
}
|
|
187
|
+
return Promise.resolve();
|
|
188
|
+
}
|
|
189
|
+
async function validateInput(input, validate) {
|
|
190
|
+
let isValid = true;
|
|
191
|
+
let errorMessage = "";
|
|
192
|
+
if (validate && isValid) {
|
|
193
|
+
const validationResult = await validate(input);
|
|
194
|
+
if (typeof validationResult === "string") {
|
|
195
|
+
isValid = false;
|
|
196
|
+
errorMessage = validationResult;
|
|
197
|
+
} else if (validationResult === false) {
|
|
198
|
+
isValid = false;
|
|
199
|
+
errorMessage = "Invalid input.";
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return { isValid, errorMessage };
|
|
203
|
+
}
|
|
204
|
+
export async function inputPrompt(options) {
|
|
205
|
+
const {
|
|
206
|
+
title,
|
|
207
|
+
hint,
|
|
208
|
+
hintPlaceholderColor = "blue",
|
|
209
|
+
validate,
|
|
210
|
+
defaultValue = "",
|
|
211
|
+
titleColor = "cyan",
|
|
212
|
+
titleTypography = "none",
|
|
213
|
+
titleVariant = "none",
|
|
214
|
+
content,
|
|
215
|
+
contentColor = "dim",
|
|
216
|
+
contentTypography = "italic",
|
|
217
|
+
contentVariant = "none",
|
|
218
|
+
borderColor = "dim",
|
|
219
|
+
placeholder,
|
|
220
|
+
hardcoded,
|
|
221
|
+
endTitle = "",
|
|
222
|
+
endTitleColor = "dim",
|
|
223
|
+
border = true,
|
|
224
|
+
symbol,
|
|
225
|
+
customSymbol,
|
|
226
|
+
symbolColor,
|
|
227
|
+
mode = "plain",
|
|
228
|
+
mask,
|
|
229
|
+
shouldStream = false,
|
|
230
|
+
streamDelay = 20
|
|
231
|
+
} = options;
|
|
232
|
+
const terminal = readline.createInterface({
|
|
233
|
+
input: process.stdin,
|
|
234
|
+
output: process.stdout
|
|
235
|
+
});
|
|
236
|
+
async function endPrompt(isCtrlC) {
|
|
237
|
+
await completePrompt(
|
|
238
|
+
"input",
|
|
239
|
+
isCtrlC,
|
|
240
|
+
endTitle,
|
|
241
|
+
endTitleColor,
|
|
242
|
+
titleTypography,
|
|
243
|
+
titleVariant,
|
|
244
|
+
border,
|
|
245
|
+
borderColor,
|
|
246
|
+
void 0,
|
|
247
|
+
false
|
|
248
|
+
);
|
|
249
|
+
terminal.close();
|
|
250
|
+
if (isCtrlC) {
|
|
251
|
+
process.exit(0);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
terminal.on("SIGINT", () => {
|
|
255
|
+
void endPrompt(true);
|
|
256
|
+
});
|
|
257
|
+
let currentInput = hardcoded?.userInput ?? "";
|
|
258
|
+
let errorMessage = hardcoded?.errorMessage ?? "";
|
|
259
|
+
let showPlaceholder = hardcoded?.showPlaceholder ?? true;
|
|
260
|
+
let isRerender = false;
|
|
261
|
+
async function handleHardcodedInput() {
|
|
262
|
+
msgUndoAll();
|
|
263
|
+
await renderPromptUI({
|
|
264
|
+
title,
|
|
265
|
+
hint,
|
|
266
|
+
hintPlaceholderColor,
|
|
267
|
+
content,
|
|
268
|
+
contentColor,
|
|
269
|
+
contentTypography,
|
|
270
|
+
contentVariant,
|
|
271
|
+
titleColor,
|
|
272
|
+
titleTypography,
|
|
273
|
+
titleVariant,
|
|
274
|
+
borderColor,
|
|
275
|
+
placeholder: showPlaceholder ? placeholder : "",
|
|
276
|
+
userInput: currentInput,
|
|
277
|
+
errorMessage,
|
|
278
|
+
border,
|
|
279
|
+
symbol,
|
|
280
|
+
customSymbol,
|
|
281
|
+
symbolColor,
|
|
282
|
+
mask,
|
|
283
|
+
shouldStream,
|
|
284
|
+
streamDelay
|
|
285
|
+
});
|
|
286
|
+
const finalAnswer = currentInput || defaultValue;
|
|
287
|
+
const validated = await validateInput(finalAnswer, validate);
|
|
288
|
+
if (!validated.isValid) {
|
|
289
|
+
terminal.close();
|
|
290
|
+
throw new Error(validated.errorMessage || "Invalid input.");
|
|
291
|
+
}
|
|
292
|
+
msg({ type: "M_MIDDLE", title: ` ${finalAnswer}` });
|
|
293
|
+
msg({ type: "M_BAR", borderColor });
|
|
294
|
+
terminal.close();
|
|
295
|
+
return finalAnswer;
|
|
296
|
+
}
|
|
297
|
+
if (hardcoded?.userInput !== void 0) {
|
|
298
|
+
return handleHardcodedInput();
|
|
299
|
+
}
|
|
300
|
+
while (true) {
|
|
301
|
+
if (isRerender) {
|
|
302
|
+
msgUndoAll();
|
|
303
|
+
}
|
|
304
|
+
const displayedUserInput = mode === "password" ? getMaskChar(mask).repeat(currentInput.length) : currentInput;
|
|
305
|
+
if (errorMessage) {
|
|
306
|
+
deleteLastLine();
|
|
307
|
+
deleteLastLine();
|
|
308
|
+
}
|
|
309
|
+
await renderPromptUI({
|
|
310
|
+
title,
|
|
311
|
+
hint,
|
|
312
|
+
hintPlaceholderColor,
|
|
313
|
+
content,
|
|
314
|
+
contentColor,
|
|
315
|
+
contentTypography,
|
|
316
|
+
contentVariant,
|
|
317
|
+
titleColor,
|
|
318
|
+
titleTypography,
|
|
319
|
+
titleVariant,
|
|
320
|
+
borderColor,
|
|
321
|
+
placeholder: showPlaceholder ? placeholder : "",
|
|
322
|
+
userInput: displayedUserInput,
|
|
323
|
+
errorMessage,
|
|
324
|
+
border,
|
|
325
|
+
symbol,
|
|
326
|
+
customSymbol,
|
|
327
|
+
symbolColor,
|
|
328
|
+
mask,
|
|
329
|
+
shouldStream,
|
|
330
|
+
streamDelay
|
|
331
|
+
});
|
|
332
|
+
if (errorMessage) {
|
|
333
|
+
deleteLastLine();
|
|
334
|
+
}
|
|
335
|
+
const formattedBar = bar({ borderColor });
|
|
336
|
+
const userInputRaw = await ask(terminal, `${formattedBar} `, mode, mask);
|
|
337
|
+
isRerender = true;
|
|
338
|
+
if (userInputRaw === null) {
|
|
339
|
+
return "";
|
|
340
|
+
}
|
|
341
|
+
currentInput = userInputRaw.trim();
|
|
342
|
+
if (showPlaceholder && currentInput !== "") {
|
|
343
|
+
showPlaceholder = false;
|
|
344
|
+
}
|
|
345
|
+
const finalAnswer = currentInput || defaultValue;
|
|
346
|
+
const validated = await validateInput(finalAnswer, validate);
|
|
347
|
+
if (validated.isValid) {
|
|
348
|
+
if (!currentInput && defaultValue) {
|
|
349
|
+
if (mode === "password") {
|
|
350
|
+
deleteLastLine();
|
|
351
|
+
deleteLastLine();
|
|
352
|
+
msg({
|
|
353
|
+
type: "M_MIDDLE",
|
|
354
|
+
title: ` ${getMaskChar(mask).repeat(defaultValue.length)}`
|
|
355
|
+
});
|
|
356
|
+
} else {
|
|
357
|
+
deleteLastLine();
|
|
358
|
+
msg({ type: "M_MIDDLE", title: ` ${re.reset(defaultValue)}` });
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (errorMessage) {
|
|
362
|
+
deleteLastLine();
|
|
363
|
+
}
|
|
364
|
+
msg({ type: "M_BAR", borderColor });
|
|
365
|
+
terminal.close();
|
|
366
|
+
return finalAnswer;
|
|
367
|
+
}
|
|
368
|
+
errorMessage = validated.errorMessage;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Default,
|
|
3
|
+
ParserArgv,
|
|
4
|
+
ParserOptions,
|
|
5
|
+
} from "~/libs/core/core-types.js";
|
|
6
|
+
|
|
7
|
+
function toArr(any: any) {
|
|
8
|
+
// biome-ignore lint/suspicious/noDoubleEquals: <explanation>
|
|
9
|
+
return any == undefined ? [] : Array.isArray(any) ? any : [any];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function toVal(out, key, val, opts) {
|
|
13
|
+
let x;
|
|
14
|
+
const old = out[key];
|
|
15
|
+
const nxt = ~opts.string.indexOf(key)
|
|
16
|
+
? // biome-ignore lint/suspicious/noDoubleEquals: <explanation>
|
|
17
|
+
val == undefined || val === true
|
|
18
|
+
? ""
|
|
19
|
+
: String(val)
|
|
20
|
+
: typeof val === "boolean"
|
|
21
|
+
? val
|
|
22
|
+
: ~opts.boolean.indexOf(key)
|
|
23
|
+
? val === "false"
|
|
24
|
+
? false
|
|
25
|
+
: val === "true" ||
|
|
26
|
+
// biome-ignore lint/style/noCommaOperator: <explanation>
|
|
27
|
+
(out._.push(((x = +val), x * 0 === 0) ? x : val), !!val)
|
|
28
|
+
: // biome-ignore lint/style/noCommaOperator: <explanation>
|
|
29
|
+
((x = +val), x * 0 === 0)
|
|
30
|
+
? x
|
|
31
|
+
: val;
|
|
32
|
+
out[key] =
|
|
33
|
+
// biome-ignore lint/suspicious/noDoubleEquals: <explanation>
|
|
34
|
+
old == undefined ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function parseRawArgs<T = Default>(
|
|
38
|
+
args: string[] = [],
|
|
39
|
+
opts: ParserOptions = {},
|
|
40
|
+
): ParserArgv<T> {
|
|
41
|
+
let k;
|
|
42
|
+
let arr;
|
|
43
|
+
let arg;
|
|
44
|
+
let name;
|
|
45
|
+
let val;
|
|
46
|
+
const out = { _: [] };
|
|
47
|
+
let i = 0;
|
|
48
|
+
let j = 0;
|
|
49
|
+
let idx = 0;
|
|
50
|
+
const len = args.length;
|
|
51
|
+
|
|
52
|
+
const alibi = opts.alias !== void 0;
|
|
53
|
+
const strict = opts.unknown !== void 0;
|
|
54
|
+
const defaults = opts.default !== void 0;
|
|
55
|
+
|
|
56
|
+
opts.alias = opts.alias || {};
|
|
57
|
+
opts.string = toArr(opts.string);
|
|
58
|
+
opts.boolean = toArr(opts.boolean);
|
|
59
|
+
|
|
60
|
+
if (alibi) {
|
|
61
|
+
for (k in opts.alias) {
|
|
62
|
+
arr = opts.alias[k] = toArr(opts.alias[k]);
|
|
63
|
+
for (i = 0; i < arr.length; i++) {
|
|
64
|
+
(opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (i = opts.boolean.length; i-- > 0; ) {
|
|
70
|
+
arr = opts.alias[opts.boolean[i]] || [];
|
|
71
|
+
for (j = arr.length; j-- > 0; ) {
|
|
72
|
+
opts.boolean.push(arr[j]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (i = opts.string.length; i-- > 0; ) {
|
|
77
|
+
arr = opts.alias[opts.string[i]] || [];
|
|
78
|
+
for (j = arr.length; j-- > 0; ) {
|
|
79
|
+
opts.string.push(arr[j]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (defaults) {
|
|
84
|
+
for (k in opts.default) {
|
|
85
|
+
name = typeof opts.default[k];
|
|
86
|
+
arr = opts.alias[k] = opts.alias[k] || [];
|
|
87
|
+
if (opts[name] !== void 0) {
|
|
88
|
+
opts[name].push(k);
|
|
89
|
+
for (i = 0; i < arr.length; i++) {
|
|
90
|
+
opts[name].push(arr[i]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const keys = strict ? Object.keys(opts.alias) : [];
|
|
97
|
+
|
|
98
|
+
for (i = 0; i < len; i++) {
|
|
99
|
+
arg = args[i];
|
|
100
|
+
|
|
101
|
+
if (arg === "--") {
|
|
102
|
+
out._ = out._.concat(args.slice(++i));
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (j = 0; j < arg.length; j++) {
|
|
107
|
+
if (arg.charCodeAt(j) !== 45) {
|
|
108
|
+
break;
|
|
109
|
+
} // "-"
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (j === 0) {
|
|
113
|
+
out._.push(arg);
|
|
114
|
+
} else if (arg.substring(j, j + 3) === "no-") {
|
|
115
|
+
name = arg.slice(Math.max(0, j + 3));
|
|
116
|
+
if (strict && !~keys.indexOf(name)) {
|
|
117
|
+
// @ts-expect-error TODO: fix ts
|
|
118
|
+
return opts.unknown(arg);
|
|
119
|
+
}
|
|
120
|
+
out[name] = false;
|
|
121
|
+
} else {
|
|
122
|
+
for (idx = j + 1; idx < arg.length; idx++) {
|
|
123
|
+
if (arg.charCodeAt(idx) === 61) {
|
|
124
|
+
break;
|
|
125
|
+
} // "="
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
name = arg.substring(j, idx);
|
|
129
|
+
val =
|
|
130
|
+
arg.slice(Math.max(0, ++idx)) ||
|
|
131
|
+
i + 1 === len ||
|
|
132
|
+
// biome-ignore lint/style/useTemplate: <explanation>
|
|
133
|
+
("" + args[i + 1]).charCodeAt(0) === 45 ||
|
|
134
|
+
args[++i];
|
|
135
|
+
arr = j === 2 ? [name] : name;
|
|
136
|
+
|
|
137
|
+
for (idx = 0; idx < arr.length; idx++) {
|
|
138
|
+
name = arr[idx];
|
|
139
|
+
if (strict && !~keys.indexOf(name)) {
|
|
140
|
+
// @ts-expect-error TODO: fix ts
|
|
141
|
+
return opts.unknown("-".repeat(j) + name);
|
|
142
|
+
}
|
|
143
|
+
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (defaults) {
|
|
149
|
+
for (k in opts.default) {
|
|
150
|
+
if (out[k] === void 0) {
|
|
151
|
+
out[k] = opts.default[k];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (alibi) {
|
|
157
|
+
for (k in out) {
|
|
158
|
+
arr = opts.alias[k] || [];
|
|
159
|
+
while (arr.length > 0) {
|
|
160
|
+
out[arr.shift()] = out[k];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// @ts-expect-error TODO: fix ts
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Resolvable } from "~/libs/core/core-types.js";
|
|
2
|
+
|
|
3
|
+
export function toArray(val: any) {
|
|
4
|
+
if (Array.isArray(val)) {
|
|
5
|
+
return val;
|
|
6
|
+
}
|
|
7
|
+
return val === undefined ? [] : [val];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function formatLineColumns(lines: string[][], linePrefix = "") {
|
|
11
|
+
const maxLength: number[] = [];
|
|
12
|
+
for (const line of lines) {
|
|
13
|
+
for (const [i, element] of line.entries()) {
|
|
14
|
+
maxLength[i] = Math.max(maxLength[i] || 0, element.length);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return lines
|
|
18
|
+
.map((l) =>
|
|
19
|
+
l
|
|
20
|
+
.map(
|
|
21
|
+
(c, i) =>
|
|
22
|
+
linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLength[i]),
|
|
23
|
+
)
|
|
24
|
+
.join(" "),
|
|
25
|
+
)
|
|
26
|
+
.join("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function resolveValue<T>(input: Resolvable<T>): T | Promise<T> {
|
|
30
|
+
return typeof input === "function" ? (input as any)() : input;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class CLIError extends Error {
|
|
34
|
+
constructor(
|
|
35
|
+
message: string,
|
|
36
|
+
public code?: string,
|
|
37
|
+
) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "CLIError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { kebabCase, camelCase } from "scule";
|
|
2
|
+
|
|
3
|
+
import type { Arg, ArgsDef, ParsedArgs } from "~/libs/core/core-types.js";
|
|
4
|
+
|
|
5
|
+
import { parseRawArgs } from "./_parser.js";
|
|
6
|
+
import { CLIError, toArray } from "./_utils.js";
|
|
7
|
+
|
|
8
|
+
export function parseArgs<T extends ArgsDef = ArgsDef>(
|
|
9
|
+
rawArgs: string[],
|
|
10
|
+
argsDef: ArgsDef,
|
|
11
|
+
): ParsedArgs<T> {
|
|
12
|
+
const parseOptions = {
|
|
13
|
+
boolean: [] as string[],
|
|
14
|
+
string: [] as string[],
|
|
15
|
+
number: [] as string[],
|
|
16
|
+
enum: [] as (number | string)[],
|
|
17
|
+
mixed: [] as string[],
|
|
18
|
+
alias: {} as Record<string, string | string[]>,
|
|
19
|
+
default: {} as Record<string, boolean | number | string>,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const args = resolveArgs(argsDef);
|
|
23
|
+
|
|
24
|
+
for (const arg of args) {
|
|
25
|
+
if (arg.type === "positional") {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (arg.type === "string" || arg.type === "number") {
|
|
29
|
+
parseOptions.string.push(arg.name);
|
|
30
|
+
} else if (arg.type === "boolean") {
|
|
31
|
+
parseOptions.boolean.push(arg.name);
|
|
32
|
+
} else if (arg.type === "enum") {
|
|
33
|
+
parseOptions.enum.push(...(arg.options || []));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (arg.default !== undefined) {
|
|
37
|
+
parseOptions.default[arg.name] = arg.default;
|
|
38
|
+
}
|
|
39
|
+
if (arg.alias) {
|
|
40
|
+
parseOptions.alias[arg.name] = arg.alias;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const parsed = parseRawArgs(rawArgs, parseOptions);
|
|
45
|
+
const [...positionalArguments] = parsed._;
|
|
46
|
+
|
|
47
|
+
const parsedArgsProxy = new Proxy(parsed, {
|
|
48
|
+
get(target: ParsedArgs<any>, prop: string) {
|
|
49
|
+
return target[prop] ?? target[camelCase(prop)] ?? target[kebabCase(prop)];
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
for (const [, arg] of args.entries()) {
|
|
54
|
+
if (arg.type === "positional") {
|
|
55
|
+
const nextPositionalArgument = positionalArguments.shift();
|
|
56
|
+
if (nextPositionalArgument !== undefined) {
|
|
57
|
+
parsedArgsProxy[arg.name] = nextPositionalArgument;
|
|
58
|
+
} else if (arg.default === undefined && arg.required !== false) {
|
|
59
|
+
throw new CLIError(
|
|
60
|
+
`Missing required positional argument: ${arg.name.toUpperCase()}`,
|
|
61
|
+
"EARG",
|
|
62
|
+
);
|
|
63
|
+
} else {
|
|
64
|
+
parsedArgsProxy[arg.name] = arg.default;
|
|
65
|
+
}
|
|
66
|
+
} else if (arg.type === "enum") {
|
|
67
|
+
const argument = parsedArgsProxy[arg.name];
|
|
68
|
+
const options = arg.options || [];
|
|
69
|
+
if (
|
|
70
|
+
argument !== undefined &&
|
|
71
|
+
options.length > 0 &&
|
|
72
|
+
!options.includes(argument)
|
|
73
|
+
) {
|
|
74
|
+
throw new CLIError(
|
|
75
|
+
`Invalid value for argument: \`--${arg.name}\` (\`${argument}\`). Expected one of: ${options.map((o) => `\`${o}\``).join(", ")}.`,
|
|
76
|
+
"EARG",
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
} else if (arg.type === "number") {
|
|
80
|
+
const _originalValue = parsedArgsProxy[arg.name];
|
|
81
|
+
parsedArgsProxy[arg.name] = Number.parseFloat(
|
|
82
|
+
parsedArgsProxy[arg.name] as string,
|
|
83
|
+
);
|
|
84
|
+
if (Number.isNaN(parsedArgsProxy[arg.name])) {
|
|
85
|
+
throw new CLIError(
|
|
86
|
+
`Invalid value for argument: \`--${arg.name}\` (\`${_originalValue}\`). Expected a number.`,
|
|
87
|
+
"EARG",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
} else if (arg.required && parsedArgsProxy[arg.name] === undefined) {
|
|
91
|
+
throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return parsedArgsProxy as ParsedArgs<T>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function resolveArgs(argsDef: ArgsDef): Arg[] {
|
|
99
|
+
const args: Arg[] = [];
|
|
100
|
+
for (const [name, argDef] of Object.entries(argsDef || {})) {
|
|
101
|
+
args.push({
|
|
102
|
+
...argDef,
|
|
103
|
+
name,
|
|
104
|
+
alias: toArray((argDef as any).alias),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return args;
|
|
108
|
+
}
|