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