1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 | const pjson = require('../package.json');
|
5 | const Config = require("@anycli/config");
|
6 | const Errors = require("@anycli/errors");
|
7 | const util_1 = require("util");
|
8 | const flags = require("./flags");
|
9 | class Command {
|
10 | constructor(argv, options) {
|
11 | this.argv = argv;
|
12 | this.id = this.ctor.id;
|
13 | this.config = Config.load(options || module.parent && module.parent.parent && module.parent.parent.filename || __dirname);
|
14 | try {
|
15 | this.debug = require('debug')(this.id ? `${this.config.bin}:${this.id}` : this.config.bin);
|
16 | }
|
17 | catch (_a) {
|
18 | this.debug = () => { };
|
19 | }
|
20 | }
|
21 | get ctor() {
|
22 | return this.constructor;
|
23 | }
|
24 | async _run() {
|
25 | let err;
|
26 | try {
|
27 | await this.init();
|
28 | await this.run();
|
29 | }
|
30 | catch (e) {
|
31 | err = e;
|
32 | await this.catch(e);
|
33 | }
|
34 | finally {
|
35 | await this.finally(err);
|
36 | }
|
37 | }
|
38 | exit(code = 0) { Errors.exit(code); }
|
39 | warn(input) { Errors.warn(input); }
|
40 | error(input, options = {}) {
|
41 | Errors.error(input, options);
|
42 | }
|
43 | log(message = '') {
|
44 | message = typeof message === 'string' ? message : util_1.inspect(message);
|
45 | process.stdout.write(message + '\n');
|
46 | }
|
47 | async init() {
|
48 | this.debug('init version: %s argv: %o', this.ctor._base, this.argv);
|
49 | if (this.config.debug)
|
50 | Errors.config.debug = true;
|
51 | if (this.config.errlog)
|
52 | Errors.config.errlog = this.config.errlog;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | global['http-call'] = global['http-call'] || {};
|
58 | global['http-call'].userAgent = this.config.userAgent;
|
59 | if (this._helpOverride())
|
60 | return this._help();
|
61 | }
|
62 | parse(options, argv = this.argv) {
|
63 | if (!options)
|
64 | options = this.constructor;
|
65 | return require('@anycli/parser').parse(argv, Object.assign({ context: this }, options));
|
66 | }
|
67 | async catch(err) {
|
68 | if (err.message.match(/Unexpected arguments?: (-h|--help)(,|\n)/)) {
|
69 | this._help();
|
70 | }
|
71 | else if (err.message.match(/Unexpected arguments?: (-v|--version)(,|\n)/)) {
|
72 | this._version();
|
73 | }
|
74 | else
|
75 | throw err;
|
76 | }
|
77 | async finally(_) {
|
78 | try {
|
79 | await require('cli-ux').done();
|
80 | }
|
81 | catch (_a) { }
|
82 | }
|
83 | _help() {
|
84 | const HHelp = require('@anycli/plugin-help').default;
|
85 | const help = new HHelp(this.config);
|
86 | help.showHelp(this.argv);
|
87 | this.exit(0);
|
88 | }
|
89 | _helpOverride() {
|
90 | if (this.argv[0] === '--version' || this.argv[0] === 'version')
|
91 | this._version();
|
92 | for (let arg of this.argv) {
|
93 | if (arg === '--help')
|
94 | return true;
|
95 | if (arg === '--')
|
96 | return false;
|
97 | }
|
98 | return false;
|
99 | }
|
100 | _version() {
|
101 | this.log(this.config.userAgent);
|
102 | this.exit(0);
|
103 | }
|
104 | }
|
105 | Command._base = `${pjson.name}@${pjson.version}`;
|
106 | Command.aliases = [];
|
107 | Command.strict = true;
|
108 | Command.parse = true;
|
109 | Command.flags = {
|
110 | version: flags.version(),
|
111 | help: flags.help(),
|
112 | };
|
113 | Command.args = [];
|
114 | Command.parserOptions = {};
|
115 |
|
116 |
|
117 |
|
118 | Command.run = async function (argv = process.argv.slice(2), opts) {
|
119 | let cmd = new this(argv, opts);
|
120 | await cmd._run(argv);
|
121 | };
|
122 | exports.default = Command;
|