1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.run = run;
|
7 |
|
8 | var _color = require('cli-engine-command/lib/color');
|
9 |
|
10 | var _cliEngineConfig = require('cli-engine-config');
|
11 |
|
12 | var _cliUx = require('cli-ux');
|
13 |
|
14 | var _cliUx2 = _interopRequireDefault(_cliUx);
|
15 |
|
16 | var _path = require('path');
|
17 |
|
18 | var _path2 = _interopRequireDefault(_path);
|
19 |
|
20 | var _semver = require('semver');
|
21 |
|
22 | var _semver2 = _interopRequireDefault(_semver);
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 | const debug = require('debug')('cli');
|
27 | const handleEPIPE = err => {
|
28 | if (err.code !== 'EPIPE') throw err;
|
29 | };
|
30 |
|
31 | if (!global.testing) {
|
32 | process.once('SIGINT', () => {
|
33 | if (_cliUx2.default.action.task) _cliUx2.default.action.stop(_color.color.red('ctrl-c'));
|
34 | _cliUx2.default.exit(1);
|
35 | });
|
36 | let handleErr = err => {
|
37 | _cliUx2.default.error(err);
|
38 | };
|
39 | process.once('uncaughtException', handleErr);
|
40 | process.once('unhandledRejection', handleErr);
|
41 | process.stdout.on('error', handleEPIPE);
|
42 | process.stderr.on('error', handleEPIPE);
|
43 | }
|
44 |
|
45 | process.env.CLI_ENGINE_VERSION = require('../package.json').version;
|
46 |
|
47 | class CLI {
|
48 |
|
49 | constructor({ config } = {}) {
|
50 | if (!config) config = {};
|
51 | if (!config.initPath) {
|
52 | config.initPath = module.parent.filename;
|
53 | }
|
54 | if (!config.root) {
|
55 | const findUp = require('find-up');
|
56 | config.root = _path2.default.dirname(findUp.sync('package.json', {
|
57 | cwd: module.parent.filename
|
58 | }));
|
59 | }
|
60 | this.config = (0, _cliEngineConfig.buildConfig)(config);
|
61 | global.config = this.config;
|
62 | global['cli-ux'] = {
|
63 | debug: this.config.debug,
|
64 | mock: this.config.mock
|
65 | };
|
66 | }
|
67 |
|
68 | async run() {
|
69 | debug('starting run');
|
70 |
|
71 | require('./fs');
|
72 | const { Updater } = require('./updater');
|
73 | const updater = new Updater(this.config);
|
74 | debug('checking autoupdater');
|
75 | await updater.autoupdate();
|
76 |
|
77 | const { Hooks } = require('./hooks');
|
78 | this.hooks = new Hooks({ config: this.config });
|
79 | await this.hooks.run('init');
|
80 |
|
81 | if (this.cmdAskingForHelp) {
|
82 | debug('running help command');
|
83 | this.cmd = await this.Help.run(this.config);
|
84 | } else {
|
85 | debug('dispatcher');
|
86 | const id = this.config.argv[1];
|
87 | const { Dispatcher } = require('./dispatcher');
|
88 | const dispatcher = new Dispatcher(this.config);
|
89 | let Command = await dispatcher.findCommand(id || this.config.defaultCommand || 'help');
|
90 |
|
91 | if (Command) {
|
92 | let { default: Lock } = require('./lock');
|
93 | let lock = new Lock(this.config);
|
94 | await lock.unread();
|
95 | let opts = {
|
96 | Command: Command,
|
97 | argv: this.config.argv.slice(2)
|
98 | };
|
99 | await this.hooks.run('prerun', opts);
|
100 | if (!Command._version) {
|
101 |
|
102 |
|
103 | debug('running old style command');
|
104 | this.cmd = await Command.run({
|
105 | argv: this.config.argv.slice(2),
|
106 | config: this.config,
|
107 | mock: this.config.mock
|
108 | });
|
109 | } else {
|
110 | if (_semver2.default.satisfies(Command._version, '>=10.0.0-ts')) {
|
111 | debug('running ts command', { _version: Command._version });
|
112 | this.cmd = await Command.run({ ...this.config, argv: this.config.argv.slice(1) });
|
113 | } else {
|
114 | debug('running flow command', { _version: Command._version });
|
115 | this.cmd = await Command.run(this.config);
|
116 | }
|
117 | }
|
118 | } else {
|
119 | let topic = await dispatcher.findTopic(id);
|
120 | if (topic) {
|
121 | await this.Help.run(this.config);
|
122 | } else {
|
123 | const { NotFound } = require('./not_found');
|
124 | return new NotFound(this.config, this.config.argv).run();
|
125 | }
|
126 | }
|
127 | }
|
128 |
|
129 | debug('flushing stdout');
|
130 | const { timeout } = require('./util');
|
131 | await timeout(this.flush(), 10000);
|
132 | debug('exiting');
|
133 | _cliUx2.default.exit(0);
|
134 | }
|
135 |
|
136 | flush() {
|
137 | if (global.testing) return Promise.resolve();
|
138 | let p = new Promise(resolve => process.stdout.once('drain', resolve));
|
139 | process.stdout.write('');
|
140 | return p;
|
141 | }
|
142 |
|
143 | get cmdAskingForHelp() {
|
144 | for (let arg of this.config.argv) {
|
145 | if (['--help', '-h'].includes(arg)) return true;
|
146 | if (arg === '--') return false;
|
147 | }
|
148 | return false;
|
149 | }
|
150 |
|
151 | get Help() {
|
152 | const { default: Help } = this.config.userPlugins ? require('./commands/help') : require('./commands/newhelp');
|
153 | return Help;
|
154 | }
|
155 | }
|
156 |
|
157 | exports.default = CLI;
|
158 | function run({ config } = {}) {
|
159 | if (!config) config = {};
|
160 | const cli = new CLI({ config });
|
161 | return cli.run();
|
162 | } |
\ | No newline at end of file |