fast-npm-meta 1.3.0 → 1.4.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/cli.mjs CHANGED
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import process$1 from "node:process";
3
- import { EventEmitter } from "events";
4
3
 
5
- //#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
6
5
  function toArr(any) {
7
6
  return any == null ? [] : Array.isArray(any) ? any : [any];
8
7
  }
@@ -10,7 +9,7 @@ function toVal(out, key, val, opts) {
10
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;
11
10
  out[key] = old == null ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
12
11
  }
13
- function mri2(args, opts) {
12
+ function lib_default(args, opts) {
14
13
  args = args || [];
15
14
  opts = opts || {};
16
15
  var k, arr, arg, name, val, out = { _: [] };
@@ -75,8 +74,10 @@ function mri2(args, opts) {
75
74
  }
76
75
  return out;
77
76
  }
78
- const removeBrackets = (v) => v.replace(/[<[].+/, "").trim();
79
- const findAllBrackets = (v) => {
77
+ function removeBrackets(v) {
78
+ return v.replace(/[<[].+/, "").trim();
79
+ }
80
+ function findAllBrackets(v) {
80
81
  const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
81
82
  const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
82
83
  const res = [];
@@ -98,8 +99,8 @@ const findAllBrackets = (v) => {
98
99
  let squareMatch;
99
100
  while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) res.push(parse(squareMatch));
100
101
  return res;
101
- };
102
- const getMriOptions = (options) => {
102
+ }
103
+ function getMriOptions(options) {
103
104
  const result = {
104
105
  alias: {},
105
106
  boolean: []
@@ -113,62 +114,76 @@ const getMriOptions = (options) => {
113
114
  } else result.boolean.push(option.names[0]);
114
115
  }
115
116
  return result;
116
- };
117
- const findLongest = (arr) => {
117
+ }
118
+ function findLongest(arr) {
118
119
  return arr.sort((a, b) => {
119
120
  return a.length > b.length ? -1 : 1;
120
121
  })[0];
121
- };
122
- const padRight = (str, length) => {
122
+ }
123
+ function padRight(str, length) {
123
124
  return str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
124
- };
125
- const camelcase = (input) => {
126
- return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
125
+ }
126
+ function camelcase(input) {
127
+ return input.replaceAll(/([a-z])-([a-z])/g, (_, p1, p2) => {
127
128
  return p1 + p2.toUpperCase();
128
129
  });
129
- };
130
- const setDotProp = (obj, keys, val) => {
131
- let i = 0;
132
- let length = keys.length;
133
- let t = obj;
134
- let x;
135
- for (; i < length; ++i) {
136
- x = t[keys[i]];
137
- t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf(".") || !(+keys[i + 1] > -1) ? {} : [];
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];
138
144
  }
139
- };
140
- const setByType = (obj, transforms) => {
145
+ }
146
+ function setByType(obj, transforms) {
141
147
  for (const key of Object.keys(transforms)) {
142
148
  const transform = transforms[key];
143
149
  if (transform.shouldTransform) {
144
- obj[key] = Array.prototype.concat.call([], obj[key]);
150
+ obj[key] = [obj[key]].flat();
145
151
  if (typeof transform.transformFunction === "function") obj[key] = obj[key].map(transform.transformFunction);
146
152
  }
147
153
  }
148
- };
149
- const getFileName = (input) => {
150
- const m = /([^\\\/]+)$/.exec(input);
154
+ }
155
+ function getFileName(input) {
156
+ const m = /([^\\/]+)$/.exec(input);
151
157
  return m ? m[1] : "";
152
- };
153
- const camelcaseOptionName = (name) => {
158
+ }
159
+ function camelcaseOptionName(name) {
154
160
  return name.split(".").map((v, i) => {
155
161
  return i === 0 ? camelcase(v) : v;
156
162
  }).join(".");
157
- };
163
+ }
158
164
  var CACError = class extends Error {
159
165
  constructor(message) {
160
166
  super(message);
161
- this.name = this.constructor.name;
162
- if (typeof Error.captureStackTrace === "function") Error.captureStackTrace(this, this.constructor);
163
- else this.stack = new Error(message).stack;
167
+ this.name = "CACError";
168
+ if (typeof Error.captureStackTrace !== "function") this.stack = new Error(message).stack;
164
169
  }
165
170
  };
166
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;
167
182
  constructor(rawName, description, config) {
168
183
  this.rawName = rawName;
169
184
  this.description = description;
170
185
  this.config = Object.assign({}, config);
171
- rawName = rawName.replace(/\.\*/g, "");
186
+ rawName = rawName.replaceAll(".*", "");
172
187
  this.negated = false;
173
188
  this.names = removeBrackets(rawName).split(",").map((v) => {
174
189
  let name = v.trim().replace(/^-{1,2}/, "");
@@ -178,16 +193,39 @@ var Option = class {
178
193
  }
179
194
  return camelcaseOptionName(name);
180
195
  }).sort((a, b) => a.length > b.length ? 1 : -1);
181
- this.name = this.names[this.names.length - 1];
196
+ this.name = this.names.at(-1);
182
197
  if (this.negated && this.config.default == null) this.config.default = true;
183
198
  if (rawName.includes("<")) this.required = true;
184
199
  else if (rawName.includes("[")) this.required = false;
185
200
  else this.isBoolean = true;
186
201
  }
187
202
  };
188
- const processArgs = process.argv;
189
- const platformInfo = `${process.platform}-${process.arch} node-${process.version}`;
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}`;
190
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;
191
229
  constructor(rawName, description, config = {}, cli) {
192
230
  this.rawName = rawName;
193
231
  this.description = description;
@@ -220,6 +258,12 @@ var Command = class {
220
258
  this.examples.push(example);
221
259
  return this;
222
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
+ */
223
267
  option(rawName, description, config) {
224
268
  const option = new Option(rawName, description, config);
225
269
  this.options.push(option);
@@ -233,6 +277,10 @@ var Command = class {
233
277
  this.commandAction = callback;
234
278
  return this;
235
279
  }
280
+ /**
281
+ * Check if a command name is matched by this command
282
+ * @param name Command name
283
+ */
236
284
  isMatched(name) {
237
285
  return this.name === name || this.aliasNames.includes(name);
238
286
  }
@@ -242,6 +290,10 @@ var Command = class {
242
290
  get isGlobalCommand() {
243
291
  return this instanceof GlobalCommand;
244
292
  }
293
+ /**
294
+ * Check if an option is registered in this command
295
+ * @param name Option name
296
+ */
245
297
  hasOption(name) {
246
298
  name = name.split(".")[0];
247
299
  return this.options.find((option) => {
@@ -263,8 +315,7 @@ var Command = class {
263
315
  body: commands.map((command) => {
264
316
  return ` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`;
265
317
  }).join("\n")
266
- });
267
- sections.push({
318
+ }, {
268
319
  title: `For more info, run any command with the \`--help\` flag`,
269
320
  body: commands.map((command) => ` $ ${name}${command.name === "" ? "" : ` ${command.name}`} --help`).join("\n")
270
321
  });
@@ -288,26 +339,33 @@ var Command = class {
288
339
  }).join("\n")
289
340
  });
290
341
  if (helpCallback) sections = helpCallback(sections) || sections;
291
- console.log(sections.map((section) => {
292
- return section.title ? `${section.title}:
293
- ${section.body}` : section.body;
342
+ console.info(sections.map((section) => {
343
+ return section.title ? `${section.title}:\n${section.body}` : section.body;
294
344
  }).join("\n\n"));
295
345
  }
296
346
  outputVersion() {
297
347
  const { name } = this.cli;
298
348
  const { versionNumber } = this.cli.globalCommand;
299
- if (versionNumber) console.log(`${name}/${versionNumber} ${platformInfo}`);
349
+ if (versionNumber) console.info(`${name}/${versionNumber} ${runtimeInfo}`);
300
350
  }
301
351
  checkRequiredArgs() {
302
352
  const minimalArgsCount = this.args.filter((arg) => arg.required).length;
303
353
  if (this.cli.args.length < minimalArgsCount) throw new CACError(`missing required args for command \`${this.rawName}\``);
304
354
  }
355
+ /**
356
+ * Check if the parsed options contain any unknown options
357
+ *
358
+ * Exit and output error when true
359
+ */
305
360
  checkUnknownOptions() {
306
361
  const { options, globalCommand } = this.cli;
307
362
  if (!this.config.allowUnknownOptions) {
308
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}`}\``);
309
364
  }
310
365
  }
366
+ /**
367
+ * Check if the required string-type options exist
368
+ */
311
369
  checkOptionValue() {
312
370
  const { options: parsedOptions, globalCommand } = this.cli;
313
371
  const options = [...globalCommand.options, ...this.options];
@@ -319,14 +377,43 @@ ${section.body}` : section.body;
319
377
  }
320
378
  }
321
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
+ }
322
387
  };
323
388
  var GlobalCommand = class extends Command {
324
389
  constructor(cli) {
325
390
  super("@@global@@", "", {}, cli);
326
391
  }
327
392
  };
328
- var __assign = Object.assign;
329
- var CAC = class extends EventEmitter {
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
+ */
330
417
  constructor(name = "") {
331
418
  super();
332
419
  this.name = name;
@@ -337,39 +424,75 @@ var CAC = class extends EventEmitter {
337
424
  this.globalCommand = new GlobalCommand(this);
338
425
  this.globalCommand.usage("<command> [options]");
339
426
  }
427
+ /**
428
+ * Add a global usage text.
429
+ *
430
+ * This is not used by sub-commands.
431
+ */
340
432
  usage(text) {
341
433
  this.globalCommand.usage(text);
342
434
  return this;
343
435
  }
436
+ /**
437
+ * Add a sub-command
438
+ */
344
439
  command(rawName, description, config) {
345
440
  const command = new Command(rawName, description || "", config, this);
346
441
  command.globalCommand = this.globalCommand;
347
442
  this.commands.push(command);
348
443
  return command;
349
444
  }
445
+ /**
446
+ * Add a global CLI option.
447
+ *
448
+ * Which is also applied to sub-commands.
449
+ */
350
450
  option(rawName, description, config) {
351
451
  this.globalCommand.option(rawName, description, config);
352
452
  return this;
353
453
  }
454
+ /**
455
+ * Show help message when `-h, --help` flags appear.
456
+ *
457
+ */
354
458
  help(callback) {
355
459
  this.globalCommand.option("-h, --help", "Display this message");
356
460
  this.globalCommand.helpCallback = callback;
357
461
  this.showHelpOnExit = true;
358
462
  return this;
359
463
  }
464
+ /**
465
+ * Show version number when `-v, --version` flags appear.
466
+ *
467
+ */
360
468
  version(version, customFlags = "-v, --version") {
361
469
  this.globalCommand.version(version, customFlags);
362
470
  this.showVersionOnExit = true;
363
471
  return this;
364
472
  }
473
+ /**
474
+ * Add a global example.
475
+ *
476
+ * This example added here will not be used by sub-commands.
477
+ */
365
478
  example(example) {
366
479
  this.globalCommand.example(example);
367
480
  return this;
368
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
+ */
369
488
  outputHelp() {
370
489
  if (this.matchedCommand) this.matchedCommand.outputHelp();
371
490
  else this.globalCommand.outputHelp();
372
491
  }
492
+ /**
493
+ * Output the version number.
494
+ *
495
+ */
373
496
  outputVersion() {
374
497
  this.globalCommand.outputVersion();
375
498
  }
@@ -384,7 +507,14 @@ var CAC = class extends EventEmitter {
384
507
  this.matchedCommand = void 0;
385
508
  this.matchedCommandName = void 0;
386
509
  }
387
- parse(argv = processArgs, { run = true } = {}) {
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;
517
+ }
388
518
  this.rawArgs = argv;
389
519
  if (!this.name) this.name = argv[1] ? getFileName(argv[1]) : "cli";
390
520
  let shouldParse = true;
@@ -393,17 +523,20 @@ var CAC = class extends EventEmitter {
393
523
  const commandName = parsed.args[0];
394
524
  if (command.isMatched(commandName)) {
395
525
  shouldParse = false;
396
- const parsedInfo = __assign(__assign({}, parsed), { args: parsed.args.slice(1) });
526
+ const parsedInfo = {
527
+ ...parsed,
528
+ args: parsed.args.slice(1)
529
+ };
397
530
  this.setParsedInfo(parsedInfo, command, commandName);
398
- this.emit(`command:${commandName}`, command);
531
+ this.dispatchEvent(new CustomEvent(`command:${commandName}`, { detail: command }));
399
532
  }
400
533
  }
401
534
  if (shouldParse) {
402
- for (const command of this.commands) if (command.name === "") {
535
+ for (const command of this.commands) if (command.isDefaultCommand) {
403
536
  shouldParse = false;
404
537
  const parsed = this.mri(argv.slice(2), command);
405
538
  this.setParsedInfo(parsed, command);
406
- this.emit(`command:!`, command);
539
+ this.dispatchEvent(new CustomEvent("command:!", { detail: command }));
407
540
  }
408
541
  }
409
542
  if (shouldParse) {
@@ -425,7 +558,7 @@ var CAC = class extends EventEmitter {
425
558
  options: this.options
426
559
  };
427
560
  if (run) this.runMatchedCommand();
428
- if (!this.matchedCommand && this.args[0]) this.emit("command:*");
561
+ if (!this.matchedCommand && this.args[0]) this.dispatchEvent(new CustomEvent("command:*", { detail: this.args[0] }));
429
562
  return parsedArgv;
430
563
  }
431
564
  mri(argv, command) {
@@ -433,26 +566,27 @@ var CAC = class extends EventEmitter {
433
566
  const mriOptions = getMriOptions(cliOptions);
434
567
  let argsAfterDoubleDashes = [];
435
568
  const doubleDashesIndex = argv.indexOf("--");
436
- if (doubleDashesIndex > -1) {
569
+ if (doubleDashesIndex !== -1) {
437
570
  argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
438
571
  argv = argv.slice(0, doubleDashesIndex);
439
572
  }
440
- let parsed = mri2(argv, mriOptions);
573
+ let parsed = lib_default(argv, mriOptions);
441
574
  parsed = Object.keys(parsed).reduce((res, name) => {
442
- return __assign(__assign({}, res), { [camelcaseOptionName(name)]: parsed[name] });
575
+ return {
576
+ ...res,
577
+ [camelcaseOptionName(name)]: parsed[name]
578
+ };
443
579
  }, { _: [] });
444
580
  const args = parsed._;
445
581
  const options = { "--": argsAfterDoubleDashes };
446
582
  const ignoreDefault = command && command.config.ignoreOptionDefaultValue ? command.config.ignoreOptionDefaultValue : this.globalCommand.config.ignoreOptionDefaultValue;
447
- let transforms = Object.create(null);
583
+ const transforms = Object.create(null);
448
584
  for (const cliOption of cliOptions) {
449
585
  if (!ignoreDefault && cliOption.config.default !== void 0) for (const name of cliOption.names) options[name] = cliOption.config.default;
450
- if (Array.isArray(cliOption.config.type)) {
451
- if (transforms[cliOption.name] === void 0) {
452
- transforms[cliOption.name] = Object.create(null);
453
- transforms[cliOption.name]["shouldTransform"] = true;
454
- transforms[cliOption.name]["transformFunction"] = cliOption.config.type[0];
455
- }
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];
456
590
  }
457
591
  }
458
592
  for (const key of Object.keys(parsed)) if (key !== "_") {
@@ -470,6 +604,7 @@ var CAC = class extends EventEmitter {
470
604
  command.checkUnknownOptions();
471
605
  command.checkOptionValue();
472
606
  command.checkRequiredArgs();
607
+ command.checkUnusedArgs();
473
608
  const actionArgs = [];
474
609
  command.args.forEach((arg, index) => {
475
610
  if (arg.variadic) actionArgs.push(args.slice(index));
@@ -479,11 +614,14 @@ var CAC = class extends EventEmitter {
479
614
  return command.commandAction.apply(this, actionArgs);
480
615
  }
481
616
  };
617
+ /**
618
+ * @param name The program name to display in help and version message
619
+ */
482
620
  const cac = (name = "") => new CAC(name);
483
621
 
484
622
  //#endregion
485
623
  //#region package.json
486
- var version = "1.3.0";
624
+ var version = "1.4.0";
487
625
 
488
626
  //#endregion
489
627
  //#region ../node_modules/.pnpm/is-network-error@1.3.0/node_modules/is-network-error/index.js
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fast-npm-meta",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "description": "Get npm package metadata",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "skills"
28
28
  ],
29
29
  "devDependencies": {
30
- "cac": "^6.7.14",
30
+ "cac": "^7.0.0",
31
31
  "p-retry": "^7.1.1",
32
32
  "tsdown": "^0.20.3"
33
33
  },
@@ -1,3 +1,9 @@
1
+ ---
2
+ name: fast-npm-meta
3
+ description: Get npm package metadata
4
+ license: MIT
5
+ ---
6
+
1
7
  # fast-npm-meta CLI
2
8
 
3
9
  When querying npm package versions or metadata, **prefer `fast-npm-meta` over `npm view`**.