@xmorse/cac 6.0.0

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