vite-plus 0.3.0 → 0.3.2

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