UNPKG

5.09 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const deps_1 = require("./deps");
12const util_1 = require("util");
13const deprecatedColor = util_1.deprecate(() => require('heroku-cli-color').default, "cli.color is deprecated. Please use `import color from 'heroku-cli-color'` instead.");
14class CLI extends deps_1.default.Base {
15 get Prompt() {
16 if (!this._prompt) {
17 this._prompt = new deps_1.default.Prompt();
18 }
19 return this._prompt;
20 }
21 get Errors() {
22 if (!this._errors) {
23 this._errors = new deps_1.default.Errors();
24 }
25 return this._errors;
26 }
27 get action() {
28 if (!this._action)
29 this._action = deps_1.default.ActionBase.getSpinner();
30 return this._action;
31 }
32 prompt(name, options = {}) {
33 return this.action.pauseAsync(() => {
34 return this.Prompt.prompt(name, options);
35 }, deps_1.default.chalk.cyan('?'));
36 }
37 confirm(message) {
38 return this.action.pauseAsync(() => __awaiter(this, void 0, void 0, function* () {
39 const confirm = () => __awaiter(this, void 0, void 0, function* () {
40 let response = (yield this.Prompt.prompt(message)).toLowerCase();
41 if (['n', 'no'].includes(response))
42 return false;
43 if (['y', 'yes'].includes(response))
44 return true;
45 return confirm();
46 });
47 return confirm();
48 }), deps_1.default.chalk.cyan('?'));
49 }
50 log(data, ...args) {
51 this.action.pause(() => {
52 return this.stdout.log(data, ...args);
53 });
54 }
55 warn(err, options = {}) {
56 this.action.pause(() => {
57 return this.Errors.warn(err, options);
58 }, deps_1.default.chalk.bold.yellow('!'));
59 }
60 error(err, options = {}) {
61 this.action.pause(() => {
62 return this.Errors.error(err, options);
63 }, deps_1.default.chalk.bold.red('!'));
64 }
65 exit(code = 1) {
66 this.Errors.exit(code);
67 }
68 table(data, options) {
69 let table = require('./table');
70 return table(this, data, options);
71 }
72 styledJSON(obj) {
73 let json = JSON.stringify(obj, null, 2);
74 if (deps_1.default.chalk.enabled) {
75 let cardinal = require('cardinal');
76 let theme = require('cardinal/themes/jq');
77 this.log(cardinal.highlight(json, { json: true, theme: theme }));
78 }
79 else {
80 this.log(json);
81 }
82 }
83 styledHeader(header) {
84 this.log(deps_1.default.chalk.dim('=== ') + deps_1.default.chalk.bold(header));
85 }
86 styledObject(obj, keys) {
87 const util = require('util');
88 let keyLengths = Object.keys(obj).map(key => key.toString().length);
89 let maxKeyLength = Math.max.apply(Math, keyLengths) + 2;
90 function pp(obj) {
91 if (typeof obj === 'string' || typeof obj === 'number') {
92 return obj;
93 }
94 else if (typeof obj === 'object') {
95 return Object.keys(obj)
96 .map(k => k + ': ' + util.inspect(obj[k]))
97 .join(', ');
98 }
99 else {
100 return util.inspect(obj);
101 }
102 }
103 let logKeyValue = (key, value) => {
104 this.log(`${deps_1.default.chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value));
105 };
106 for (var key of keys || Object.keys(obj).sort()) {
107 let value = obj[key];
108 if (Array.isArray(value)) {
109 if (value.length > 0) {
110 logKeyValue(key, value[0]);
111 for (var e of value.slice(1)) {
112 this.log(' '.repeat(maxKeyLength) + pp(e));
113 }
114 }
115 }
116 else if (value !== null && value !== undefined) {
117 logKeyValue(key, value);
118 }
119 }
120 }
121 get color() {
122 return deprecatedColor();
123 }
124 /**
125 * puts in a handler for process.on('uncaughtException') and process.on('unhandledRejection')
126 */
127 handleUnhandleds() {
128 this.Errors.handleUnhandleds();
129 }
130 /**
131 * cleanup any outstanding output like actions that need to be stopped
132 */
133 done() {
134 this.action.stop();
135 }
136}
137exports.CLI = CLI;
138exports.cli = new CLI();
139exports.default = exports.cli;