clerc 0.9.1 → 0.10.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/dist/index.cjs +38 -390
- package/dist/index.d.ts +5 -323
- package/dist/index.mjs +5 -366
- package/dist/toolkit.cjs +14 -0
- package/dist/toolkit.d.ts +1 -0
- package/dist/toolkit.mjs +1 -0
- package/package.json +12 -5
package/dist/index.cjs
CHANGED
|
@@ -2,393 +2,41 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
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
|
-
class MultipleCommandsMatchedError extends Error {
|
|
46
|
-
constructor(name) {
|
|
47
|
-
super(`Multiple commands matched: ${name}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
class CommandNameConflictError extends Error {
|
|
51
|
-
constructor(n1, n2) {
|
|
52
|
-
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function resolveFlattenCommands(commands) {
|
|
57
|
-
const commandsMap = /* @__PURE__ */ new Map();
|
|
58
|
-
for (const command of Object.values(commands)) {
|
|
59
|
-
if (command.alias) {
|
|
60
|
-
const aliases = utils.mustArray(command.alias);
|
|
61
|
-
for (const alias of aliases) {
|
|
62
|
-
if (alias in commands) {
|
|
63
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
64
|
-
}
|
|
65
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
commandsMap.set(command.name.split(" "), command);
|
|
69
|
-
}
|
|
70
|
-
return commandsMap;
|
|
71
|
-
}
|
|
72
|
-
function resolveCommand(commands, name) {
|
|
73
|
-
if (name === SingleCommand) {
|
|
74
|
-
return commands[SingleCommand];
|
|
75
|
-
}
|
|
76
|
-
const nameArr = utils.mustArray(name);
|
|
77
|
-
const commandsMap = resolveFlattenCommands(commands);
|
|
78
|
-
const possibleCommands = [];
|
|
79
|
-
commandsMap.forEach((v, k) => {
|
|
80
|
-
if (utils.arrayStartsWith(nameArr, k)) {
|
|
81
|
-
possibleCommands.push(v);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
if (possibleCommands.length > 1) {
|
|
85
|
-
const matchedCommandNames = possibleCommands.map((c) => c.name).join(", ");
|
|
86
|
-
throw new MultipleCommandsMatchedError(matchedCommandNames);
|
|
87
|
-
}
|
|
88
|
-
return possibleCommands[0];
|
|
89
|
-
}
|
|
90
|
-
function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
91
|
-
const parentArr = parent === "" ? [] : Array.isArray(parent) ? parent : parent.split(" ");
|
|
92
|
-
return Object.values(commands).filter((c) => {
|
|
93
|
-
const commandNameArr = c.name.split(" ");
|
|
94
|
-
return utils.arrayStartsWith(commandNameArr, parentArr) && commandNameArr.length - parentArr.length <= depth;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
98
|
-
function resolveParametersBeforeFlag(argv, isSingleCommand) {
|
|
99
|
-
if (isSingleCommand) {
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
const parameters = [];
|
|
103
|
-
for (const arg of argv) {
|
|
104
|
-
if (arg.startsWith("-")) {
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
parameters.push(arg);
|
|
108
|
-
}
|
|
109
|
-
return parameters;
|
|
110
|
-
}
|
|
111
|
-
const resolveArgv = () => isPlatform.isNode() ? process.argv.slice(2) : isPlatform.isDeno() ? Deno.args : [];
|
|
112
|
-
function compose(inspectors) {
|
|
113
|
-
return (getCtx) => {
|
|
114
|
-
return dispatch(0);
|
|
115
|
-
function dispatch(i) {
|
|
116
|
-
const inspector = inspectors[i];
|
|
117
|
-
return inspector(getCtx(), dispatch.bind(null, i + 1));
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const { stringify } = JSON;
|
|
123
|
-
function parseParameters(parameters) {
|
|
124
|
-
const parsedParameters = [];
|
|
125
|
-
let hasOptional;
|
|
126
|
-
let hasSpread;
|
|
127
|
-
for (const parameter of parameters) {
|
|
128
|
-
if (hasSpread) {
|
|
129
|
-
throw new Error(`Invalid parameter: Spread parameter ${stringify(hasSpread)} must be last`);
|
|
130
|
-
}
|
|
131
|
-
const firstCharacter = parameter[0];
|
|
132
|
-
const lastCharacter = parameter[parameter.length - 1];
|
|
133
|
-
let required;
|
|
134
|
-
if (firstCharacter === "<" && lastCharacter === ">") {
|
|
135
|
-
required = true;
|
|
136
|
-
if (hasOptional) {
|
|
137
|
-
throw new Error(`Invalid parameter: Required parameter ${stringify(parameter)} cannot come after optional parameter ${stringify(hasOptional)}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (firstCharacter === "[" && lastCharacter === "]") {
|
|
141
|
-
required = false;
|
|
142
|
-
hasOptional = parameter;
|
|
143
|
-
}
|
|
144
|
-
if (required === void 0) {
|
|
145
|
-
throw new Error(`Invalid parameter: ${stringify(parameter)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);
|
|
146
|
-
}
|
|
147
|
-
let name = parameter.slice(1, -1);
|
|
148
|
-
const spread = name.slice(-3) === "...";
|
|
149
|
-
if (spread) {
|
|
150
|
-
hasSpread = parameter;
|
|
151
|
-
name = name.slice(0, -3);
|
|
152
|
-
}
|
|
153
|
-
parsedParameters.push({
|
|
154
|
-
name,
|
|
155
|
-
required,
|
|
156
|
-
spread
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
return parsedParameters;
|
|
160
|
-
}
|
|
161
|
-
function mapParametersToArguments(mapping, parameters, cliArguments) {
|
|
162
|
-
for (let i = 0; i < parameters.length; i += 1) {
|
|
163
|
-
const { name, required, spread } = parameters[i];
|
|
164
|
-
const camelCaseName = utils.camelCase(name);
|
|
165
|
-
if (camelCaseName in mapping) {
|
|
166
|
-
throw new Error(`Invalid parameter: ${stringify(name)} is used more than once.`);
|
|
167
|
-
}
|
|
168
|
-
const value = spread ? cliArguments.slice(i) : cliArguments[i];
|
|
169
|
-
if (spread) {
|
|
170
|
-
i = parameters.length;
|
|
171
|
-
}
|
|
172
|
-
if (required && (!value || spread && value.length === 0)) {
|
|
173
|
-
console.error(`Error: Missing required parameter ${stringify(name)}
|
|
174
|
-
`);
|
|
175
|
-
return process.exit(1);
|
|
176
|
-
}
|
|
177
|
-
mapping[camelCaseName] = value;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
var __accessCheck = (obj, member, msg) => {
|
|
182
|
-
if (!member.has(obj))
|
|
183
|
-
throw TypeError("Cannot " + msg);
|
|
184
|
-
};
|
|
185
|
-
var __privateGet = (obj, member, getter) => {
|
|
186
|
-
__accessCheck(obj, member, "read from private field");
|
|
187
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
188
|
-
};
|
|
189
|
-
var __privateAdd = (obj, member, value) => {
|
|
190
|
-
if (member.has(obj))
|
|
191
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
192
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
193
|
-
};
|
|
194
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
195
|
-
__accessCheck(obj, member, "write to private field");
|
|
196
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
197
|
-
return value;
|
|
198
|
-
};
|
|
199
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _isSingleCommand, isSingleCommand_get, _hasCommands, hasCommands_get;
|
|
200
|
-
const SingleCommand = Symbol("SingleCommand");
|
|
201
|
-
const _Clerc = class {
|
|
202
|
-
constructor() {
|
|
203
|
-
__privateAdd(this, _isSingleCommand);
|
|
204
|
-
__privateAdd(this, _hasCommands);
|
|
205
|
-
__privateAdd(this, _name, "");
|
|
206
|
-
__privateAdd(this, _description, "");
|
|
207
|
-
__privateAdd(this, _version, "");
|
|
208
|
-
__privateAdd(this, _inspectors, []);
|
|
209
|
-
__privateAdd(this, _commands, {});
|
|
210
|
-
__privateAdd(this, _commandEmitter, new liteEmit.LiteEmit());
|
|
211
|
-
}
|
|
212
|
-
get _name() {
|
|
213
|
-
return __privateGet(this, _name);
|
|
214
|
-
}
|
|
215
|
-
get _description() {
|
|
216
|
-
return __privateGet(this, _description);
|
|
217
|
-
}
|
|
218
|
-
get _version() {
|
|
219
|
-
return __privateGet(this, _version);
|
|
220
|
-
}
|
|
221
|
-
get _inspectors() {
|
|
222
|
-
return __privateGet(this, _inspectors);
|
|
223
|
-
}
|
|
224
|
-
get _commands() {
|
|
225
|
-
return __privateGet(this, _commands);
|
|
226
|
-
}
|
|
227
|
-
static create() {
|
|
228
|
-
return new _Clerc();
|
|
229
|
-
}
|
|
230
|
-
name(name) {
|
|
231
|
-
__privateSet(this, _name, name);
|
|
232
|
-
return this;
|
|
233
|
-
}
|
|
234
|
-
description(description) {
|
|
235
|
-
__privateSet(this, _description, description);
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
version(version) {
|
|
239
|
-
__privateSet(this, _version, version);
|
|
240
|
-
return this;
|
|
241
|
-
}
|
|
242
|
-
command(nameOrCommand, description, options) {
|
|
243
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
244
|
-
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
245
|
-
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
246
|
-
if (__privateGet(this, _commands)[name]) {
|
|
247
|
-
if (name === SingleCommand) {
|
|
248
|
-
throw new CommandExistsError("SingleCommand");
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
252
|
-
throw new SingleCommandError();
|
|
253
|
-
}
|
|
254
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
255
|
-
throw new CommonCommandExistsError();
|
|
256
|
-
}
|
|
257
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
258
|
-
throw new SingleCommandAliasError();
|
|
259
|
-
}
|
|
260
|
-
if (name !== SingleCommand) {
|
|
261
|
-
const splitedName = name.split(" ");
|
|
262
|
-
const existedCommandNames = Object.keys(__privateGet(this, _commands)).filter((name2) => typeof name2 === "string").map((name2) => name2.split(" "));
|
|
263
|
-
if (existedCommandNames.some((name2) => utils.arrayStartsWith(splitedName, name2))) {
|
|
264
|
-
throw new ParentCommandExistsError(splitedName.join(" "));
|
|
265
|
-
}
|
|
266
|
-
if (existedCommandNames.some((name2) => utils.arrayStartsWith(name2, splitedName))) {
|
|
267
|
-
throw new SubcommandExistsError(splitedName.join(" "));
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
__privateGet(this, _commands)[name] = !isCommandObject ? { name, description, ...options } : nameOrCommand;
|
|
271
|
-
if (isCommandObject && nameOrCommand.handler) {
|
|
272
|
-
this.on(nameOrCommand.name, nameOrCommand.handler);
|
|
273
|
-
}
|
|
274
|
-
return this;
|
|
275
|
-
}
|
|
276
|
-
on(name, handler) {
|
|
277
|
-
__privateGet(this, _commandEmitter).on(name, handler);
|
|
278
|
-
return this;
|
|
279
|
-
}
|
|
280
|
-
use(plugin) {
|
|
281
|
-
return plugin.setup(this);
|
|
282
|
-
}
|
|
283
|
-
inspector(inspector) {
|
|
284
|
-
__privateGet(this, _inspectors).push(inspector);
|
|
285
|
-
return this;
|
|
286
|
-
}
|
|
287
|
-
parse(argv = resolveArgv()) {
|
|
288
|
-
const name = resolveParametersBeforeFlag(argv, __privateGet(this, _isSingleCommand, isSingleCommand_get));
|
|
289
|
-
const stringName = name.join(" ");
|
|
290
|
-
const getCommand = () => __privateGet(this, _isSingleCommand, isSingleCommand_get) ? __privateGet(this, _commands)[SingleCommand] : resolveCommand(__privateGet(this, _commands), name);
|
|
291
|
-
const getContext = () => {
|
|
292
|
-
const command = getCommand();
|
|
293
|
-
const isCommandResolved = !!command;
|
|
294
|
-
const parsed = typeFlag.typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
295
|
-
const { _: args, flags } = parsed;
|
|
296
|
-
let parameters = __privateGet(this, _isSingleCommand, isSingleCommand_get) || !isCommandResolved ? args : args.slice(command.name.split(" ").length);
|
|
297
|
-
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
298
|
-
const hasEof = commandParameters.indexOf("--");
|
|
299
|
-
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
300
|
-
const mapping = /* @__PURE__ */ Object.create(null);
|
|
301
|
-
if (hasEof > -1 && eofParameters.length > 0) {
|
|
302
|
-
commandParameters = commandParameters.slice(0, hasEof);
|
|
303
|
-
const eofArguments = parsed._["--"];
|
|
304
|
-
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
305
|
-
mapParametersToArguments(
|
|
306
|
-
mapping,
|
|
307
|
-
parseParameters(commandParameters),
|
|
308
|
-
parameters
|
|
309
|
-
);
|
|
310
|
-
mapParametersToArguments(
|
|
311
|
-
mapping,
|
|
312
|
-
parseParameters(eofParameters),
|
|
313
|
-
eofArguments
|
|
314
|
-
);
|
|
315
|
-
} else {
|
|
316
|
-
mapParametersToArguments(
|
|
317
|
-
mapping,
|
|
318
|
-
parseParameters(commandParameters),
|
|
319
|
-
parameters
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
const context = {
|
|
323
|
-
name: command == null ? void 0 : command.name,
|
|
324
|
-
resolved: isCommandResolved,
|
|
325
|
-
isSingleCommand: __privateGet(this, _isSingleCommand, isSingleCommand_get),
|
|
326
|
-
raw: { ...parsed, parameters },
|
|
327
|
-
parameters: mapping,
|
|
328
|
-
flags,
|
|
329
|
-
unknownFlags: parsed.unknownFlags,
|
|
330
|
-
cli: this
|
|
331
|
-
};
|
|
332
|
-
return context;
|
|
333
|
-
};
|
|
334
|
-
const emitHandler = () => {
|
|
335
|
-
const command = getCommand();
|
|
336
|
-
const handlerContext = getContext();
|
|
337
|
-
if (!command) {
|
|
338
|
-
throw new NoSuchCommandError(stringName);
|
|
339
|
-
}
|
|
340
|
-
__privateGet(this, _commandEmitter).emit(command.name, handlerContext);
|
|
341
|
-
};
|
|
342
|
-
const inspectors = [...__privateGet(this, _inspectors), emitHandler];
|
|
343
|
-
const callInspector = compose(inspectors);
|
|
344
|
-
callInspector(getContext);
|
|
345
|
-
return this;
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
let Clerc = _Clerc;
|
|
349
|
-
_name = new WeakMap();
|
|
350
|
-
_description = new WeakMap();
|
|
351
|
-
_version = new WeakMap();
|
|
352
|
-
_inspectors = new WeakMap();
|
|
353
|
-
_commands = new WeakMap();
|
|
354
|
-
_commandEmitter = new WeakMap();
|
|
355
|
-
_isSingleCommand = new WeakSet();
|
|
356
|
-
isSingleCommand_get = function() {
|
|
357
|
-
return __privateGet(this, _commands)[SingleCommand] !== void 0;
|
|
358
|
-
};
|
|
359
|
-
_hasCommands = new WeakSet();
|
|
360
|
-
hasCommands_get = function() {
|
|
361
|
-
return Object.keys(__privateGet(this, _commands)).length > 0;
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
const definePlugin = (p) => p;
|
|
365
|
-
const defineHandler = (_cli, _key, handler) => handler;
|
|
366
|
-
const defineInspector = (_cli, inspector) => inspector;
|
|
367
|
-
const defineCommand = (c) => c;
|
|
368
|
-
const defineCommandWithHandler = (c) => c;
|
|
369
|
-
|
|
370
|
-
exports.Clerc = Clerc;
|
|
371
|
-
exports.CommandExistsError = CommandExistsError;
|
|
372
|
-
exports.CommandNameConflictError = CommandNameConflictError;
|
|
373
|
-
exports.CommonCommandExistsError = CommonCommandExistsError;
|
|
374
|
-
exports.MultipleCommandsMatchedError = MultipleCommandsMatchedError;
|
|
375
|
-
exports.NoSuchCommandError = NoSuchCommandError;
|
|
376
|
-
exports.ParentCommandExistsError = ParentCommandExistsError;
|
|
377
|
-
exports.SingleCommand = SingleCommand;
|
|
378
|
-
exports.SingleCommandAliasError = SingleCommandAliasError;
|
|
379
|
-
exports.SingleCommandError = SingleCommandError;
|
|
380
|
-
exports.SubcommandExistsError = SubcommandExistsError;
|
|
381
|
-
exports.compose = compose;
|
|
382
|
-
exports.defineCommand = defineCommand;
|
|
383
|
-
exports.defineCommandWithHandler = defineCommandWithHandler;
|
|
384
|
-
exports.defineHandler = defineHandler;
|
|
385
|
-
exports.defineInspector = defineInspector;
|
|
386
|
-
exports.definePlugin = definePlugin;
|
|
387
|
-
exports.mapParametersToArguments = mapParametersToArguments;
|
|
388
|
-
exports.parseParameters = parseParameters;
|
|
389
|
-
exports.resolveArgv = resolveArgv;
|
|
390
|
-
exports.resolveCommand = resolveCommand;
|
|
391
|
-
exports.resolveFlattenCommands = resolveFlattenCommands;
|
|
392
|
-
exports.resolveParametersBeforeFlag = resolveParametersBeforeFlag;
|
|
393
|
-
exports.resolveRootCommands = resolveRootCommands;
|
|
394
|
-
exports.resolveSubcommandsByParent = resolveSubcommandsByParent;
|
|
5
|
+
var core = require('@clerc/core');
|
|
6
|
+
var pluginCompletions = require('@clerc/plugin-completions');
|
|
7
|
+
var pluginHelp = require('@clerc/plugin-help');
|
|
8
|
+
var pluginNotFound = require('@clerc/plugin-not-found');
|
|
9
|
+
var pluginStrictFlags = require('@clerc/plugin-strict-flags');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
Object.keys(core).forEach(function (k) {
|
|
14
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return core[k]; }
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
Object.keys(pluginCompletions).forEach(function (k) {
|
|
20
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return pluginCompletions[k]; }
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
Object.keys(pluginHelp).forEach(function (k) {
|
|
26
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return pluginHelp[k]; }
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
Object.keys(pluginNotFound).forEach(function (k) {
|
|
32
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () { return pluginNotFound[k]; }
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
Object.keys(pluginStrictFlags).forEach(function (k) {
|
|
38
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () { return pluginStrictFlags[k]; }
|
|
41
|
+
});
|
|
42
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,323 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type TypeFunctionArray<ReturnType> = readonly [TypeFunction<ReturnType>];
|
|
7
|
-
type FlagType<ReturnType = any> = TypeFunction<ReturnType> | TypeFunctionArray<ReturnType>;
|
|
8
|
-
type FlagSchemaBase<TF> = {
|
|
9
|
-
/**
|
|
10
|
-
Type of the flag as a function that parses the argv string and returns the parsed value.
|
|
11
|
-
|
|
12
|
-
@example
|
|
13
|
-
```
|
|
14
|
-
type: String
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
@example Wrap in an array to accept multiple values.
|
|
18
|
-
```
|
|
19
|
-
type: [Boolean]
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
@example Custom function type that uses moment.js to parse string as date.
|
|
23
|
-
```
|
|
24
|
-
type: function CustomDate(value: string) {
|
|
25
|
-
return moment(value).toDate();
|
|
26
|
-
}
|
|
27
|
-
```
|
|
28
|
-
*/
|
|
29
|
-
type: TF;
|
|
30
|
-
/**
|
|
31
|
-
A single-character alias for the flag.
|
|
32
|
-
|
|
33
|
-
@example
|
|
34
|
-
```
|
|
35
|
-
alias: 's'
|
|
36
|
-
```
|
|
37
|
-
*/
|
|
38
|
-
alias?: string;
|
|
39
|
-
} & Record<PropertyKey, unknown>;
|
|
40
|
-
type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
|
|
41
|
-
/**
|
|
42
|
-
Default value of the flag. Also accepts a function that returns the default value.
|
|
43
|
-
[Default: undefined]
|
|
44
|
-
|
|
45
|
-
@example
|
|
46
|
-
```
|
|
47
|
-
default: 'hello'
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
@example
|
|
51
|
-
```
|
|
52
|
-
default: () => [1, 2, 3]
|
|
53
|
-
```
|
|
54
|
-
*/
|
|
55
|
-
default: DefaultType | (() => DefaultType);
|
|
56
|
-
};
|
|
57
|
-
type FlagSchema<TF = FlagType> = (FlagSchemaBase<TF> | FlagSchemaDefault<TF>);
|
|
58
|
-
type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions);
|
|
59
|
-
type Flags<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
|
|
60
|
-
type InferFlagType<Flag extends FlagTypeOrSchema> = (Flag extends (TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>>) ? (Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D> ? T[] | D : T[]) : (Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>> ? (Flag extends FlagSchemaDefault<TypeFunction<T>, infer D> ? T | D : T | undefined) : never));
|
|
61
|
-
interface ParsedFlags<Schemas = Record<string, unknown>> {
|
|
62
|
-
flags: Schemas;
|
|
63
|
-
unknownFlags: Record<string, (string | boolean)[]>;
|
|
64
|
-
_: string[] & {
|
|
65
|
-
[DOUBLE_DASH]: string[];
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
type TypeFlag<Schemas extends Flags> = ParsedFlags<{
|
|
69
|
-
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
|
|
70
|
-
}>;
|
|
71
|
-
|
|
72
|
-
type FlagOptions = FlagSchema & {
|
|
73
|
-
description?: string;
|
|
74
|
-
};
|
|
75
|
-
type Flag = FlagOptions & {
|
|
76
|
-
name: string;
|
|
77
|
-
};
|
|
78
|
-
declare interface CommandCustomProperties {
|
|
79
|
-
}
|
|
80
|
-
interface CommandOptions<P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>> extends CommandCustomProperties {
|
|
81
|
-
alias?: A;
|
|
82
|
-
parameters?: P;
|
|
83
|
-
flags?: F;
|
|
84
|
-
examples?: [string, string][];
|
|
85
|
-
notes?: string[];
|
|
86
|
-
}
|
|
87
|
-
type Command<N extends string | SingleCommandType = string, O extends CommandOptions = CommandOptions> = O & {
|
|
88
|
-
name: N;
|
|
89
|
-
description: string;
|
|
90
|
-
};
|
|
91
|
-
type CommandAlias<N extends string | SingleCommandType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
92
|
-
__isAlias?: true;
|
|
93
|
-
};
|
|
94
|
-
type CommandWithHandler<N extends string | SingleCommandType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
95
|
-
handler?: HandlerInCommand<Record<N, Command<N, O>>, N>;
|
|
96
|
-
};
|
|
97
|
-
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
98
|
-
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
99
|
-
type CommandRecord = Dict<Command> & {
|
|
100
|
-
[SingleCommand]?: Command;
|
|
101
|
-
};
|
|
102
|
-
type MakeEventMap<T extends CommandRecord> = {
|
|
103
|
-
[K in keyof T]: [InspectorContext];
|
|
104
|
-
};
|
|
105
|
-
type PossibleInputKind = string | number | boolean | Dict<any>;
|
|
106
|
-
type NonNullableFlag<T extends Dict<FlagOptions> | undefined> = T extends undefined ? {} : NonNullable<T>;
|
|
107
|
-
type NonNullableParameters<T extends string[] | undefined> = T extends undefined ? [] : NonNullable<T>;
|
|
108
|
-
interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
|
|
109
|
-
name?: N;
|
|
110
|
-
resolved: boolean;
|
|
111
|
-
isSingleCommand: boolean;
|
|
112
|
-
raw: TypeFlag<NonNullableFlag<C[N]["flags"]>> & {
|
|
113
|
-
parameters: string[];
|
|
114
|
-
};
|
|
115
|
-
parameters: {
|
|
116
|
-
[Parameter in [...NonNullableParameters<C[N]["parameters"]>][number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
|
|
117
|
-
};
|
|
118
|
-
unknownFlags: ParsedFlags["unknownFlags"];
|
|
119
|
-
flags: TypeFlag<NonNullableFlag<C[N]["flags"]>>["flags"];
|
|
120
|
-
cli: Clerc<C>;
|
|
121
|
-
}
|
|
122
|
-
type Handler<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
|
|
123
|
-
type HandlerInCommand<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K> & {
|
|
124
|
-
name: K;
|
|
125
|
-
flags: Exclude<TypeFlag<NonNullableFlag<C[K]["flags"]>>["flags"], Record<string, never>>;
|
|
126
|
-
}) => void;
|
|
127
|
-
type FallbackType<T, U> = {} extends T ? U : T;
|
|
128
|
-
type InspectorContext<C extends CommandRecord = CommandRecord> = HandlerContext<C> & {
|
|
129
|
-
flags: FallbackType<TypeFlag<NonNullableFlag<C[keyof C]["flags"]>>["flags"], Dict<any>>;
|
|
130
|
-
};
|
|
131
|
-
type Inspector<C extends CommandRecord = CommandRecord> = (ctx: InspectorContext<C>, next: () => void) => void;
|
|
132
|
-
interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
133
|
-
setup: (cli: T) => U;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
declare const SingleCommand: unique symbol;
|
|
137
|
-
type SingleCommandType = typeof SingleCommand;
|
|
138
|
-
declare class Clerc<C extends CommandRecord = {}> {
|
|
139
|
-
#private;
|
|
140
|
-
private constructor();
|
|
141
|
-
get _name(): string;
|
|
142
|
-
get _description(): string;
|
|
143
|
-
get _version(): string;
|
|
144
|
-
get _inspectors(): Inspector<CommandRecord>[];
|
|
145
|
-
get _commands(): C;
|
|
146
|
-
/**
|
|
147
|
-
* Create a new cli
|
|
148
|
-
* @returns
|
|
149
|
-
* @example
|
|
150
|
-
* ```ts
|
|
151
|
-
* const cli = Clerc.create()
|
|
152
|
-
* ```
|
|
153
|
-
*/
|
|
154
|
-
static create(): Clerc<{}>;
|
|
155
|
-
/**
|
|
156
|
-
* Set the name of the cli
|
|
157
|
-
* @param name
|
|
158
|
-
* @returns
|
|
159
|
-
* @example
|
|
160
|
-
* ```ts
|
|
161
|
-
* Clerc.create()
|
|
162
|
-
* .name("test")
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
name(name: string): this;
|
|
166
|
-
/**
|
|
167
|
-
* Set the description of the cli
|
|
168
|
-
* @param description
|
|
169
|
-
* @returns
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* Clerc.create()
|
|
173
|
-
* .description("test cli")
|
|
174
|
-
*/
|
|
175
|
-
description(description: string): this;
|
|
176
|
-
/**
|
|
177
|
-
* Set the version of the cli
|
|
178
|
-
* @param version
|
|
179
|
-
* @returns
|
|
180
|
-
* @example
|
|
181
|
-
* ```ts
|
|
182
|
-
* Clerc.create()
|
|
183
|
-
* .version("1.0.0")
|
|
184
|
-
*/
|
|
185
|
-
version(version: string): this;
|
|
186
|
-
/**
|
|
187
|
-
* Register a command
|
|
188
|
-
* @param name
|
|
189
|
-
* @param description
|
|
190
|
-
* @param options
|
|
191
|
-
* @returns
|
|
192
|
-
* @example
|
|
193
|
-
* ```ts
|
|
194
|
-
* Clerc.create()
|
|
195
|
-
* .command("test", "test command", {
|
|
196
|
-
* alias: "t",
|
|
197
|
-
* flags: {
|
|
198
|
-
* foo: {
|
|
199
|
-
* alias: "f",
|
|
200
|
-
* description: "foo flag",
|
|
201
|
-
* }
|
|
202
|
-
* }
|
|
203
|
-
* })
|
|
204
|
-
* ```
|
|
205
|
-
* @example
|
|
206
|
-
* ```ts
|
|
207
|
-
* Clerc.create()
|
|
208
|
-
* .command("", "single command", {
|
|
209
|
-
* flags: {
|
|
210
|
-
* foo: {
|
|
211
|
-
* alias: "f",
|
|
212
|
-
* description: "foo flag",
|
|
213
|
-
* }
|
|
214
|
-
* }
|
|
215
|
-
* })
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
command<N extends string | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
219
|
-
command<N extends string | SingleCommandType, P extends string[], O extends CommandOptions<[...P]>>(name: N, description: string, options?: O & CommandOptions<[...P]>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
220
|
-
/**
|
|
221
|
-
* Register a handler
|
|
222
|
-
* @param name
|
|
223
|
-
* @param handler
|
|
224
|
-
* @returns
|
|
225
|
-
* @example
|
|
226
|
-
* ```ts
|
|
227
|
-
* Clerc.create()
|
|
228
|
-
* .command("test", "test command")
|
|
229
|
-
* .on("test", (ctx) => {
|
|
230
|
-
* console.log(ctx);
|
|
231
|
-
* })
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
on<K extends keyof CM, CM extends this["_commands"] = this["_commands"]>(name: LiteralUnion<K, string>, handler: Handler<CM, K>): this;
|
|
235
|
-
/**
|
|
236
|
-
* Use a plugin
|
|
237
|
-
* @param plugin
|
|
238
|
-
* @returns
|
|
239
|
-
* @example
|
|
240
|
-
* ```ts
|
|
241
|
-
* Clerc.create()
|
|
242
|
-
* .use(plugin)
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
|
-
use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): U;
|
|
246
|
-
/**
|
|
247
|
-
* Register a inspector
|
|
248
|
-
* @param inspector
|
|
249
|
-
* @returns
|
|
250
|
-
* @example
|
|
251
|
-
* ```ts
|
|
252
|
-
* Clerc.create()
|
|
253
|
-
* .inspector((ctx, next) => {
|
|
254
|
-
* console.log(ctx);
|
|
255
|
-
* next();
|
|
256
|
-
* })
|
|
257
|
-
* ```
|
|
258
|
-
*/
|
|
259
|
-
inspector(inspector: Inspector): this;
|
|
260
|
-
/**
|
|
261
|
-
* Parse the command line arguments
|
|
262
|
-
* @param args
|
|
263
|
-
* @returns
|
|
264
|
-
* @example
|
|
265
|
-
* ```ts
|
|
266
|
-
* Clerc.create()
|
|
267
|
-
* .parse(process.argv.slice(2)) // Optional
|
|
268
|
-
* ```
|
|
269
|
-
*/
|
|
270
|
-
parse(argv?: string[]): this;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
274
|
-
declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
|
|
275
|
-
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
276
|
-
declare const defineCommand: <N extends string | typeof SingleCommand, P extends string[], O extends CommandOptions<[...P], MaybeArray<string>, Dict<FlagOptions>>>(c: Command<N, O>) => Command<N, O>;
|
|
277
|
-
declare const defineCommandWithHandler: <N extends string | typeof SingleCommand, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>) => CommandWithHandler<N, O & CommandOptions<[...P], A, F>>;
|
|
278
|
-
|
|
279
|
-
declare class SingleCommandError extends Error {
|
|
280
|
-
constructor();
|
|
281
|
-
}
|
|
282
|
-
declare class SingleCommandAliasError extends Error {
|
|
283
|
-
constructor();
|
|
284
|
-
}
|
|
285
|
-
declare class CommandExistsError extends Error {
|
|
286
|
-
constructor(name: string);
|
|
287
|
-
}
|
|
288
|
-
declare class CommonCommandExistsError extends Error {
|
|
289
|
-
constructor();
|
|
290
|
-
}
|
|
291
|
-
declare class NoSuchCommandError extends Error {
|
|
292
|
-
constructor(name: string);
|
|
293
|
-
}
|
|
294
|
-
declare class ParentCommandExistsError extends Error {
|
|
295
|
-
constructor(name: string);
|
|
296
|
-
}
|
|
297
|
-
declare class SubcommandExistsError extends Error {
|
|
298
|
-
constructor(name: string);
|
|
299
|
-
}
|
|
300
|
-
declare class MultipleCommandsMatchedError extends Error {
|
|
301
|
-
constructor(name: string);
|
|
302
|
-
}
|
|
303
|
-
declare class CommandNameConflictError extends Error {
|
|
304
|
-
constructor(n1: string, n2: string);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[], CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
308
|
-
declare function resolveCommand(commands: CommandRecord, name: string | string[] | SingleCommandType): Command | undefined;
|
|
309
|
-
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
310
|
-
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
311
|
-
declare function resolveParametersBeforeFlag(argv: string[], isSingleCommand: boolean): string[];
|
|
312
|
-
declare const resolveArgv: () => string[];
|
|
313
|
-
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
314
|
-
|
|
315
|
-
interface ParsedParameter {
|
|
316
|
-
name: string;
|
|
317
|
-
required: boolean;
|
|
318
|
-
spread: boolean;
|
|
319
|
-
}
|
|
320
|
-
declare function parseParameters(parameters: string[]): ParsedParameter[];
|
|
321
|
-
declare function mapParametersToArguments(mapping: Record<string, string | string[]>, parameters: ParsedParameter[], cliArguments: string[]): undefined;
|
|
322
|
-
|
|
323
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandWithHandler, CommonCommandExistsError, FallbackType, Flag, FlagOptions, Handler, HandlerContext, HandlerInCommand, Inspector, InspectorContext, MakeEventMap, MultipleCommandsMatchedError, NoSuchCommandError, ParentCommandExistsError, Plugin, PossibleInputKind, SingleCommand, SingleCommandAliasError, SingleCommandError, SingleCommandType, SubcommandExistsError, compose, defineCommand, defineCommandWithHandler, defineHandler, defineInspector, definePlugin, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
|
1
|
+
export * from '@clerc/core';
|
|
2
|
+
export * from '@clerc/plugin-completions';
|
|
3
|
+
export * from '@clerc/plugin-help';
|
|
4
|
+
export * from '@clerc/plugin-not-found';
|
|
5
|
+
export * from '@clerc/plugin-strict-flags';
|
package/dist/index.mjs
CHANGED
|
@@ -1,366 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class SingleCommandError extends Error {
|
|
7
|
-
constructor() {
|
|
8
|
-
super("Single command mode enabled.");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class SingleCommandAliasError extends Error {
|
|
12
|
-
constructor() {
|
|
13
|
-
super("Single command cannot have alias.");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
class CommandExistsError extends Error {
|
|
17
|
-
constructor(name) {
|
|
18
|
-
super(`Command "${name}" exists.`);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
class CommonCommandExistsError extends Error {
|
|
22
|
-
constructor() {
|
|
23
|
-
super("Common command exists.");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
class NoSuchCommandError extends Error {
|
|
27
|
-
constructor(name) {
|
|
28
|
-
super(`No such command: ${name}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
class ParentCommandExistsError extends Error {
|
|
32
|
-
constructor(name) {
|
|
33
|
-
super(`Command "${name}" cannot exist with its parent`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
class SubcommandExistsError extends Error {
|
|
37
|
-
constructor(name) {
|
|
38
|
-
super(`Command "${name}" cannot exist with its subcommand`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
class MultipleCommandsMatchedError extends Error {
|
|
42
|
-
constructor(name) {
|
|
43
|
-
super(`Multiple commands matched: ${name}`);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
class CommandNameConflictError extends Error {
|
|
47
|
-
constructor(n1, n2) {
|
|
48
|
-
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function resolveFlattenCommands(commands) {
|
|
53
|
-
const commandsMap = /* @__PURE__ */ new Map();
|
|
54
|
-
for (const command of Object.values(commands)) {
|
|
55
|
-
if (command.alias) {
|
|
56
|
-
const aliases = mustArray(command.alias);
|
|
57
|
-
for (const alias of aliases) {
|
|
58
|
-
if (alias in commands) {
|
|
59
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
60
|
-
}
|
|
61
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
commandsMap.set(command.name.split(" "), command);
|
|
65
|
-
}
|
|
66
|
-
return commandsMap;
|
|
67
|
-
}
|
|
68
|
-
function resolveCommand(commands, name) {
|
|
69
|
-
if (name === SingleCommand) {
|
|
70
|
-
return commands[SingleCommand];
|
|
71
|
-
}
|
|
72
|
-
const nameArr = mustArray(name);
|
|
73
|
-
const commandsMap = resolveFlattenCommands(commands);
|
|
74
|
-
const possibleCommands = [];
|
|
75
|
-
commandsMap.forEach((v, k) => {
|
|
76
|
-
if (arrayStartsWith(nameArr, k)) {
|
|
77
|
-
possibleCommands.push(v);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
if (possibleCommands.length > 1) {
|
|
81
|
-
const matchedCommandNames = possibleCommands.map((c) => c.name).join(", ");
|
|
82
|
-
throw new MultipleCommandsMatchedError(matchedCommandNames);
|
|
83
|
-
}
|
|
84
|
-
return possibleCommands[0];
|
|
85
|
-
}
|
|
86
|
-
function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
87
|
-
const parentArr = parent === "" ? [] : Array.isArray(parent) ? parent : parent.split(" ");
|
|
88
|
-
return Object.values(commands).filter((c) => {
|
|
89
|
-
const commandNameArr = c.name.split(" ");
|
|
90
|
-
return arrayStartsWith(commandNameArr, parentArr) && commandNameArr.length - parentArr.length <= depth;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
94
|
-
function resolveParametersBeforeFlag(argv, isSingleCommand) {
|
|
95
|
-
if (isSingleCommand) {
|
|
96
|
-
return [];
|
|
97
|
-
}
|
|
98
|
-
const parameters = [];
|
|
99
|
-
for (const arg of argv) {
|
|
100
|
-
if (arg.startsWith("-")) {
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
parameters.push(arg);
|
|
104
|
-
}
|
|
105
|
-
return parameters;
|
|
106
|
-
}
|
|
107
|
-
const resolveArgv = () => isNode() ? process.argv.slice(2) : isDeno() ? Deno.args : [];
|
|
108
|
-
function compose(inspectors) {
|
|
109
|
-
return (getCtx) => {
|
|
110
|
-
return dispatch(0);
|
|
111
|
-
function dispatch(i) {
|
|
112
|
-
const inspector = inspectors[i];
|
|
113
|
-
return inspector(getCtx(), dispatch.bind(null, i + 1));
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const { stringify } = JSON;
|
|
119
|
-
function parseParameters(parameters) {
|
|
120
|
-
const parsedParameters = [];
|
|
121
|
-
let hasOptional;
|
|
122
|
-
let hasSpread;
|
|
123
|
-
for (const parameter of parameters) {
|
|
124
|
-
if (hasSpread) {
|
|
125
|
-
throw new Error(`Invalid parameter: Spread parameter ${stringify(hasSpread)} must be last`);
|
|
126
|
-
}
|
|
127
|
-
const firstCharacter = parameter[0];
|
|
128
|
-
const lastCharacter = parameter[parameter.length - 1];
|
|
129
|
-
let required;
|
|
130
|
-
if (firstCharacter === "<" && lastCharacter === ">") {
|
|
131
|
-
required = true;
|
|
132
|
-
if (hasOptional) {
|
|
133
|
-
throw new Error(`Invalid parameter: Required parameter ${stringify(parameter)} cannot come after optional parameter ${stringify(hasOptional)}`);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (firstCharacter === "[" && lastCharacter === "]") {
|
|
137
|
-
required = false;
|
|
138
|
-
hasOptional = parameter;
|
|
139
|
-
}
|
|
140
|
-
if (required === void 0) {
|
|
141
|
-
throw new Error(`Invalid parameter: ${stringify(parameter)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);
|
|
142
|
-
}
|
|
143
|
-
let name = parameter.slice(1, -1);
|
|
144
|
-
const spread = name.slice(-3) === "...";
|
|
145
|
-
if (spread) {
|
|
146
|
-
hasSpread = parameter;
|
|
147
|
-
name = name.slice(0, -3);
|
|
148
|
-
}
|
|
149
|
-
parsedParameters.push({
|
|
150
|
-
name,
|
|
151
|
-
required,
|
|
152
|
-
spread
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
return parsedParameters;
|
|
156
|
-
}
|
|
157
|
-
function mapParametersToArguments(mapping, parameters, cliArguments) {
|
|
158
|
-
for (let i = 0; i < parameters.length; i += 1) {
|
|
159
|
-
const { name, required, spread } = parameters[i];
|
|
160
|
-
const camelCaseName = camelCase(name);
|
|
161
|
-
if (camelCaseName in mapping) {
|
|
162
|
-
throw new Error(`Invalid parameter: ${stringify(name)} is used more than once.`);
|
|
163
|
-
}
|
|
164
|
-
const value = spread ? cliArguments.slice(i) : cliArguments[i];
|
|
165
|
-
if (spread) {
|
|
166
|
-
i = parameters.length;
|
|
167
|
-
}
|
|
168
|
-
if (required && (!value || spread && value.length === 0)) {
|
|
169
|
-
console.error(`Error: Missing required parameter ${stringify(name)}
|
|
170
|
-
`);
|
|
171
|
-
return process.exit(1);
|
|
172
|
-
}
|
|
173
|
-
mapping[camelCaseName] = value;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
var __accessCheck = (obj, member, msg) => {
|
|
178
|
-
if (!member.has(obj))
|
|
179
|
-
throw TypeError("Cannot " + msg);
|
|
180
|
-
};
|
|
181
|
-
var __privateGet = (obj, member, getter) => {
|
|
182
|
-
__accessCheck(obj, member, "read from private field");
|
|
183
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
184
|
-
};
|
|
185
|
-
var __privateAdd = (obj, member, value) => {
|
|
186
|
-
if (member.has(obj))
|
|
187
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
188
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
189
|
-
};
|
|
190
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
191
|
-
__accessCheck(obj, member, "write to private field");
|
|
192
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
193
|
-
return value;
|
|
194
|
-
};
|
|
195
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _isSingleCommand, isSingleCommand_get, _hasCommands, hasCommands_get;
|
|
196
|
-
const SingleCommand = Symbol("SingleCommand");
|
|
197
|
-
const _Clerc = class {
|
|
198
|
-
constructor() {
|
|
199
|
-
__privateAdd(this, _isSingleCommand);
|
|
200
|
-
__privateAdd(this, _hasCommands);
|
|
201
|
-
__privateAdd(this, _name, "");
|
|
202
|
-
__privateAdd(this, _description, "");
|
|
203
|
-
__privateAdd(this, _version, "");
|
|
204
|
-
__privateAdd(this, _inspectors, []);
|
|
205
|
-
__privateAdd(this, _commands, {});
|
|
206
|
-
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
207
|
-
}
|
|
208
|
-
get _name() {
|
|
209
|
-
return __privateGet(this, _name);
|
|
210
|
-
}
|
|
211
|
-
get _description() {
|
|
212
|
-
return __privateGet(this, _description);
|
|
213
|
-
}
|
|
214
|
-
get _version() {
|
|
215
|
-
return __privateGet(this, _version);
|
|
216
|
-
}
|
|
217
|
-
get _inspectors() {
|
|
218
|
-
return __privateGet(this, _inspectors);
|
|
219
|
-
}
|
|
220
|
-
get _commands() {
|
|
221
|
-
return __privateGet(this, _commands);
|
|
222
|
-
}
|
|
223
|
-
static create() {
|
|
224
|
-
return new _Clerc();
|
|
225
|
-
}
|
|
226
|
-
name(name) {
|
|
227
|
-
__privateSet(this, _name, name);
|
|
228
|
-
return this;
|
|
229
|
-
}
|
|
230
|
-
description(description) {
|
|
231
|
-
__privateSet(this, _description, description);
|
|
232
|
-
return this;
|
|
233
|
-
}
|
|
234
|
-
version(version) {
|
|
235
|
-
__privateSet(this, _version, version);
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
command(nameOrCommand, description, options) {
|
|
239
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
240
|
-
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
241
|
-
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
242
|
-
if (__privateGet(this, _commands)[name]) {
|
|
243
|
-
if (name === SingleCommand) {
|
|
244
|
-
throw new CommandExistsError("SingleCommand");
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
248
|
-
throw new SingleCommandError();
|
|
249
|
-
}
|
|
250
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
251
|
-
throw new CommonCommandExistsError();
|
|
252
|
-
}
|
|
253
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
254
|
-
throw new SingleCommandAliasError();
|
|
255
|
-
}
|
|
256
|
-
if (name !== SingleCommand) {
|
|
257
|
-
const splitedName = name.split(" ");
|
|
258
|
-
const existedCommandNames = Object.keys(__privateGet(this, _commands)).filter((name2) => typeof name2 === "string").map((name2) => name2.split(" "));
|
|
259
|
-
if (existedCommandNames.some((name2) => arrayStartsWith(splitedName, name2))) {
|
|
260
|
-
throw new ParentCommandExistsError(splitedName.join(" "));
|
|
261
|
-
}
|
|
262
|
-
if (existedCommandNames.some((name2) => arrayStartsWith(name2, splitedName))) {
|
|
263
|
-
throw new SubcommandExistsError(splitedName.join(" "));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
__privateGet(this, _commands)[name] = !isCommandObject ? { name, description, ...options } : nameOrCommand;
|
|
267
|
-
if (isCommandObject && nameOrCommand.handler) {
|
|
268
|
-
this.on(nameOrCommand.name, nameOrCommand.handler);
|
|
269
|
-
}
|
|
270
|
-
return this;
|
|
271
|
-
}
|
|
272
|
-
on(name, handler) {
|
|
273
|
-
__privateGet(this, _commandEmitter).on(name, handler);
|
|
274
|
-
return this;
|
|
275
|
-
}
|
|
276
|
-
use(plugin) {
|
|
277
|
-
return plugin.setup(this);
|
|
278
|
-
}
|
|
279
|
-
inspector(inspector) {
|
|
280
|
-
__privateGet(this, _inspectors).push(inspector);
|
|
281
|
-
return this;
|
|
282
|
-
}
|
|
283
|
-
parse(argv = resolveArgv()) {
|
|
284
|
-
const name = resolveParametersBeforeFlag(argv, __privateGet(this, _isSingleCommand, isSingleCommand_get));
|
|
285
|
-
const stringName = name.join(" ");
|
|
286
|
-
const getCommand = () => __privateGet(this, _isSingleCommand, isSingleCommand_get) ? __privateGet(this, _commands)[SingleCommand] : resolveCommand(__privateGet(this, _commands), name);
|
|
287
|
-
const getContext = () => {
|
|
288
|
-
const command = getCommand();
|
|
289
|
-
const isCommandResolved = !!command;
|
|
290
|
-
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
291
|
-
const { _: args, flags } = parsed;
|
|
292
|
-
let parameters = __privateGet(this, _isSingleCommand, isSingleCommand_get) || !isCommandResolved ? args : args.slice(command.name.split(" ").length);
|
|
293
|
-
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
294
|
-
const hasEof = commandParameters.indexOf("--");
|
|
295
|
-
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
296
|
-
const mapping = /* @__PURE__ */ Object.create(null);
|
|
297
|
-
if (hasEof > -1 && eofParameters.length > 0) {
|
|
298
|
-
commandParameters = commandParameters.slice(0, hasEof);
|
|
299
|
-
const eofArguments = parsed._["--"];
|
|
300
|
-
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
301
|
-
mapParametersToArguments(
|
|
302
|
-
mapping,
|
|
303
|
-
parseParameters(commandParameters),
|
|
304
|
-
parameters
|
|
305
|
-
);
|
|
306
|
-
mapParametersToArguments(
|
|
307
|
-
mapping,
|
|
308
|
-
parseParameters(eofParameters),
|
|
309
|
-
eofArguments
|
|
310
|
-
);
|
|
311
|
-
} else {
|
|
312
|
-
mapParametersToArguments(
|
|
313
|
-
mapping,
|
|
314
|
-
parseParameters(commandParameters),
|
|
315
|
-
parameters
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
const context = {
|
|
319
|
-
name: command == null ? void 0 : command.name,
|
|
320
|
-
resolved: isCommandResolved,
|
|
321
|
-
isSingleCommand: __privateGet(this, _isSingleCommand, isSingleCommand_get),
|
|
322
|
-
raw: { ...parsed, parameters },
|
|
323
|
-
parameters: mapping,
|
|
324
|
-
flags,
|
|
325
|
-
unknownFlags: parsed.unknownFlags,
|
|
326
|
-
cli: this
|
|
327
|
-
};
|
|
328
|
-
return context;
|
|
329
|
-
};
|
|
330
|
-
const emitHandler = () => {
|
|
331
|
-
const command = getCommand();
|
|
332
|
-
const handlerContext = getContext();
|
|
333
|
-
if (!command) {
|
|
334
|
-
throw new NoSuchCommandError(stringName);
|
|
335
|
-
}
|
|
336
|
-
__privateGet(this, _commandEmitter).emit(command.name, handlerContext);
|
|
337
|
-
};
|
|
338
|
-
const inspectors = [...__privateGet(this, _inspectors), emitHandler];
|
|
339
|
-
const callInspector = compose(inspectors);
|
|
340
|
-
callInspector(getContext);
|
|
341
|
-
return this;
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
let Clerc = _Clerc;
|
|
345
|
-
_name = new WeakMap();
|
|
346
|
-
_description = new WeakMap();
|
|
347
|
-
_version = new WeakMap();
|
|
348
|
-
_inspectors = new WeakMap();
|
|
349
|
-
_commands = new WeakMap();
|
|
350
|
-
_commandEmitter = new WeakMap();
|
|
351
|
-
_isSingleCommand = new WeakSet();
|
|
352
|
-
isSingleCommand_get = function() {
|
|
353
|
-
return __privateGet(this, _commands)[SingleCommand] !== void 0;
|
|
354
|
-
};
|
|
355
|
-
_hasCommands = new WeakSet();
|
|
356
|
-
hasCommands_get = function() {
|
|
357
|
-
return Object.keys(__privateGet(this, _commands)).length > 0;
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const definePlugin = (p) => p;
|
|
361
|
-
const defineHandler = (_cli, _key, handler) => handler;
|
|
362
|
-
const defineInspector = (_cli, inspector) => inspector;
|
|
363
|
-
const defineCommand = (c) => c;
|
|
364
|
-
const defineCommandWithHandler = (c) => c;
|
|
365
|
-
|
|
366
|
-
export { Clerc, CommandExistsError, CommandNameConflictError, CommonCommandExistsError, MultipleCommandsMatchedError, NoSuchCommandError, ParentCommandExistsError, SingleCommand, SingleCommandAliasError, SingleCommandError, SubcommandExistsError, compose, defineCommand, defineCommandWithHandler, defineHandler, defineInspector, definePlugin, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
|
1
|
+
export * from '@clerc/core';
|
|
2
|
+
export * from '@clerc/plugin-completions';
|
|
3
|
+
export * from '@clerc/plugin-help';
|
|
4
|
+
export * from '@clerc/plugin-not-found';
|
|
5
|
+
export * from '@clerc/plugin-strict-flags';
|
package/dist/toolkit.cjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var toolkit = require('@clerc/toolkit');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Object.keys(toolkit).forEach(function (k) {
|
|
10
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () { return toolkit[k]; }
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@clerc/toolkit';
|
package/dist/toolkit.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@clerc/toolkit';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clerc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc is a simple and easy-to-use cli framework.",
|
|
6
6
|
"keywords": [
|
|
@@ -27,6 +27,11 @@
|
|
|
27
27
|
"types": "./dist/index.d.ts",
|
|
28
28
|
"require": "./dist/index.cjs",
|
|
29
29
|
"import": "./dist/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./toolkit": {
|
|
32
|
+
"types": "./dist/toolkit.d.ts",
|
|
33
|
+
"require": "./dist/toolkit.cjs",
|
|
34
|
+
"import": "./dist/toolkit.mjs"
|
|
30
35
|
}
|
|
31
36
|
},
|
|
32
37
|
"main": "dist/index.cjs",
|
|
@@ -39,10 +44,12 @@
|
|
|
39
44
|
"access": "public"
|
|
40
45
|
},
|
|
41
46
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"@clerc/
|
|
47
|
+
"@clerc/core": "0.10.0",
|
|
48
|
+
"@clerc/plugin-help": "0.10.0",
|
|
49
|
+
"@clerc/plugin-completions": "0.10.0",
|
|
50
|
+
"@clerc/plugin-not-found": "0.10.0",
|
|
51
|
+
"@clerc/plugin-strict-flags": "0.10.0",
|
|
52
|
+
"@clerc/toolkit": "0.10.0"
|
|
46
53
|
},
|
|
47
54
|
"scripts": {
|
|
48
55
|
"build": "puild",
|