UNPKG

cli-engine

Version:
162 lines (137 loc) 4.62 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = run; var _color = require('cli-engine-command/lib/color'); var _cliEngineConfig = require('cli-engine-config'); var _cliUx = require('cli-ux'); var _cliUx2 = _interopRequireDefault(_cliUx); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _semver = require('semver'); var _semver2 = _interopRequireDefault(_semver); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const debug = require('debug')('cli'); const handleEPIPE = err => { if (err.code !== 'EPIPE') throw err; }; if (!global.testing) { process.once('SIGINT', () => { if (_cliUx2.default.action.task) _cliUx2.default.action.stop(_color.color.red('ctrl-c')); _cliUx2.default.exit(1); }); let handleErr = err => { _cliUx2.default.error(err); }; process.once('uncaughtException', handleErr); process.once('unhandledRejection', handleErr); process.stdout.on('error', handleEPIPE); process.stderr.on('error', handleEPIPE); } process.env.CLI_ENGINE_VERSION = require('../package.json').version; class CLI { constructor({ config } = {}) { if (!config) config = {}; if (!config.initPath) { config.initPath = module.parent.filename; } if (!config.root) { const findUp = require('find-up'); config.root = _path2.default.dirname(findUp.sync('package.json', { cwd: module.parent.filename })); } this.config = (0, _cliEngineConfig.buildConfig)(config); global.config = this.config; global['cli-ux'] = { debug: this.config.debug, mock: this.config.mock }; } async run() { debug('starting run'); require('./fs'); const { Updater } = require('./updater'); const updater = new Updater(this.config); debug('checking autoupdater'); await updater.autoupdate(); const { Hooks } = require('./hooks'); this.hooks = new Hooks({ config: this.config }); await this.hooks.run('init'); if (this.cmdAskingForHelp) { debug('running help command'); this.cmd = await this.Help.run(this.config); } else { debug('dispatcher'); const id = this.config.argv[1]; const { Dispatcher } = require('./dispatcher'); const dispatcher = new Dispatcher(this.config); let Command = await dispatcher.findCommand(id || this.config.defaultCommand || 'help'); if (Command) { let { default: Lock } = require('./lock'); let lock = new Lock(this.config); await lock.unread(); let opts = { Command: Command, argv: this.config.argv.slice(2) }; await this.hooks.run('prerun', opts); if (!Command._version) { // old style command // flow$ignore debug('running old style command'); this.cmd = await Command.run({ argv: this.config.argv.slice(2), config: this.config, mock: this.config.mock }); } else { if (_semver2.default.satisfies(Command._version, '>=10.0.0-ts')) { debug('running ts command', { _version: Command._version }); this.cmd = await Command.run({ ...this.config, argv: this.config.argv.slice(1) }); } else { debug('running flow command', { _version: Command._version }); this.cmd = await Command.run(this.config); } } } else { let topic = await dispatcher.findTopic(id); if (topic) { await this.Help.run(this.config); } else { const { NotFound } = require('./not_found'); return new NotFound(this.config, this.config.argv).run(); } } } debug('flushing stdout'); const { timeout } = require('./util'); await timeout(this.flush(), 10000); debug('exiting'); _cliUx2.default.exit(0); } flush() { if (global.testing) return Promise.resolve(); let p = new Promise(resolve => process.stdout.once('drain', resolve)); process.stdout.write(''); return p; } get cmdAskingForHelp() { for (let arg of this.config.argv) { if (['--help', '-h'].includes(arg)) return true; if (arg === '--') return false; } return false; } get Help() { const { default: Help } = this.config.userPlugins ? require('./commands/help') : require('./commands/newhelp'); return Help; } } exports.default = CLI; function run({ config } = {}) { if (!config) config = {}; const cli = new CLI({ config }); return cli.run(); }