@clerc/core 0.44.0 → 1.0.0-beta.10
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/dist/index.d.ts +266 -478
- package/dist/index.js +333 -810
- package/package.json +9 -16
package/dist/index.js
CHANGED
|
@@ -1,828 +1,351 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { defu } from 'defu';
|
|
4
|
-
import { LiteEmit } from 'lite-emit';
|
|
5
|
-
import { typeFlag } from 'type-flag';
|
|
6
|
-
import { IS_NODE, IS_ELECTRON, IS_DENO } from 'is-platform';
|
|
1
|
+
import { Choices, DOUBLE_DASH, DOUBLE_DASH as DOUBLE_DASH$1, KNOWN_FLAG, PARAMETER, PARAMETER as PARAMETER$1, UNKNOWN_FLAG, parse } from "@clerc/parser";
|
|
2
|
+
import { LiteEmit } from "lite-emit";
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
//#region ../utils/src/index.ts
|
|
5
|
+
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
6
|
+
/**
|
|
7
|
+
* Converts a dash- or space-separated string to camelCase.
|
|
8
|
+
* Not using regexp for better performance, because this function is used in parser.
|
|
9
|
+
*/
|
|
10
|
+
function camelCase(str) {
|
|
11
|
+
const firstIdx = Math.min(str.includes("-") ? str.indexOf("-") : Infinity, str.includes(" ") ? str.indexOf(" ") : Infinity);
|
|
12
|
+
if (firstIdx === Infinity) return str;
|
|
13
|
+
let result = str.slice(0, firstIdx);
|
|
14
|
+
for (let i = firstIdx; i < str.length; i++) if ((str[i] === "-" || str[i] === " ") && i + 1 < str.length) {
|
|
15
|
+
const nextChar = str.charCodeAt(i + 1);
|
|
16
|
+
if (nextChar >= 97 && nextChar <= 122) {
|
|
17
|
+
result += String.fromCharCode(nextChar - 32);
|
|
18
|
+
i++;
|
|
19
|
+
} else {
|
|
20
|
+
result += str[i + 1];
|
|
21
|
+
i++;
|
|
22
|
+
}
|
|
23
|
+
} else if (str[i] !== "-" && str[i] !== " ") result += str[i];
|
|
24
|
+
return result;
|
|
14
25
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
class CommandNameConflictError extends Error {
|
|
27
|
-
constructor(n1, n2, t) {
|
|
28
|
-
super(t("core.commandNameConflict", s(n1), s(n2)));
|
|
29
|
-
this.n1 = n1;
|
|
30
|
-
this.n2 = n2;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
class ScriptNameNotSetError extends Error {
|
|
34
|
-
constructor(t) {
|
|
35
|
-
super(t("core.scriptNameNotSet"));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
class DescriptionNotSetError extends Error {
|
|
39
|
-
constructor(t) {
|
|
40
|
-
super(t("core.descriptionNotSet"));
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
class VersionNotSetError extends Error {
|
|
44
|
-
constructor(t) {
|
|
45
|
-
super(t("core.versionNotSet"));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
class InvalidCommandNameError extends Error {
|
|
49
|
-
constructor(commandName, t) {
|
|
50
|
-
super(t("core.badNameFormat", s(commandName)));
|
|
51
|
-
this.commandName = commandName;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
class LocaleNotCalledFirstError extends Error {
|
|
55
|
-
constructor(t) {
|
|
56
|
-
super(t("core.localeMustBeCalledFirst"));
|
|
57
|
-
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/commands.ts
|
|
29
|
+
function resolveCommand(commandsMap, parameters) {
|
|
30
|
+
for (let i = parameters.length; i >= 0; i--) {
|
|
31
|
+
const name = parameters.slice(0, i).join(" ");
|
|
32
|
+
if (commandsMap.has(name)) return [commandsMap.get(name), name];
|
|
33
|
+
}
|
|
34
|
+
return [void 0, void 0];
|
|
58
35
|
}
|
|
59
36
|
|
|
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
|
-
"core.localeMustBeCalledFirst": "locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002",
|
|
88
|
-
"core.cliParseMustBeCalled": "cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002",
|
|
89
|
-
"core.spreadParameterMustBeLast": "\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002",
|
|
90
|
-
"core.requiredParameterCannotComeAfterOptionalParameter": "\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002",
|
|
91
|
-
"core.parameterMustBeWrappedInBrackets": "\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002",
|
|
92
|
-
"core.parameterIsUsedMoreThanOnce": "\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002",
|
|
93
|
-
"core.missingRequiredParameter": "\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"
|
|
94
|
-
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/errors.ts
|
|
39
|
+
var NoSuchCommandError = class extends Error {
|
|
40
|
+
constructor(commandName, text = `No such command: "${commandName}".`) {
|
|
41
|
+
super(text);
|
|
42
|
+
this.commandName = commandName;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var NoCommandSpecifiedError = class extends Error {
|
|
46
|
+
constructor(text = "No command specified.") {
|
|
47
|
+
super(text);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var InvalidCommandError = class extends Error {
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super(message);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var MissingRequiredMetadataError = class extends Error {
|
|
56
|
+
constructor(metadataName) {
|
|
57
|
+
super(`CLI ${metadataName} is required.`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var InvalidParametersError = class extends Error {
|
|
61
|
+
constructor(message) {
|
|
62
|
+
super(message);
|
|
63
|
+
}
|
|
95
64
|
};
|
|
96
65
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const firstCharacter = parameter[0];
|
|
109
|
-
const lastCharacter = parameter[parameter.length - 1];
|
|
110
|
-
let required;
|
|
111
|
-
if (firstCharacter === "<" && lastCharacter === ">") {
|
|
112
|
-
required = true;
|
|
113
|
-
if (hasOptional) {
|
|
114
|
-
throw new Error(
|
|
115
|
-
t(
|
|
116
|
-
"core.requiredParameterMustBeBeforeOptional",
|
|
117
|
-
stringify(parameter),
|
|
118
|
-
stringify(hasOptional)
|
|
119
|
-
)
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (firstCharacter === "[" && lastCharacter === "]") {
|
|
124
|
-
required = false;
|
|
125
|
-
hasOptional = parameter;
|
|
126
|
-
}
|
|
127
|
-
if (required === void 0) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
t("core.parameterMustBeWrappedInBrackets", stringify(parameter))
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
let name = parameter.slice(1, -1);
|
|
133
|
-
const spread = name.slice(-3) === "...";
|
|
134
|
-
if (spread) {
|
|
135
|
-
hasSpread = parameter;
|
|
136
|
-
name = name.slice(0, -3);
|
|
137
|
-
}
|
|
138
|
-
parsedParameters.push({
|
|
139
|
-
name,
|
|
140
|
-
required,
|
|
141
|
-
spread
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
return parsedParameters;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/interceptor.ts
|
|
68
|
+
function normalizeInspector(inspector) {
|
|
69
|
+
if (typeof inspector === "function") return {
|
|
70
|
+
enforce: "normal",
|
|
71
|
+
handler: inspector
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
enforce: inspector.enforce ?? "normal",
|
|
75
|
+
handler: inspector.handler
|
|
76
|
+
};
|
|
145
77
|
}
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
78
|
+
function compose(inspectors) {
|
|
79
|
+
const normalized = inspectors.map(normalizeInspector);
|
|
80
|
+
const pre = normalized.filter((i) => i.enforce === "pre");
|
|
81
|
+
const normal = normalized.filter((i) => i.enforce === "normal");
|
|
82
|
+
const post = normalized.filter((i) => i.enforce === "post");
|
|
83
|
+
const orderedInspectors = [
|
|
84
|
+
...pre,
|
|
85
|
+
...normal,
|
|
86
|
+
...post
|
|
87
|
+
];
|
|
88
|
+
return async (context) => {
|
|
89
|
+
let index = 0;
|
|
90
|
+
async function dispatch() {
|
|
91
|
+
if (index >= orderedInspectors.length) return;
|
|
92
|
+
await orderedInspectors[index++].handler(context, dispatch);
|
|
93
|
+
}
|
|
94
|
+
await dispatch();
|
|
95
|
+
};
|
|
162
96
|
}
|
|
163
97
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
commandsMap.set(typeof alias === "symbol" ? alias : alias.split(" "), {
|
|
176
|
-
...command,
|
|
177
|
-
__isAlias: true
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
function resolveFlattenCommands(commands, t) {
|
|
183
|
-
const commandsMap = /* @__PURE__ */ new Map();
|
|
184
|
-
if (commands[Root]) {
|
|
185
|
-
commandsMap.set(Root, commands[Root]);
|
|
186
|
-
setCommand(commandsMap, commands, commands[Root], t);
|
|
187
|
-
}
|
|
188
|
-
for (const command of Object.values(commands)) {
|
|
189
|
-
setCommand(commandsMap, commands, command, t);
|
|
190
|
-
commandsMap.set(command.name.split(" "), command);
|
|
191
|
-
}
|
|
192
|
-
return commandsMap;
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/parameters.ts
|
|
100
|
+
function getParametersToResolve(argv) {
|
|
101
|
+
const parameters = [];
|
|
102
|
+
for (const arg of argv) {
|
|
103
|
+
if (arg.startsWith("-")) break;
|
|
104
|
+
parameters.push(arg);
|
|
105
|
+
}
|
|
106
|
+
return parameters;
|
|
193
107
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
108
|
+
const PARAMETER_REGEX = /^(<|\[)([\w ]+)(\.\.\.)?(\]|>)$/;
|
|
109
|
+
const isParameterDefinitionBracketsValid = (definition) => definition.startsWith("<") && definition.endsWith(">") || definition.startsWith("[") && definition.endsWith("]");
|
|
110
|
+
function _parseParameters(definitions, parameters) {
|
|
111
|
+
const result = {};
|
|
112
|
+
let hasOptional = false;
|
|
113
|
+
for (const [i, definition] of definitions.entries()) {
|
|
114
|
+
const match = definition.match(PARAMETER_REGEX);
|
|
115
|
+
if (!match || !isParameterDefinitionBracketsValid(definition)) throw new InvalidParametersError(`Invalid parameter definition: ${definition}`);
|
|
116
|
+
const name = camelCase(match[2]);
|
|
117
|
+
const isVariadic = !!match[3];
|
|
118
|
+
const isRequired = definition.startsWith("<");
|
|
119
|
+
if (name in result) throw new InvalidParametersError(`Duplicate parameter name: ${name}`);
|
|
120
|
+
if (isVariadic && i !== definitions.length - 1) throw new InvalidParametersError("Variadic parameter must be the last parameter in the definition.");
|
|
121
|
+
if (isRequired) {
|
|
122
|
+
if (hasOptional) throw new InvalidParametersError(`Required parameter "${name}" cannot appear after an optional parameter.`);
|
|
123
|
+
} else hasOptional = true;
|
|
124
|
+
const value = isVariadic ? parameters.slice(i) : parameters[i];
|
|
125
|
+
if (isRequired && (isVariadic ? value.length === 0 : value === void 0)) throw new InvalidParametersError(`Missing required ${isVariadic ? "variadic " : ""}parameter: ${name}`);
|
|
126
|
+
result[name] = value;
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
211
129
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const objectInterceptor = typeof interceptor === "object" ? interceptor : { fn: interceptor };
|
|
224
|
-
const { enforce, fn } = objectInterceptor;
|
|
225
|
-
if (enforce === "post" || enforce === "pre") {
|
|
226
|
-
interceptorMap[enforce].push(fn);
|
|
227
|
-
} else {
|
|
228
|
-
interceptorMap.normal.push(fn);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
const mergedInterceptorFns = [
|
|
232
|
-
...interceptorMap.pre,
|
|
233
|
-
...interceptorMap.normal,
|
|
234
|
-
...interceptorMap.post
|
|
235
|
-
];
|
|
236
|
-
return (ctx) => {
|
|
237
|
-
return dispatch(0);
|
|
238
|
-
function dispatch(i) {
|
|
239
|
-
const interceptor = mergedInterceptorFns[i];
|
|
240
|
-
return interceptor(ctx, dispatch.bind(null, i + 1));
|
|
241
|
-
}
|
|
242
|
-
};
|
|
130
|
+
function parseParameters(definitions, parameters, doubleDashParameters) {
|
|
131
|
+
const doubleDashIndex = definitions.indexOf(DOUBLE_DASH$1);
|
|
132
|
+
if (doubleDashIndex === -1) return _parseParameters(definitions, parameters);
|
|
133
|
+
else {
|
|
134
|
+
const definitionBeforeDoubleDash = definitions.slice(0, doubleDashIndex);
|
|
135
|
+
const definitionAfterDoubleDash = definitions.slice(doubleDashIndex + 1);
|
|
136
|
+
return {
|
|
137
|
+
..._parseParameters(definitionBeforeDoubleDash, parameters),
|
|
138
|
+
..._parseParameters(definitionAfterDoubleDash, doubleDashParameters)
|
|
139
|
+
};
|
|
140
|
+
}
|
|
243
141
|
}
|
|
244
|
-
const INVALID_RE = /\s{2,}/;
|
|
245
|
-
const isValidName = (name) => name === Root ? true : !(name.startsWith(" ") || name.endsWith(" ")) && !INVALID_RE.test(name);
|
|
246
|
-
const withBrackets = (s, isOptional) => isOptional ? `[${s}]` : `<${s}>`;
|
|
247
|
-
const ROOT = "<Root>";
|
|
248
|
-
const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : ROOT;
|
|
249
|
-
const detectLocale = () => process.env.CLERC_LOCALE ? process.env.CLERC_LOCALE : Intl.DateTimeFormat().resolvedOptions().locale;
|
|
250
|
-
const stripFlags = (argv) => argv.filter((arg) => !arg.startsWith("-"));
|
|
251
142
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
434
|
-
__privateSet(this, _version, version);
|
|
435
|
-
return this;
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Set the Locale You must call this method once after you created the Clerc
|
|
439
|
-
* instance.
|
|
440
|
-
*
|
|
441
|
-
* @example
|
|
442
|
-
*
|
|
443
|
-
* ```ts
|
|
444
|
-
* Clerc.create()
|
|
445
|
-
* .locale("en")
|
|
446
|
-
* .command(...)
|
|
447
|
-
* ```
|
|
448
|
-
*
|
|
449
|
-
* @param locale
|
|
450
|
-
* @returns
|
|
451
|
-
*/
|
|
452
|
-
locale(locale) {
|
|
453
|
-
if (__privateGet(this, _isOtherMethodCalled)) {
|
|
454
|
-
throw new LocaleNotCalledFirstError(this.i18n.t);
|
|
455
|
-
}
|
|
456
|
-
__privateSet(this, _locale, locale);
|
|
457
|
-
return this;
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Set the fallback Locale You must call this method once after you created
|
|
461
|
-
* the Clerc instance.
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
*
|
|
465
|
-
* ```ts
|
|
466
|
-
* Clerc.create()
|
|
467
|
-
* .fallbackLocale("en")
|
|
468
|
-
* .command(...)
|
|
469
|
-
* ```
|
|
470
|
-
*
|
|
471
|
-
* @param fallbackLocale
|
|
472
|
-
* @returns
|
|
473
|
-
*/
|
|
474
|
-
fallbackLocale(fallbackLocale) {
|
|
475
|
-
if (__privateGet(this, _isOtherMethodCalled)) {
|
|
476
|
-
throw new LocaleNotCalledFirstError(this.i18n.t);
|
|
477
|
-
}
|
|
478
|
-
__privateSet(this, _defaultLocale, fallbackLocale);
|
|
479
|
-
return this;
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Register a error handler
|
|
483
|
-
*
|
|
484
|
-
* @example
|
|
485
|
-
*
|
|
486
|
-
* ```ts
|
|
487
|
-
* Clerc.create().errorHandler((err) => {
|
|
488
|
-
* console.log(err);
|
|
489
|
-
* });
|
|
490
|
-
* ```
|
|
491
|
-
*
|
|
492
|
-
* @param handler
|
|
493
|
-
* @returns
|
|
494
|
-
*/
|
|
495
|
-
errorHandler(handler) {
|
|
496
|
-
__privateGet(this, _errorHandlers).push(handler);
|
|
497
|
-
return this;
|
|
498
|
-
}
|
|
499
|
-
command(nameOrCommand, description, options = {}) {
|
|
500
|
-
__privateMethod(this, _callWithErrorHandling, callWithErrorHandling_fn).call(this, () => {
|
|
501
|
-
if (Array.isArray(nameOrCommand)) {
|
|
502
|
-
for (const command of nameOrCommand) {
|
|
503
|
-
__privateMethod(this, _command, command_fn).call(this, command);
|
|
504
|
-
}
|
|
505
|
-
} else {
|
|
506
|
-
__privateMethod(this, _command, command_fn).call(this, nameOrCommand, description, options);
|
|
507
|
-
}
|
|
508
|
-
});
|
|
509
|
-
return this;
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Register a global flag
|
|
513
|
-
*
|
|
514
|
-
* @example
|
|
515
|
-
*
|
|
516
|
-
* ```ts
|
|
517
|
-
* Clerc.create().flag("help", "help", {
|
|
518
|
-
* alias: "h",
|
|
519
|
-
* type: Boolean,
|
|
520
|
-
* });
|
|
521
|
-
* ```
|
|
522
|
-
*
|
|
523
|
-
* @param name
|
|
524
|
-
* @param description
|
|
525
|
-
* @param options
|
|
526
|
-
* @returns
|
|
527
|
-
*/
|
|
528
|
-
flag(name, description, options) {
|
|
529
|
-
__privateGet(this, _flags)[name] = {
|
|
530
|
-
description,
|
|
531
|
-
...options
|
|
532
|
-
};
|
|
533
|
-
return this;
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Register a handler
|
|
537
|
-
*
|
|
538
|
-
* @example
|
|
539
|
-
*
|
|
540
|
-
* ```ts
|
|
541
|
-
* Clerc.create()
|
|
542
|
-
* .command("test", "test command")
|
|
543
|
-
* .on("test", (ctx) => {
|
|
544
|
-
* console.log(ctx);
|
|
545
|
-
* });
|
|
546
|
-
* ```
|
|
547
|
-
*
|
|
548
|
-
* @param name
|
|
549
|
-
* @param handler
|
|
550
|
-
* @returns
|
|
551
|
-
*/
|
|
552
|
-
on(name, handler) {
|
|
553
|
-
__privateGet(this, _commandEmitter).on(name, handler);
|
|
554
|
-
return this;
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* Use a plugin
|
|
558
|
-
*
|
|
559
|
-
* @example
|
|
560
|
-
*
|
|
561
|
-
* ```ts
|
|
562
|
-
* Clerc.create().use(plugin);
|
|
563
|
-
* ```
|
|
564
|
-
*
|
|
565
|
-
* @param plugin
|
|
566
|
-
* @returns
|
|
567
|
-
*/
|
|
568
|
-
use(plugin) {
|
|
569
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
570
|
-
return plugin.setup(this);
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* @deprecated This is a typo. Use `intercetor()` instead.
|
|
574
|
-
*/
|
|
575
|
-
inspector(interceptor) {
|
|
576
|
-
return this.interceptor(interceptor);
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Register a interceptor
|
|
580
|
-
*
|
|
581
|
-
* @example
|
|
582
|
-
*
|
|
583
|
-
* ```ts
|
|
584
|
-
* Clerc.create().interceptor((ctx, next) => {
|
|
585
|
-
* console.log(ctx);
|
|
586
|
-
* next();
|
|
587
|
-
* });
|
|
588
|
-
* ```
|
|
589
|
-
*
|
|
590
|
-
* @param interceptor
|
|
591
|
-
* @returns
|
|
592
|
-
*/
|
|
593
|
-
interceptor(interceptor) {
|
|
594
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
595
|
-
__privateGet(this, _interceptors).push(interceptor);
|
|
596
|
-
return this;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Parse the command line arguments
|
|
600
|
-
*
|
|
601
|
-
* @example
|
|
602
|
-
*
|
|
603
|
-
* ```ts
|
|
604
|
-
* Clerc.create().parse(process.argv.slice(2)); // Optional
|
|
605
|
-
* ```
|
|
606
|
-
*
|
|
607
|
-
* @param args
|
|
608
|
-
* @param optionsOrArgv
|
|
609
|
-
* @returns
|
|
610
|
-
*/
|
|
611
|
-
parse(optionsOrArgv = resolveArgv()) {
|
|
612
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
613
|
-
const { argv, run } = Array.isArray(optionsOrArgv) ? {
|
|
614
|
-
argv: optionsOrArgv,
|
|
615
|
-
run: true
|
|
616
|
-
} : {
|
|
617
|
-
argv: resolveArgv(),
|
|
618
|
-
...optionsOrArgv
|
|
619
|
-
};
|
|
620
|
-
__privateSet(this, _argv, [...argv]);
|
|
621
|
-
__privateMethod(this, _validateMeta, validateMeta_fn).call(this);
|
|
622
|
-
if (run) {
|
|
623
|
-
this.runMatchedCommand();
|
|
624
|
-
}
|
|
625
|
-
return this;
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Run matched command
|
|
629
|
-
*
|
|
630
|
-
* @example
|
|
631
|
-
*
|
|
632
|
-
* ```ts
|
|
633
|
-
* Clerc.create().parse({ run: false }).runMatchedCommand();
|
|
634
|
-
* ```
|
|
635
|
-
*
|
|
636
|
-
* @returns
|
|
637
|
-
*/
|
|
638
|
-
runMatchedCommand() {
|
|
639
|
-
__privateMethod(this, _callWithErrorHandling, callWithErrorHandling_fn).call(this, () => __privateMethod(this, _runMatchedCommand, runMatchedCommand_fn).call(this));
|
|
640
|
-
process.title = __privateGet(this, _name);
|
|
641
|
-
return this;
|
|
642
|
-
}
|
|
643
|
-
};
|
|
644
|
-
_name = new WeakMap();
|
|
645
|
-
_scriptName = new WeakMap();
|
|
646
|
-
_description = new WeakMap();
|
|
647
|
-
_version = new WeakMap();
|
|
648
|
-
_interceptors = new WeakMap();
|
|
649
|
-
_commands = new WeakMap();
|
|
650
|
-
_commandEmitter = new WeakMap();
|
|
651
|
-
_flags = new WeakMap();
|
|
652
|
-
_usedNames = new WeakMap();
|
|
653
|
-
_argv = new WeakMap();
|
|
654
|
-
_errorHandlers = new WeakMap();
|
|
655
|
-
_isOtherMethodCalled = new WeakMap();
|
|
656
|
-
_defaultLocale = new WeakMap();
|
|
657
|
-
_locale = new WeakMap();
|
|
658
|
-
_locales = new WeakMap();
|
|
659
|
-
_hasRootOrAlias = new WeakSet();
|
|
660
|
-
hasRootOrAlias_get = function() {
|
|
661
|
-
return __privateGet(this, _usedNames).has(Root);
|
|
662
|
-
};
|
|
663
|
-
_hasRoot = new WeakSet();
|
|
664
|
-
hasRoot_get = function() {
|
|
665
|
-
return Object.prototype.hasOwnProperty.call(this._commands, Root);
|
|
666
|
-
};
|
|
667
|
-
_addCoreLocales = new WeakSet();
|
|
668
|
-
addCoreLocales_fn = function() {
|
|
669
|
-
this.i18n.add(locales);
|
|
670
|
-
};
|
|
671
|
-
_otherMethodCalled = new WeakSet();
|
|
672
|
-
otherMethodCalled_fn = function() {
|
|
673
|
-
__privateSet(this, _isOtherMethodCalled, true);
|
|
674
|
-
};
|
|
675
|
-
_command = new WeakSet();
|
|
676
|
-
command_fn = function(nameOrCommand, description, options = {}) {
|
|
677
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
678
|
-
const { t } = this.i18n;
|
|
679
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === Root);
|
|
680
|
-
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
681
|
-
const name = isCommandObject ? nameOrCommand.name : nameOrCommand;
|
|
682
|
-
if (!isValidName(name)) {
|
|
683
|
-
throw new InvalidCommandNameError(name, t);
|
|
684
|
-
}
|
|
685
|
-
const { handler = void 0, ...commandToSave } = isCommandObject ? nameOrCommand : { name, description, ...options };
|
|
686
|
-
const nameList = [commandToSave.name];
|
|
687
|
-
const aliasList = commandToSave.alias ? toArray(commandToSave.alias) : [];
|
|
688
|
-
commandToSave.alias && nameList.push(...aliasList);
|
|
689
|
-
for (const name2 of nameList) {
|
|
690
|
-
if (__privateGet(this, _usedNames).has(name2)) {
|
|
691
|
-
throw new CommandExistsError(formatCommandName(name2), t);
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
__privateGet(this, _commands)[name] = commandToSave;
|
|
695
|
-
__privateGet(this, _usedNames).add(commandToSave.name);
|
|
696
|
-
for (const a of aliasList) {
|
|
697
|
-
__privateGet(this, _usedNames).add(a);
|
|
698
|
-
}
|
|
699
|
-
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
700
|
-
return this;
|
|
701
|
-
};
|
|
702
|
-
_validateMeta = new WeakSet();
|
|
703
|
-
validateMeta_fn = function() {
|
|
704
|
-
const { t } = this.i18n;
|
|
705
|
-
if (!__privateGet(this, _scriptName)) {
|
|
706
|
-
throw new ScriptNameNotSetError(t);
|
|
707
|
-
}
|
|
708
|
-
if (!__privateGet(this, _description)) {
|
|
709
|
-
throw new DescriptionNotSetError(t);
|
|
710
|
-
}
|
|
711
|
-
if (!__privateGet(this, _version)) {
|
|
712
|
-
throw new VersionNotSetError(t);
|
|
713
|
-
}
|
|
714
|
-
};
|
|
715
|
-
_getContext = new WeakSet();
|
|
716
|
-
getContext_fn = function(getCommand) {
|
|
717
|
-
var _a;
|
|
718
|
-
const argv = __privateGet(this, _argv);
|
|
719
|
-
const { t } = this.i18n;
|
|
720
|
-
const [command, called] = getCommand();
|
|
721
|
-
const isCommandResolved = !!command;
|
|
722
|
-
const flagsMerged = {
|
|
723
|
-
...__privateGet(this, _flags),
|
|
724
|
-
...command == null ? void 0 : command.flags
|
|
725
|
-
};
|
|
726
|
-
const parsed = typeFlag(flagsMerged, [...argv]);
|
|
727
|
-
const parametersOffset = (command == null ? void 0 : command.name) === Root || called === Root ? 0 : (called == null ? void 0 : called.length) ? called.length : command == null ? void 0 : command.name.split(" ").length;
|
|
728
|
-
const { _: args, flags, unknownFlags } = parsed;
|
|
729
|
-
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(parametersOffset);
|
|
730
|
-
let commandParameters = (_a = command == null ? void 0 : command.parameters) != null ? _a : [];
|
|
731
|
-
const hasEof = commandParameters.indexOf("--");
|
|
732
|
-
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
733
|
-
const mapping = /* @__PURE__ */ Object.create(null);
|
|
734
|
-
if (hasEof > -1 && eofParameters.length > 0) {
|
|
735
|
-
commandParameters = commandParameters.slice(0, hasEof);
|
|
736
|
-
const eofArguments = args["--"];
|
|
737
|
-
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
738
|
-
mapParametersToArguments(
|
|
739
|
-
mapping,
|
|
740
|
-
parseParameters(commandParameters, t),
|
|
741
|
-
parameters,
|
|
742
|
-
t
|
|
743
|
-
);
|
|
744
|
-
mapParametersToArguments(
|
|
745
|
-
mapping,
|
|
746
|
-
parseParameters(eofParameters, t),
|
|
747
|
-
eofArguments,
|
|
748
|
-
t
|
|
749
|
-
);
|
|
750
|
-
} else {
|
|
751
|
-
mapParametersToArguments(
|
|
752
|
-
mapping,
|
|
753
|
-
parseParameters(commandParameters, t),
|
|
754
|
-
parameters,
|
|
755
|
-
t
|
|
756
|
-
);
|
|
757
|
-
}
|
|
758
|
-
const mergedFlags = { ...flags, ...unknownFlags };
|
|
759
|
-
const context = {
|
|
760
|
-
name: command == null ? void 0 : command.name,
|
|
761
|
-
called: Array.isArray(called) ? called.join(" ") : called,
|
|
762
|
-
resolved: isCommandResolved,
|
|
763
|
-
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
764
|
-
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
765
|
-
raw: { ...parsed, parameters, mergedFlags },
|
|
766
|
-
parameters: mapping,
|
|
767
|
-
flags,
|
|
768
|
-
unknownFlags,
|
|
769
|
-
cli: this
|
|
770
|
-
};
|
|
771
|
-
return context;
|
|
772
|
-
};
|
|
773
|
-
_handleError = new WeakSet();
|
|
774
|
-
handleError_fn = function(e) {
|
|
775
|
-
if (__privateGet(this, _errorHandlers).length > 0) {
|
|
776
|
-
for (const cb of __privateGet(this, _errorHandlers)) {
|
|
777
|
-
cb(e);
|
|
778
|
-
}
|
|
779
|
-
} else {
|
|
780
|
-
throw e;
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
_callWithErrorHandling = new WeakSet();
|
|
784
|
-
callWithErrorHandling_fn = function(fn) {
|
|
785
|
-
try {
|
|
786
|
-
fn();
|
|
787
|
-
} catch (e) {
|
|
788
|
-
__privateMethod(this, _handleError, handleError_fn).call(this, e);
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
_runMatchedCommand = new WeakSet();
|
|
792
|
-
runMatchedCommand_fn = function() {
|
|
793
|
-
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
794
|
-
const { t } = this.i18n;
|
|
795
|
-
const argv = __privateGet(this, _argv);
|
|
796
|
-
if (!argv) {
|
|
797
|
-
throw new Error(t("core.cliParseMustBeCalled"));
|
|
798
|
-
}
|
|
799
|
-
const getCommand = () => resolveCommand(__privateGet(this, _commands), argv, t);
|
|
800
|
-
const getContext = () => __privateMethod(this, _getContext, getContext_fn).call(this, getCommand);
|
|
801
|
-
const emitHandler = {
|
|
802
|
-
enforce: "post",
|
|
803
|
-
fn: (ctx) => {
|
|
804
|
-
const [command] = getCommand();
|
|
805
|
-
const stringName = stripFlags(argv).join(" ");
|
|
806
|
-
if (!command) {
|
|
807
|
-
const error = stringName ? new NoSuchCommandError(stringName, t) : new NoCommandGivenError(t);
|
|
808
|
-
throw error;
|
|
809
|
-
}
|
|
810
|
-
__privateGet(this, _commandEmitter).emit(command.name, ctx);
|
|
811
|
-
}
|
|
812
|
-
};
|
|
813
|
-
const interceptor = [...__privateGet(this, _interceptors), emitHandler];
|
|
814
|
-
const callInterceptor = compose(interceptor);
|
|
815
|
-
callInterceptor(getContext());
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region ../../node_modules/.pnpm/is-platform@1.0.0/node_modules/is-platform/dist/index.mjs
|
|
145
|
+
const IS_DENO = typeof Deno !== "undefined";
|
|
146
|
+
const IS_NODE = typeof process !== "undefined" && !IS_DENO;
|
|
147
|
+
const IS_ELECTRON = process.versions.electron && !process.defaultApp;
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/platform.ts
|
|
151
|
+
const platformArgv = IS_NODE ? process.argv.slice(IS_ELECTRON ? 1 : 2) : IS_DENO ? Deno.args : [];
|
|
152
|
+
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/cli.ts
|
|
155
|
+
var Clerc = class Clerc {
|
|
156
|
+
#argv = [];
|
|
157
|
+
#commands = /* @__PURE__ */ new Map();
|
|
158
|
+
#emitter = new LiteEmit({ errorHandler: (error) => this.#handleError(error) });
|
|
159
|
+
#globalFlags = {};
|
|
160
|
+
#interceptors = [];
|
|
161
|
+
#errorHandlers = [];
|
|
162
|
+
#name = "";
|
|
163
|
+
#scriptName = "";
|
|
164
|
+
#description = "";
|
|
165
|
+
#version = "";
|
|
166
|
+
constructor({ name, scriptName, description, version } = {}) {
|
|
167
|
+
if (name) this.#name = name;
|
|
168
|
+
if (scriptName) this.#scriptName = scriptName;
|
|
169
|
+
if (description) this.#description = description;
|
|
170
|
+
if (version) this.#version = version;
|
|
171
|
+
}
|
|
172
|
+
get _name() {
|
|
173
|
+
return this.#name || this.#scriptName;
|
|
174
|
+
}
|
|
175
|
+
get _scriptName() {
|
|
176
|
+
return this.#scriptName;
|
|
177
|
+
}
|
|
178
|
+
get _description() {
|
|
179
|
+
return this.#description;
|
|
180
|
+
}
|
|
181
|
+
get _version() {
|
|
182
|
+
return this.#version;
|
|
183
|
+
}
|
|
184
|
+
get _commands() {
|
|
185
|
+
return this.#commands;
|
|
186
|
+
}
|
|
187
|
+
get _globalFlags() {
|
|
188
|
+
return this.#globalFlags;
|
|
189
|
+
}
|
|
190
|
+
static create(options) {
|
|
191
|
+
return new Clerc(options);
|
|
192
|
+
}
|
|
193
|
+
name(name) {
|
|
194
|
+
this.#name = name;
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
scriptName(scriptName) {
|
|
198
|
+
this.#scriptName = scriptName;
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
description(description) {
|
|
202
|
+
this.#description = description;
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
version(version) {
|
|
206
|
+
this.#version = version;
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
use(plugin) {
|
|
210
|
+
plugin.setup(this);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
errorHandler(handler) {
|
|
214
|
+
this.#errorHandlers.push(handler);
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
#handleError(error) {
|
|
218
|
+
if (this.#errorHandlers.length > 0) for (const callback of this.#errorHandlers) callback(error);
|
|
219
|
+
else throw error;
|
|
220
|
+
}
|
|
221
|
+
#callWithErrorHandler(fn) {
|
|
222
|
+
try {
|
|
223
|
+
const result = fn();
|
|
224
|
+
if (result instanceof Promise) return result.catch((error) => {
|
|
225
|
+
this.#handleError(error);
|
|
226
|
+
});
|
|
227
|
+
return result;
|
|
228
|
+
} catch (error) {
|
|
229
|
+
this.#handleError(error);
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
#validateCommandNameAndAlias(name, aliases) {
|
|
234
|
+
if (this.#commands.has(name)) throw new InvalidCommandError(`Command with name "${name}" already exists.`);
|
|
235
|
+
for (const alias of aliases) if (this.#commands.has(alias)) throw new InvalidCommandError(`Command with name "${alias}" already exists.`);
|
|
236
|
+
}
|
|
237
|
+
command(nameOrCommandObject, description, options) {
|
|
238
|
+
const command = typeof nameOrCommandObject === "string" ? {
|
|
239
|
+
name: nameOrCommandObject,
|
|
240
|
+
description,
|
|
241
|
+
...options
|
|
242
|
+
} : nameOrCommandObject;
|
|
243
|
+
const aliases = toArray(command?.alias ?? []);
|
|
244
|
+
this.#callWithErrorHandler(() => this.#validateCommandNameAndAlias(command.name, aliases));
|
|
245
|
+
this.#commands.set(command.name, command);
|
|
246
|
+
for (const alias of aliases) this.#commands.set(alias, {
|
|
247
|
+
...command,
|
|
248
|
+
__isAlias: true
|
|
249
|
+
});
|
|
250
|
+
if (command.handler) this.on(command.name, command.handler);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
globalFlag(name, description, options) {
|
|
254
|
+
this.#globalFlags[name] = {
|
|
255
|
+
description,
|
|
256
|
+
...options
|
|
257
|
+
};
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
interceptor(interceptor) {
|
|
261
|
+
this.#interceptors.push(interceptor);
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
on(name, handler) {
|
|
265
|
+
this.#emitter.on(name, handler);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
#validate() {
|
|
269
|
+
if (!this.#scriptName) throw new MissingRequiredMetadataError("script name");
|
|
270
|
+
if (!this.#description) throw new MissingRequiredMetadataError("description");
|
|
271
|
+
if (!this.#version) throw new MissingRequiredMetadataError("version");
|
|
272
|
+
}
|
|
273
|
+
#parseArgv(argv, command) {
|
|
274
|
+
const { flags, ignore } = command ?? {};
|
|
275
|
+
return this.#callWithErrorHandler(() => parse(argv, {
|
|
276
|
+
flags: {
|
|
277
|
+
...this.#globalFlags,
|
|
278
|
+
...flags
|
|
279
|
+
},
|
|
280
|
+
ignore
|
|
281
|
+
}));
|
|
282
|
+
}
|
|
283
|
+
async run() {
|
|
284
|
+
const parametersToResolve = getParametersToResolve(this.#argv);
|
|
285
|
+
const [command, calledAs] = resolveCommand(this.#commands, parametersToResolve);
|
|
286
|
+
const argvToPass = command && calledAs.length > 0 ? this.#argv.slice(calledAs.split(" ").length) : this.#argv;
|
|
287
|
+
const parsed = this.#callWithErrorHandler(() => this.#parseArgv(argvToPass, command));
|
|
288
|
+
let parameters = {};
|
|
289
|
+
let parametersError;
|
|
290
|
+
try {
|
|
291
|
+
parameters = command?.parameters ? parseParameters(command.parameters, parsed.parameters, parsed.doubleDash) : {};
|
|
292
|
+
} catch (e) {
|
|
293
|
+
parametersError = e;
|
|
294
|
+
}
|
|
295
|
+
const context = {
|
|
296
|
+
resolved: !!command,
|
|
297
|
+
command,
|
|
298
|
+
calledAs,
|
|
299
|
+
parameters,
|
|
300
|
+
flags: parsed.flags,
|
|
301
|
+
ignored: parsed.ignored,
|
|
302
|
+
rawParsed: parsed,
|
|
303
|
+
missingParameters: !!parametersError
|
|
304
|
+
};
|
|
305
|
+
const emitInterceptor = {
|
|
306
|
+
enforce: "post",
|
|
307
|
+
handler: (ctx) => {
|
|
308
|
+
if (parametersError) throw parametersError;
|
|
309
|
+
if (command) this.#emitter.emit(command.name, ctx);
|
|
310
|
+
else throw parametersToResolve.length > 0 ? new NoSuchCommandError(parametersToResolve.join(" ")) : new NoCommandSpecifiedError();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
const composedInterceptor = compose([...this.#interceptors, emitInterceptor]);
|
|
314
|
+
return this.#callWithErrorHandler(() => composedInterceptor(context));
|
|
315
|
+
}
|
|
316
|
+
parse(argvOrOptions = platformArgv) {
|
|
317
|
+
this.#callWithErrorHandler(() => this.#validate());
|
|
318
|
+
if (Array.isArray(argvOrOptions)) argvOrOptions = { argv: argvOrOptions };
|
|
319
|
+
const { argv = platformArgv, run = true } = argvOrOptions;
|
|
320
|
+
this.#argv = argv;
|
|
321
|
+
if (run) return this.run();
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
816
324
|
};
|
|
817
|
-
let Clerc = _Clerc;
|
|
818
325
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
const defineInterceptor = (_cli, interceptor) => interceptor;
|
|
822
|
-
const defineInspector = defineInterceptor;
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/helpers.ts
|
|
823
328
|
const defineCommand = (command, handler) => ({
|
|
824
|
-
|
|
825
|
-
|
|
329
|
+
...command,
|
|
330
|
+
handler
|
|
826
331
|
});
|
|
827
332
|
|
|
828
|
-
|
|
333
|
+
//#endregion
|
|
334
|
+
//#region src/ignore.ts
|
|
335
|
+
function createStopAtFirstParameter() {
|
|
336
|
+
let encounteredParameter = false;
|
|
337
|
+
return (type) => {
|
|
338
|
+
if (type === PARAMETER$1 && !encounteredParameter) {
|
|
339
|
+
encounteredParameter = true;
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
return encounteredParameter;
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region src/plugin.ts
|
|
348
|
+
const definePlugin = (plugin) => plugin;
|
|
349
|
+
|
|
350
|
+
//#endregion
|
|
351
|
+
export { Choices, Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, KNOWN_FLAG, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|