UNPKG

3.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const Errors = tslib_1.__importStar(require("@oclif/errors"));
5const util = tslib_1.__importStar(require("util"));
6const base_1 = require("./action/base");
7exports.ActionBase = base_1.ActionBase;
8const config_1 = require("./config");
9exports.config = config_1.config;
10exports.Config = config_1.Config;
11const deps_1 = tslib_1.__importDefault(require("./deps"));
12const exit_1 = require("./exit");
13exports.ExitError = exit_1.ExitError;
14const Table = tslib_1.__importStar(require("./styled/table"));
15exports.Table = Table;
16exports.ux = {
17 config: config_1.config,
18 warn: Errors.warn,
19 error: Errors.error,
20 exit: Errors.exit,
21 get prompt() { return deps_1.default.prompt.prompt; },
22 /**
23 * "press anykey to continue"
24 */
25 get anykey() { return deps_1.default.prompt.anykey; },
26 get confirm() { return deps_1.default.prompt.confirm; },
27 get action() { return config_1.config.action; },
28 get prideAction() { return config_1.config.prideAction; },
29 styledObject(obj, keys) { exports.ux.info(deps_1.default.styledObject(obj, keys)); },
30 get styledHeader() { return deps_1.default.styledHeader; },
31 get styledJSON() { return deps_1.default.styledJSON; },
32 get table() { return deps_1.default.table; },
33 get tree() { return deps_1.default.tree; },
34 get open() { return deps_1.default.open; },
35 get wait() { return deps_1.default.wait; },
36 async done() {
37 config_1.config.action.stop();
38 // await flushStdout()
39 },
40 trace(format, ...args) {
41 if (this.config.outputLevel === 'trace') {
42 process.stdout.write(util.format(format, ...args) + '\n');
43 }
44 },
45 debug(format, ...args) {
46 if (['trace', 'debug'].includes(this.config.outputLevel)) {
47 process.stdout.write(util.format(format, ...args) + '\n');
48 }
49 },
50 info(format, ...args) {
51 process.stdout.write(util.format(format, ...args) + '\n');
52 },
53 log(format, ...args) {
54 this.info(format || '', ...args);
55 },
56 url(text, uri, params = {}) {
57 const supports = require('supports-hyperlinks');
58 if (supports.stdout) {
59 const hyperlinker = require('hyperlinker');
60 this.log(hyperlinker(text, uri, params));
61 }
62 else {
63 this.log(uri);
64 }
65 },
66 annotation(text, annotation) {
67 const supports = require('supports-hyperlinks');
68 if (supports.stdout) {
69 //\u001b]8;;https://google.com\u0007sometext\u001b]8;;\u0007
70 this.log(`\u001b]1337;AddAnnotation=${text.length}|${annotation}\u0007${text}`);
71 }
72 else {
73 this.log(text);
74 }
75 },
76 async flush() {
77 function timeout(p, ms) {
78 function wait(ms, unref = false) {
79 return new Promise(resolve => {
80 let t = setTimeout(() => resolve(), ms);
81 if (unref)
82 t.unref();
83 });
84 }
85 return Promise.race([p, wait(ms, true).then(() => exports.ux.error('timed out'))]);
86 }
87 async function flush() {
88 let p = new Promise(resolve => process.stdout.once('drain', () => resolve()));
89 process.stdout.write('');
90 return p;
91 }
92 await timeout(flush(), 10000);
93 }
94};
95exports.default = exports.ux;
96exports.cli = exports.ux;
97const cliuxProcessExitHandler = async () => {
98 try {
99 await exports.ux.done();
100 }
101 catch (err) {
102 // tslint:disable no-console
103 console.error(err);
104 process.exitCode = 1;
105 }
106};
107// to avoid MaxListenersExceededWarning
108// only attach named listener once
109let cliuxListener = process.listeners('exit').find(fn => fn.name === cliuxProcessExitHandler.name);
110if (!cliuxListener) {
111 process.once('exit', cliuxProcessExitHandler);
112}