UNPKG

3.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4require('./fs');
5const color_1 = require("@heroku-cli/color");
6const cli_ux_1 = require("cli-ux");
7const path = require("path");
8const config_1 = require("./config");
9const deps_1 = require("./deps");
10class CLI {
11 constructor(config) {
12 this.config = config;
13 }
14 run(argv) {
15 return tslib_1.__awaiter(this, void 0, void 0, function* () {
16 this.debug('starting run: %o', argv);
17 this.setupHandlers();
18 const id = argv[2];
19 if (id !== 'update') {
20 const updater = new deps_1.default.Updater(this.config);
21 yield updater.autoupdate();
22 }
23 const commands = new deps_1.default.CommandManager(this.config);
24 const result = yield commands.run(argv);
25 yield this.exitAfterStdoutFlush();
26 return result;
27 });
28 }
29 get debug() {
30 return require('debug')('cli');
31 }
32 get global() {
33 return global;
34 }
35 setupHandlers() {
36 process.env.CLI_ENGINE_VERSION = require('../package.json').version;
37 if (this.global.testing)
38 return;
39 process.once('SIGINT', () => {
40 if (cli_ux_1.default.action.task)
41 cli_ux_1.default.action.stop(color_1.color.red('ctrl-c'));
42 cli_ux_1.default.exit(1);
43 });
44 let handleErr = (err) => {
45 cli_ux_1.default.error(err);
46 };
47 process.once('uncaughtException', handleErr);
48 process.once('unhandledRejection', handleErr);
49 const handleEPIPE = (err) => {
50 if (err.code !== 'EPIPE')
51 throw err;
52 };
53 process.stdout.on('error', handleEPIPE);
54 process.stderr.on('error', handleEPIPE);
55 }
56 exitAfterStdoutFlush() {
57 return tslib_1.__awaiter(this, void 0, void 0, function* () {
58 if (this.global.testing)
59 return;
60 yield deps_1.default.util.timeout(this.flush(), 10000);
61 });
62 }
63 flush() {
64 let p = new Promise(resolve => process.stdout.once('drain', resolve));
65 process.stdout.write('');
66 return p;
67 }
68}
69exports.default = CLI;
70function run(arg1 = process.argv, opts = {}) {
71 let argv;
72 if (Array.isArray(arg1))
73 argv = arg1;
74 else {
75 opts = arg1;
76 argv = opts.argv || process.argv;
77 }
78 if (!opts.root)
79 opts.root = path.join(module.parent.filename, '..', '..');
80 if (!opts.pjson) {
81 const f = path.join(opts.root, 'package.json');
82 opts.pjson = require(f);
83 deps_1.default.validate.cliPjson(opts.pjson, f);
84 }
85 const config = new config_1.Config(opts);
86 config.plugins = new deps_1.default.Plugins(config);
87 if (config.debug)
88 cli_ux_1.default.config.debug = true;
89 cli_ux_1.default.config.errlog = config.errlog;
90 return new CLI(config).run(argv);
91}
92exports.run = run;
93var hooks_1 = require("./hooks");
94exports.Hook = hooks_1.Hook;