@jsse/eslint-config 0.5.1 → 0.5.3
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/CHANGELOG.md +13 -0
- package/dist/cli.js +261 -214
- package/dist/index.d.ts +45 -67
- package/dist/index.js +158 -80
- package/dist/{version-CQhcdCbl.js → version-B7sAPTF4.js} +1 -1
- package/package.json +19 -17
package/dist/cli.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { n as DEBUG$1, t as VERSION } from "./version-
|
|
1
|
+
import { n as DEBUG$1, t as VERSION } from "./version-B7sAPTF4.js";
|
|
2
2
|
import process$1 from "node:process";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
-
import { EventEmitter } from "events";
|
|
5
4
|
|
|
6
|
-
//#region node_modules/.pnpm/cac@
|
|
5
|
+
//#region node_modules/.pnpm/cac@7.0.0/node_modules/cac/dist/index.js
|
|
7
6
|
function toArr(any) {
|
|
8
7
|
return any == null ? [] : Array.isArray(any) ? any : [any];
|
|
9
8
|
}
|
|
@@ -11,7 +10,7 @@ function toVal(out, key, val, opts) {
|
|
|
11
10
|
var x, old = out[key], nxt = !!~opts.string.indexOf(key) ? val == null || val === true ? "" : String(val) : typeof val === "boolean" ? val : !!~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
|
|
12
11
|
out[key] = old == null ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
|
|
13
12
|
}
|
|
14
|
-
function
|
|
13
|
+
function lib_default(args, opts) {
|
|
15
14
|
args = args || [];
|
|
16
15
|
opts = opts || {};
|
|
17
16
|
var k, arr, arg, name, val, out = { _: [] };
|
|
@@ -22,13 +21,9 @@ function mri2(args, opts) {
|
|
|
22
21
|
opts.alias = opts.alias || {};
|
|
23
22
|
opts.string = toArr(opts.string);
|
|
24
23
|
opts.boolean = toArr(opts.boolean);
|
|
25
|
-
if (alibi) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
for (i = 0; i < arr.length; i++) {
|
|
29
|
-
(opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
24
|
+
if (alibi) for (k in opts.alias) {
|
|
25
|
+
arr = opts.alias[k] = toArr(opts.alias[k]);
|
|
26
|
+
for (i = 0; i < arr.length; i++) (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
32
27
|
}
|
|
33
28
|
for (i = opts.boolean.length; i-- > 0;) {
|
|
34
29
|
arr = opts.alias[opts.boolean[i]] || [];
|
|
@@ -38,16 +33,12 @@ function mri2(args, opts) {
|
|
|
38
33
|
arr = opts.alias[opts.string[i]] || [];
|
|
39
34
|
for (j = arr.length; j-- > 0;) opts.string.push(arr[j]);
|
|
40
35
|
}
|
|
41
|
-
if (defaults) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
for (i = 0; i < arr.length; i++) {
|
|
48
|
-
opts[name].push(arr[i]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
36
|
+
if (defaults) for (k in opts.default) {
|
|
37
|
+
name = typeof opts.default[k];
|
|
38
|
+
arr = opts.alias[k] = opts.alias[k] || [];
|
|
39
|
+
if (opts[name] !== void 0) {
|
|
40
|
+
opts[name].push(k);
|
|
41
|
+
for (i = 0; i < arr.length; i++) opts[name].push(arr[i]);
|
|
51
42
|
}
|
|
52
43
|
}
|
|
53
44
|
const keys = strict ? Object.keys(opts.alias) : [];
|
|
@@ -57,21 +48,14 @@ function mri2(args, opts) {
|
|
|
57
48
|
out._ = out._.concat(args.slice(++i));
|
|
58
49
|
break;
|
|
59
50
|
}
|
|
60
|
-
for (j = 0; j < arg.length; j++)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (j === 0) {
|
|
64
|
-
out._.push(arg);
|
|
65
|
-
} else if (arg.substring(j, j + 3) === "no-") {
|
|
51
|
+
for (j = 0; j < arg.length; j++) if (arg.charCodeAt(j) !== 45) break;
|
|
52
|
+
if (j === 0) out._.push(arg);
|
|
53
|
+
else if (arg.substring(j, j + 3) === "no-") {
|
|
66
54
|
name = arg.substring(j + 3);
|
|
67
|
-
if (strict && !~keys.indexOf(name))
|
|
68
|
-
return opts.unknown(arg);
|
|
69
|
-
}
|
|
55
|
+
if (strict && !~keys.indexOf(name)) return opts.unknown(arg);
|
|
70
56
|
out[name] = false;
|
|
71
57
|
} else {
|
|
72
|
-
for (idx = j + 1; idx < arg.length; idx++)
|
|
73
|
-
if (arg.charCodeAt(idx) === 61) break;
|
|
74
|
-
}
|
|
58
|
+
for (idx = j + 1; idx < arg.length; idx++) if (arg.charCodeAt(idx) === 61) break;
|
|
75
59
|
name = arg.substring(j, idx);
|
|
76
60
|
val = arg.substring(++idx) || i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i];
|
|
77
61
|
arr = j === 2 ? [name] : name;
|
|
@@ -83,24 +67,18 @@ function mri2(args, opts) {
|
|
|
83
67
|
}
|
|
84
68
|
}
|
|
85
69
|
if (defaults) {
|
|
86
|
-
for (k in opts.default)
|
|
87
|
-
if (out[k] === void 0) {
|
|
88
|
-
out[k] = opts.default[k];
|
|
89
|
-
}
|
|
90
|
-
}
|
|
70
|
+
for (k in opts.default) if (out[k] === void 0) out[k] = opts.default[k];
|
|
91
71
|
}
|
|
92
|
-
if (alibi) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
while (arr.length > 0) {
|
|
96
|
-
out[arr.shift()] = out[k];
|
|
97
|
-
}
|
|
98
|
-
}
|
|
72
|
+
if (alibi) for (k in out) {
|
|
73
|
+
arr = opts.alias[k] || [];
|
|
74
|
+
while (arr.length > 0) out[arr.shift()] = out[k];
|
|
99
75
|
}
|
|
100
76
|
return out;
|
|
101
77
|
}
|
|
102
|
-
|
|
103
|
-
|
|
78
|
+
function removeBrackets(v) {
|
|
79
|
+
return v.replace(/[<[].+/, "").trim();
|
|
80
|
+
}
|
|
81
|
+
function findAllBrackets(v) {
|
|
104
82
|
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
|
|
105
83
|
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
|
|
106
84
|
const res = [];
|
|
@@ -118,99 +96,95 @@ const findAllBrackets = (v) => {
|
|
|
118
96
|
};
|
|
119
97
|
};
|
|
120
98
|
let angledMatch;
|
|
121
|
-
while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v))
|
|
122
|
-
res.push(parse(angledMatch));
|
|
123
|
-
}
|
|
99
|
+
while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) res.push(parse(angledMatch));
|
|
124
100
|
let squareMatch;
|
|
125
|
-
while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v))
|
|
126
|
-
res.push(parse(squareMatch));
|
|
127
|
-
}
|
|
101
|
+
while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) res.push(parse(squareMatch));
|
|
128
102
|
return res;
|
|
129
|
-
}
|
|
130
|
-
|
|
103
|
+
}
|
|
104
|
+
function getMriOptions(options) {
|
|
131
105
|
const result = {
|
|
132
106
|
alias: {},
|
|
133
107
|
boolean: []
|
|
134
108
|
};
|
|
135
109
|
for (const [index, option] of options.entries()) {
|
|
136
|
-
if (option.names.length > 1)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === "boolean";
|
|
143
|
-
});
|
|
144
|
-
if (!hasStringTypeOption) {
|
|
145
|
-
result.boolean.push(option.names[0]);
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
result.boolean.push(option.names[0]);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
110
|
+
if (option.names.length > 1) result.alias[option.names[0]] = option.names.slice(1);
|
|
111
|
+
if (option.isBoolean) if (option.negated) {
|
|
112
|
+
if (!options.some((o, i) => {
|
|
113
|
+
return i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === "boolean";
|
|
114
|
+
})) result.boolean.push(option.names[0]);
|
|
115
|
+
} else result.boolean.push(option.names[0]);
|
|
151
116
|
}
|
|
152
117
|
return result;
|
|
153
|
-
}
|
|
154
|
-
|
|
118
|
+
}
|
|
119
|
+
function findLongest(arr) {
|
|
155
120
|
return arr.sort((a, b) => {
|
|
156
121
|
return a.length > b.length ? -1 : 1;
|
|
157
122
|
})[0];
|
|
158
|
-
}
|
|
159
|
-
|
|
123
|
+
}
|
|
124
|
+
function padRight(str, length) {
|
|
160
125
|
return str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return input.
|
|
126
|
+
}
|
|
127
|
+
function camelcase(input) {
|
|
128
|
+
return input.replaceAll(/([a-z])-([a-z])/g, (_, p1, p2) => {
|
|
164
129
|
return p1 + p2.toUpperCase();
|
|
165
130
|
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
let
|
|
169
|
-
let
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
131
|
+
}
|
|
132
|
+
function setDotProp(obj, keys, val) {
|
|
133
|
+
let current = obj;
|
|
134
|
+
for (let i = 0; i < keys.length; i++) {
|
|
135
|
+
const key = keys[i];
|
|
136
|
+
if (i === keys.length - 1) {
|
|
137
|
+
current[key] = val;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (current[key] == null) {
|
|
141
|
+
const nextKeyIsArrayIndex = +keys[i + 1] > -1;
|
|
142
|
+
current[key] = nextKeyIsArrayIndex ? [] : {};
|
|
143
|
+
}
|
|
144
|
+
current = current[key];
|
|
175
145
|
}
|
|
176
|
-
}
|
|
177
|
-
|
|
146
|
+
}
|
|
147
|
+
function setByType(obj, transforms) {
|
|
178
148
|
for (const key of Object.keys(transforms)) {
|
|
179
149
|
const transform = transforms[key];
|
|
180
150
|
if (transform.shouldTransform) {
|
|
181
|
-
obj[key] =
|
|
182
|
-
if (typeof transform.transformFunction === "function")
|
|
183
|
-
obj[key] = obj[key].map(transform.transformFunction);
|
|
184
|
-
}
|
|
151
|
+
obj[key] = [obj[key]].flat();
|
|
152
|
+
if (typeof transform.transformFunction === "function") obj[key] = obj[key].map(transform.transformFunction);
|
|
185
153
|
}
|
|
186
154
|
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const m = /([
|
|
155
|
+
}
|
|
156
|
+
function getFileName(input) {
|
|
157
|
+
const m = /([^\\/]+)$/.exec(input);
|
|
190
158
|
return m ? m[1] : "";
|
|
191
|
-
}
|
|
192
|
-
|
|
159
|
+
}
|
|
160
|
+
function camelcaseOptionName(name) {
|
|
193
161
|
return name.split(".").map((v, i) => {
|
|
194
162
|
return i === 0 ? camelcase(v) : v;
|
|
195
163
|
}).join(".");
|
|
196
|
-
}
|
|
164
|
+
}
|
|
197
165
|
var CACError = class extends Error {
|
|
198
166
|
constructor(message) {
|
|
199
167
|
super(message);
|
|
200
|
-
this.name =
|
|
201
|
-
if (typeof Error.captureStackTrace
|
|
202
|
-
Error.captureStackTrace(this, this.constructor);
|
|
203
|
-
} else {
|
|
204
|
-
this.stack = new Error(message).stack;
|
|
205
|
-
}
|
|
168
|
+
this.name = "CACError";
|
|
169
|
+
if (typeof Error.captureStackTrace !== "function") this.stack = new Error(message).stack;
|
|
206
170
|
}
|
|
207
171
|
};
|
|
208
172
|
var Option = class {
|
|
173
|
+
rawName;
|
|
174
|
+
description;
|
|
175
|
+
/** Option name */
|
|
176
|
+
name;
|
|
177
|
+
/** Option name and aliases */
|
|
178
|
+
names;
|
|
179
|
+
isBoolean;
|
|
180
|
+
required;
|
|
181
|
+
config;
|
|
182
|
+
negated;
|
|
209
183
|
constructor(rawName, description, config) {
|
|
210
184
|
this.rawName = rawName;
|
|
211
185
|
this.description = description;
|
|
212
186
|
this.config = Object.assign({}, config);
|
|
213
|
-
rawName = rawName.
|
|
187
|
+
rawName = rawName.replaceAll(".*", "");
|
|
214
188
|
this.negated = false;
|
|
215
189
|
this.names = removeBrackets(rawName).split(",").map((v) => {
|
|
216
190
|
let name = v.trim().replace(/^-{1,2}/, "");
|
|
@@ -220,22 +194,39 @@ var Option = class {
|
|
|
220
194
|
}
|
|
221
195
|
return camelcaseOptionName(name);
|
|
222
196
|
}).sort((a, b) => a.length > b.length ? 1 : -1);
|
|
223
|
-
this.name = this.names
|
|
224
|
-
if (this.negated && this.config.default == null)
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
this.required = true;
|
|
229
|
-
} else if (rawName.includes("[")) {
|
|
230
|
-
this.required = false;
|
|
231
|
-
} else {
|
|
232
|
-
this.isBoolean = true;
|
|
233
|
-
}
|
|
197
|
+
this.name = this.names.at(-1);
|
|
198
|
+
if (this.negated && this.config.default == null) this.config.default = true;
|
|
199
|
+
if (rawName.includes("<")) this.required = true;
|
|
200
|
+
else if (rawName.includes("[")) this.required = false;
|
|
201
|
+
else this.isBoolean = true;
|
|
234
202
|
}
|
|
235
203
|
};
|
|
236
|
-
|
|
237
|
-
|
|
204
|
+
let runtimeProcessArgs;
|
|
205
|
+
let runtimeInfo;
|
|
206
|
+
if (typeof process !== "undefined") {
|
|
207
|
+
let runtimeName;
|
|
208
|
+
if (typeof Deno !== "undefined" && typeof Deno.version?.deno === "string") runtimeName = "deno";
|
|
209
|
+
else if (typeof Bun !== "undefined" && typeof Bun.version === "string") runtimeName = "bun";
|
|
210
|
+
else runtimeName = "node";
|
|
211
|
+
runtimeInfo = `${process.platform}-${process.arch} ${runtimeName}-${process.version}`;
|
|
212
|
+
runtimeProcessArgs = process.argv;
|
|
213
|
+
} else if (typeof navigator === "undefined") runtimeInfo = `unknown`;
|
|
214
|
+
else runtimeInfo = `${navigator.platform} ${navigator.userAgent}`;
|
|
238
215
|
var Command = class {
|
|
216
|
+
rawName;
|
|
217
|
+
description;
|
|
218
|
+
config;
|
|
219
|
+
cli;
|
|
220
|
+
options;
|
|
221
|
+
aliasNames;
|
|
222
|
+
name;
|
|
223
|
+
args;
|
|
224
|
+
commandAction;
|
|
225
|
+
usageText;
|
|
226
|
+
versionNumber;
|
|
227
|
+
examples;
|
|
228
|
+
helpCallback;
|
|
229
|
+
globalCommand;
|
|
239
230
|
constructor(rawName, description, config = {}, cli) {
|
|
240
231
|
this.rawName = rawName;
|
|
241
232
|
this.description = description;
|
|
@@ -268,6 +259,12 @@ var Command = class {
|
|
|
268
259
|
this.examples.push(example);
|
|
269
260
|
return this;
|
|
270
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Add a option for this command
|
|
264
|
+
* @param rawName Raw option name(s)
|
|
265
|
+
* @param description Option description
|
|
266
|
+
* @param config Option config
|
|
267
|
+
*/
|
|
271
268
|
option(rawName, description, config) {
|
|
272
269
|
const option = new Option(rawName, description, config);
|
|
273
270
|
this.options.push(option);
|
|
@@ -281,6 +278,10 @@ var Command = class {
|
|
|
281
278
|
this.commandAction = callback;
|
|
282
279
|
return this;
|
|
283
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Check if a command name is matched by this command
|
|
283
|
+
* @param name Command name
|
|
284
|
+
*/
|
|
284
285
|
isMatched(name) {
|
|
285
286
|
return this.name === name || this.aliasNames.includes(name);
|
|
286
287
|
}
|
|
@@ -290,6 +291,10 @@ var Command = class {
|
|
|
290
291
|
get isGlobalCommand() {
|
|
291
292
|
return this instanceof GlobalCommand;
|
|
292
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Check if an option is registered in this command
|
|
296
|
+
* @param name Option name
|
|
297
|
+
*/
|
|
293
298
|
hasOption(name) {
|
|
294
299
|
name = name.split(".")[0];
|
|
295
300
|
return this.options.find((option) => {
|
|
@@ -304,24 +309,20 @@ var Command = class {
|
|
|
304
309
|
title: "Usage",
|
|
305
310
|
body: ` $ ${name} ${this.usageText || this.rawName}`
|
|
306
311
|
});
|
|
307
|
-
|
|
308
|
-
if (showCommands) {
|
|
312
|
+
if ((this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0) {
|
|
309
313
|
const longestCommandName = findLongest(commands.map((command) => command.rawName));
|
|
310
314
|
sections.push({
|
|
311
315
|
title: "Commands",
|
|
312
316
|
body: commands.map((command) => {
|
|
313
317
|
return ` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`;
|
|
314
318
|
}).join("\n")
|
|
315
|
-
}
|
|
316
|
-
sections.push({
|
|
319
|
+
}, {
|
|
317
320
|
title: `For more info, run any command with the \`--help\` flag`,
|
|
318
321
|
body: commands.map((command) => ` $ ${name}${command.name === "" ? "" : ` ${command.name}`} --help`).join("\n")
|
|
319
322
|
});
|
|
320
323
|
}
|
|
321
324
|
let options = this.isGlobalCommand ? globalOptions : [...this.options, ...globalOptions || []];
|
|
322
|
-
if (!this.isGlobalCommand && !this.isDefaultCommand)
|
|
323
|
-
options = options.filter((option) => option.name !== "version");
|
|
324
|
-
}
|
|
325
|
+
if (!this.isGlobalCommand && !this.isDefaultCommand) options = options.filter((option) => option.name !== "version");
|
|
325
326
|
if (options.length > 0) {
|
|
326
327
|
const longestOptionName = findLongest(options.map((option) => option.rawName));
|
|
327
328
|
sections.push({
|
|
@@ -331,48 +332,41 @@ var Command = class {
|
|
|
331
332
|
}).join("\n")
|
|
332
333
|
});
|
|
333
334
|
}
|
|
334
|
-
if (this.examples.length > 0) {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (helpCallback) {
|
|
346
|
-
sections = helpCallback(sections) || sections;
|
|
347
|
-
}
|
|
348
|
-
console.log(sections.map((section) => {
|
|
349
|
-
return section.title ? `${section.title}:
|
|
350
|
-
${section.body}` : section.body;
|
|
335
|
+
if (this.examples.length > 0) sections.push({
|
|
336
|
+
title: "Examples",
|
|
337
|
+
body: this.examples.map((example) => {
|
|
338
|
+
if (typeof example === "function") return example(name);
|
|
339
|
+
return example;
|
|
340
|
+
}).join("\n")
|
|
341
|
+
});
|
|
342
|
+
if (helpCallback) sections = helpCallback(sections) || sections;
|
|
343
|
+
console.info(sections.map((section) => {
|
|
344
|
+
return section.title ? `${section.title}:\n${section.body}` : section.body;
|
|
351
345
|
}).join("\n\n"));
|
|
352
346
|
}
|
|
353
347
|
outputVersion() {
|
|
354
348
|
const { name } = this.cli;
|
|
355
349
|
const { versionNumber } = this.cli.globalCommand;
|
|
356
|
-
if (versionNumber) {
|
|
357
|
-
console.log(`${name}/${versionNumber} ${platformInfo}`);
|
|
358
|
-
}
|
|
350
|
+
if (versionNumber) console.info(`${name}/${versionNumber} ${runtimeInfo}`);
|
|
359
351
|
}
|
|
360
352
|
checkRequiredArgs() {
|
|
361
353
|
const minimalArgsCount = this.args.filter((arg) => arg.required).length;
|
|
362
|
-
if (this.cli.args.length < minimalArgsCount) {
|
|
363
|
-
throw new CACError(`missing required args for command \`${this.rawName}\``);
|
|
364
|
-
}
|
|
354
|
+
if (this.cli.args.length < minimalArgsCount) throw new CACError(`missing required args for command \`${this.rawName}\``);
|
|
365
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* Check if the parsed options contain any unknown options
|
|
358
|
+
*
|
|
359
|
+
* Exit and output error when true
|
|
360
|
+
*/
|
|
366
361
|
checkUnknownOptions() {
|
|
367
362
|
const { options, globalCommand } = this.cli;
|
|
368
363
|
if (!this.config.allowUnknownOptions) {
|
|
369
|
-
for (const name of Object.keys(options)) {
|
|
370
|
-
if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) {
|
|
371
|
-
throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
364
|
+
for (const name of Object.keys(options)) if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
|
|
374
365
|
}
|
|
375
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* Check if the required string-type options exist
|
|
369
|
+
*/
|
|
376
370
|
checkOptionValue() {
|
|
377
371
|
const { options: parsedOptions, globalCommand } = this.cli;
|
|
378
372
|
const options = [...globalCommand.options, ...this.options];
|
|
@@ -380,20 +374,47 @@ ${section.body}` : section.body;
|
|
|
380
374
|
const value = parsedOptions[option.name.split(".")[0]];
|
|
381
375
|
if (option.required) {
|
|
382
376
|
const hasNegated = options.some((o) => o.negated && o.names.includes(option.name));
|
|
383
|
-
if (value === true || value === false && !hasNegated) {
|
|
384
|
-
throw new CACError(`option \`${option.rawName}\` value is missing`);
|
|
385
|
-
}
|
|
377
|
+
if (value === true || value === false && !hasNegated) throw new CACError(`option \`${option.rawName}\` value is missing`);
|
|
386
378
|
}
|
|
387
379
|
}
|
|
388
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Check if the number of args is more than expected
|
|
383
|
+
*/
|
|
384
|
+
checkUnusedArgs() {
|
|
385
|
+
const maximumArgsCount = this.args.some((arg) => arg.variadic) ? Infinity : this.args.length;
|
|
386
|
+
if (maximumArgsCount < this.cli.args.length) throw new CACError(`Unused args: ${this.cli.args.slice(maximumArgsCount).map((arg) => `\`${arg}\``).join(", ")}`);
|
|
387
|
+
}
|
|
389
388
|
};
|
|
390
389
|
var GlobalCommand = class extends Command {
|
|
391
390
|
constructor(cli) {
|
|
392
391
|
super("@@global@@", "", {}, cli);
|
|
393
392
|
}
|
|
394
393
|
};
|
|
395
|
-
var
|
|
396
|
-
|
|
394
|
+
var CAC = class extends EventTarget {
|
|
395
|
+
/** The program name to display in help and version message */
|
|
396
|
+
name;
|
|
397
|
+
commands;
|
|
398
|
+
globalCommand;
|
|
399
|
+
matchedCommand;
|
|
400
|
+
matchedCommandName;
|
|
401
|
+
/**
|
|
402
|
+
* Raw CLI arguments
|
|
403
|
+
*/
|
|
404
|
+
rawArgs;
|
|
405
|
+
/**
|
|
406
|
+
* Parsed CLI arguments
|
|
407
|
+
*/
|
|
408
|
+
args;
|
|
409
|
+
/**
|
|
410
|
+
* Parsed CLI options, camelCased
|
|
411
|
+
*/
|
|
412
|
+
options;
|
|
413
|
+
showHelpOnExit;
|
|
414
|
+
showVersionOnExit;
|
|
415
|
+
/**
|
|
416
|
+
* @param name The program name to display in help and version message
|
|
417
|
+
*/
|
|
397
418
|
constructor(name = "") {
|
|
398
419
|
super();
|
|
399
420
|
this.name = name;
|
|
@@ -404,84 +425,119 @@ var CAC = class extends EventEmitter {
|
|
|
404
425
|
this.globalCommand = new GlobalCommand(this);
|
|
405
426
|
this.globalCommand.usage("<command> [options]");
|
|
406
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* Add a global usage text.
|
|
430
|
+
*
|
|
431
|
+
* This is not used by sub-commands.
|
|
432
|
+
*/
|
|
407
433
|
usage(text) {
|
|
408
434
|
this.globalCommand.usage(text);
|
|
409
435
|
return this;
|
|
410
436
|
}
|
|
437
|
+
/**
|
|
438
|
+
* Add a sub-command
|
|
439
|
+
*/
|
|
411
440
|
command(rawName, description, config) {
|
|
412
441
|
const command = new Command(rawName, description || "", config, this);
|
|
413
442
|
command.globalCommand = this.globalCommand;
|
|
414
443
|
this.commands.push(command);
|
|
415
444
|
return command;
|
|
416
445
|
}
|
|
446
|
+
/**
|
|
447
|
+
* Add a global CLI option.
|
|
448
|
+
*
|
|
449
|
+
* Which is also applied to sub-commands.
|
|
450
|
+
*/
|
|
417
451
|
option(rawName, description, config) {
|
|
418
452
|
this.globalCommand.option(rawName, description, config);
|
|
419
453
|
return this;
|
|
420
454
|
}
|
|
455
|
+
/**
|
|
456
|
+
* Show help message when `-h, --help` flags appear.
|
|
457
|
+
*
|
|
458
|
+
*/
|
|
421
459
|
help(callback) {
|
|
422
460
|
this.globalCommand.option("-h, --help", "Display this message");
|
|
423
461
|
this.globalCommand.helpCallback = callback;
|
|
424
462
|
this.showHelpOnExit = true;
|
|
425
463
|
return this;
|
|
426
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Show version number when `-v, --version` flags appear.
|
|
467
|
+
*
|
|
468
|
+
*/
|
|
427
469
|
version(version, customFlags = "-v, --version") {
|
|
428
470
|
this.globalCommand.version(version, customFlags);
|
|
429
471
|
this.showVersionOnExit = true;
|
|
430
472
|
return this;
|
|
431
473
|
}
|
|
474
|
+
/**
|
|
475
|
+
* Add a global example.
|
|
476
|
+
*
|
|
477
|
+
* This example added here will not be used by sub-commands.
|
|
478
|
+
*/
|
|
432
479
|
example(example) {
|
|
433
480
|
this.globalCommand.example(example);
|
|
434
481
|
return this;
|
|
435
482
|
}
|
|
483
|
+
/**
|
|
484
|
+
* Output the corresponding help message
|
|
485
|
+
* When a sub-command is matched, output the help message for the command
|
|
486
|
+
* Otherwise output the global one.
|
|
487
|
+
*
|
|
488
|
+
*/
|
|
436
489
|
outputHelp() {
|
|
437
|
-
if (this.matchedCommand)
|
|
438
|
-
|
|
439
|
-
} else {
|
|
440
|
-
this.globalCommand.outputHelp();
|
|
441
|
-
}
|
|
490
|
+
if (this.matchedCommand) this.matchedCommand.outputHelp();
|
|
491
|
+
else this.globalCommand.outputHelp();
|
|
442
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Output the version number.
|
|
495
|
+
*
|
|
496
|
+
*/
|
|
443
497
|
outputVersion() {
|
|
444
498
|
this.globalCommand.outputVersion();
|
|
445
499
|
}
|
|
446
500
|
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) {
|
|
447
501
|
this.args = args;
|
|
448
502
|
this.options = options;
|
|
449
|
-
if (matchedCommand)
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
if (matchedCommandName) {
|
|
453
|
-
this.matchedCommandName = matchedCommandName;
|
|
454
|
-
}
|
|
503
|
+
if (matchedCommand) this.matchedCommand = matchedCommand;
|
|
504
|
+
if (matchedCommandName) this.matchedCommandName = matchedCommandName;
|
|
455
505
|
return this;
|
|
456
506
|
}
|
|
457
507
|
unsetMatchedCommand() {
|
|
458
508
|
this.matchedCommand = void 0;
|
|
459
509
|
this.matchedCommandName = void 0;
|
|
460
510
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
511
|
+
/**
|
|
512
|
+
* Parse argv
|
|
513
|
+
*/
|
|
514
|
+
parse(argv, { run = true } = {}) {
|
|
515
|
+
if (!argv) {
|
|
516
|
+
if (!runtimeProcessArgs) throw new Error("No argv provided and runtime process argv is not available.");
|
|
517
|
+
argv = runtimeProcessArgs;
|
|
465
518
|
}
|
|
519
|
+
this.rawArgs = argv;
|
|
520
|
+
if (!this.name) this.name = argv[1] ? getFileName(argv[1]) : "cli";
|
|
466
521
|
let shouldParse = true;
|
|
467
522
|
for (const command of this.commands) {
|
|
468
523
|
const parsed = this.mri(argv.slice(2), command);
|
|
469
524
|
const commandName = parsed.args[0];
|
|
470
525
|
if (command.isMatched(commandName)) {
|
|
471
526
|
shouldParse = false;
|
|
472
|
-
const parsedInfo =
|
|
527
|
+
const parsedInfo = {
|
|
528
|
+
...parsed,
|
|
529
|
+
args: parsed.args.slice(1)
|
|
530
|
+
};
|
|
473
531
|
this.setParsedInfo(parsedInfo, command, commandName);
|
|
474
|
-
this.
|
|
532
|
+
this.dispatchEvent(new CustomEvent(`command:${commandName}`, { detail: command }));
|
|
475
533
|
}
|
|
476
534
|
}
|
|
477
535
|
if (shouldParse) {
|
|
478
|
-
for (const command of this.commands) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
this.emit(`command:!`, command);
|
|
484
|
-
}
|
|
536
|
+
for (const command of this.commands) if (command.isDefaultCommand) {
|
|
537
|
+
shouldParse = false;
|
|
538
|
+
const parsed = this.mri(argv.slice(2), command);
|
|
539
|
+
this.setParsedInfo(parsed, command);
|
|
540
|
+
this.dispatchEvent(new CustomEvent("command:!", { detail: command }));
|
|
485
541
|
}
|
|
486
542
|
}
|
|
487
543
|
if (shouldParse) {
|
|
@@ -502,12 +558,8 @@ var CAC = class extends EventEmitter {
|
|
|
502
558
|
args: this.args,
|
|
503
559
|
options: this.options
|
|
504
560
|
};
|
|
505
|
-
if (run)
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
if (!this.matchedCommand && this.args[0]) {
|
|
509
|
-
this.emit("command:*");
|
|
510
|
-
}
|
|
561
|
+
if (run) this.runMatchedCommand();
|
|
562
|
+
if (!this.matchedCommand && this.args[0]) this.dispatchEvent(new CustomEvent("command:*", { detail: this.args[0] }));
|
|
511
563
|
return parsedArgv;
|
|
512
564
|
}
|
|
513
565
|
mri(argv, command) {
|
|
@@ -515,38 +567,32 @@ var CAC = class extends EventEmitter {
|
|
|
515
567
|
const mriOptions = getMriOptions(cliOptions);
|
|
516
568
|
let argsAfterDoubleDashes = [];
|
|
517
569
|
const doubleDashesIndex = argv.indexOf("--");
|
|
518
|
-
if (doubleDashesIndex
|
|
570
|
+
if (doubleDashesIndex !== -1) {
|
|
519
571
|
argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
|
|
520
572
|
argv = argv.slice(0, doubleDashesIndex);
|
|
521
573
|
}
|
|
522
|
-
let parsed =
|
|
574
|
+
let parsed = lib_default(argv, mriOptions);
|
|
523
575
|
parsed = Object.keys(parsed).reduce((res, name) => {
|
|
524
|
-
return
|
|
576
|
+
return {
|
|
577
|
+
...res,
|
|
578
|
+
[camelcaseOptionName(name)]: parsed[name]
|
|
579
|
+
};
|
|
525
580
|
}, { _: [] });
|
|
526
581
|
const args = parsed._;
|
|
527
582
|
const options = { "--": argsAfterDoubleDashes };
|
|
528
583
|
const ignoreDefault = command && command.config.ignoreOptionDefaultValue ? command.config.ignoreOptionDefaultValue : this.globalCommand.config.ignoreOptionDefaultValue;
|
|
529
|
-
|
|
584
|
+
const transforms = Object.create(null);
|
|
530
585
|
for (const cliOption of cliOptions) {
|
|
531
|
-
if (!ignoreDefault && cliOption.config.default !== void 0)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if (Array.isArray(cliOption.config.type)) {
|
|
537
|
-
if (transforms[cliOption.name] === void 0) {
|
|
538
|
-
transforms[cliOption.name] = Object.create(null);
|
|
539
|
-
transforms[cliOption.name]["shouldTransform"] = true;
|
|
540
|
-
transforms[cliOption.name]["transformFunction"] = cliOption.config.type[0];
|
|
541
|
-
}
|
|
586
|
+
if (!ignoreDefault && cliOption.config.default !== void 0) for (const name of cliOption.names) options[name] = cliOption.config.default;
|
|
587
|
+
if (Array.isArray(cliOption.config.type) && transforms[cliOption.name] === void 0) {
|
|
588
|
+
transforms[cliOption.name] = Object.create(null);
|
|
589
|
+
transforms[cliOption.name].shouldTransform = true;
|
|
590
|
+
transforms[cliOption.name].transformFunction = cliOption.config.type[0];
|
|
542
591
|
}
|
|
543
592
|
}
|
|
544
|
-
for (const key of Object.keys(parsed)) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
setDotProp(options, keys, parsed[key]);
|
|
548
|
-
setByType(options, transforms);
|
|
549
|
-
}
|
|
593
|
+
for (const key of Object.keys(parsed)) if (key !== "_") {
|
|
594
|
+
setDotProp(options, key.split("."), parsed[key]);
|
|
595
|
+
setByType(options, transforms);
|
|
550
596
|
}
|
|
551
597
|
return {
|
|
552
598
|
args,
|
|
@@ -559,18 +605,19 @@ var CAC = class extends EventEmitter {
|
|
|
559
605
|
command.checkUnknownOptions();
|
|
560
606
|
command.checkOptionValue();
|
|
561
607
|
command.checkRequiredArgs();
|
|
608
|
+
command.checkUnusedArgs();
|
|
562
609
|
const actionArgs = [];
|
|
563
610
|
command.args.forEach((arg, index) => {
|
|
564
|
-
if (arg.variadic)
|
|
565
|
-
|
|
566
|
-
} else {
|
|
567
|
-
actionArgs.push(args[index]);
|
|
568
|
-
}
|
|
611
|
+
if (arg.variadic) actionArgs.push(args.slice(index));
|
|
612
|
+
else actionArgs.push(args[index]);
|
|
569
613
|
});
|
|
570
614
|
actionArgs.push(options);
|
|
571
615
|
return command.commandAction.apply(this, actionArgs);
|
|
572
616
|
}
|
|
573
617
|
};
|
|
618
|
+
/**
|
|
619
|
+
* @param name The program name to display in help and version message
|
|
620
|
+
*/
|
|
574
621
|
const cac = (name = "") => new CAC(name);
|
|
575
622
|
|
|
576
623
|
//#endregion
|