UNPKG

1.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const help_1 = require("@oclif/help");
4const command_1 = require("./command");
5const ROOT_INDEX_CMD_ID = '';
6class Main extends command_1.default {
7 static run(argv = process.argv.slice(2), options) {
8 return super.run(argv, options || (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname);
9 }
10 async init() {
11 const [id, ...argv] = this.argv;
12 await this.config.runHook('init', { id, argv });
13 return super.init();
14 }
15 async run() {
16 let [id, ...argv] = this.argv;
17 this.parse(Object.assign({ strict: false, '--': false }, this.ctor));
18 if (!this.config.findCommand(id)) {
19 const topic = this.config.findTopic(id);
20 if (topic)
21 return this._help();
22 if (this.config.findCommand(ROOT_INDEX_CMD_ID)) {
23 id = ROOT_INDEX_CMD_ID;
24 argv = this.argv;
25 }
26 }
27 await this.config.runCommand(id, argv);
28 }
29 _helpOverride() {
30 if (['-v', '--version', 'version'].includes(this.argv[0]))
31 return this._version();
32 if (['-h', 'help'].includes(this.argv[0]))
33 return true;
34 if (this.argv.length === 0 && !this.config.findCommand(ROOT_INDEX_CMD_ID))
35 return true;
36 for (const arg of this.argv) {
37 if (arg === '--help')
38 return true;
39 if (arg === '--')
40 return false;
41 }
42 return false;
43 }
44 _help() {
45 const HelpClass = help_1.getHelpClass(this.config);
46 const help = new HelpClass(this.config);
47 help.showHelp(this.argv);
48 return this.exit(0);
49 }
50}
51exports.Main = Main;
52function run(argv = process.argv.slice(2), options) {
53 return Main.run(argv, options);
54}
55exports.run = run;