UNPKG

@rnv/cli

Version:
441 lines 22.7 kB
"use strict"; var _Ora_linesToClear, _Ora_isDiscardingStdin, _Ora_lineCount, _Ora_frameIndex, _Ora_options, _Ora_spinner, _Ora_stream, _Ora_id, _Ora_initialInterval, _Ora_isEnabled, _Ora_isSilent, _Ora_indent, _Ora_text, _Ora_prefixText, _StdinDiscarder_requests, _StdinDiscarder_mutedStream, _StdinDiscarder_ourEmit, _StdinDiscarder_rl; Object.defineProperty(exports, "__esModule", { value: true }); exports.oraPromise = void 0; var tslib_1 = require("tslib"); //@ts-nocheck var readline_1 = tslib_1.__importDefault(require("readline")); var chalk_1 = tslib_1.__importDefault(require("chalk")); var cli_cursor_1 = tslib_1.__importDefault(require("cli-cursor")); var cli_spinners_1 = tslib_1.__importDefault(require("cli-spinners")); var log_symbols_1 = tslib_1.__importDefault(require("log-symbols")); var wcwidth_1 = tslib_1.__importDefault(require("wcwidth")); var is_interactive_1 = tslib_1.__importDefault(require("is-interactive")); var is_unicode_supported_1 = tslib_1.__importDefault(require("is-unicode-supported")); var bl_1 = require("bl"); var logger_1 = require("./logger"); var stdinDiscarder; var Ora = /** @class */ (function () { function Ora(options) { _Ora_linesToClear.set(this, 0); _Ora_isDiscardingStdin.set(this, false); _Ora_lineCount.set(this, 0); _Ora_frameIndex.set(this, 0); _Ora_options.set(this, void 0); _Ora_spinner.set(this, void 0); _Ora_stream.set(this, void 0); _Ora_id.set(this, void 0); _Ora_initialInterval.set(this, void 0); _Ora_isEnabled.set(this, void 0); _Ora_isSilent.set(this, void 0); _Ora_indent.set(this, void 0); _Ora_text.set(this, void 0); _Ora_prefixText.set(this, void 0); if (!stdinDiscarder) { stdinDiscarder = new StdinDiscarder(); } var opts = options; if (typeof options === 'string') { opts = { text: options, }; } tslib_1.__classPrivateFieldSet(this, _Ora_options, tslib_1.__assign({ color: 'cyan', stream: process.stderr, discardStdin: true, hideCursor: true }, opts), "f"); // Public this.color = tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").color; // It's important that these use the public setters. this.spinner = tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").spinner; tslib_1.__classPrivateFieldSet(this, _Ora_initialInterval, tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").interval, "f"); tslib_1.__classPrivateFieldSet(this, _Ora_stream, tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").stream, "f"); tslib_1.__classPrivateFieldSet(this, _Ora_isEnabled, typeof tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").isEnabled === 'boolean' ? tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").isEnabled : (0, is_interactive_1.default)({ stream: tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f") }), "f"); tslib_1.__classPrivateFieldSet(this, _Ora_isSilent, typeof tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").isSilent === 'boolean' ? tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").isSilent : false, "f"); // Set *after* `this.#stream`. // It's important that these use the public setters. this.text = tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").text; this.prefixText = tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").prefixText; this.indent = tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").indent; if (process.env.NODE_ENV === 'test') { this._stream = tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f"); this._isEnabled = tslib_1.__classPrivateFieldGet(this, _Ora_isEnabled, "f"); Object.defineProperty(this, '_linesToClear', { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_linesToClear, "f"); }, set: function (newValue) { tslib_1.__classPrivateFieldSet(this, _Ora_linesToClear, newValue, "f"); }, }); Object.defineProperty(this, '_frameIndex', { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_frameIndex, "f"); }, }); Object.defineProperty(this, '_lineCount', { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_lineCount, "f"); }, }); } } Object.defineProperty(Ora.prototype, "indent", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f"); }, set: function (indent) { if (indent === void 0) { indent = 0; } if (!(indent >= 0 && Number.isInteger(indent))) { throw new Error('The `indent` option must be an integer from 0 and up'); } tslib_1.__classPrivateFieldSet(this, _Ora_indent, indent, "f"); this.updateLineCount(); }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "interval", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_initialInterval, "f") || tslib_1.__classPrivateFieldGet(this, _Ora_spinner, "f").interval || 100; }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "spinner", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_spinner, "f"); }, set: function (spinner) { tslib_1.__classPrivateFieldSet(this, _Ora_frameIndex, 0, "f"); tslib_1.__classPrivateFieldSet(this, _Ora_initialInterval, undefined, "f"); if (typeof spinner === 'object') { if (spinner.frames === undefined) { throw new Error('The given spinner must have a `frames` property'); } tslib_1.__classPrivateFieldSet(this, _Ora_spinner, spinner, "f"); } else if (!(0, is_unicode_supported_1.default)()) { tslib_1.__classPrivateFieldSet(this, _Ora_spinner, cli_spinners_1.default.line, "f"); } else if (spinner === undefined) { // Set default spinner tslib_1.__classPrivateFieldSet(this, _Ora_spinner, cli_spinners_1.default.dots, "f"); } else if (spinner !== 'default' && cli_spinners_1.default[spinner]) { tslib_1.__classPrivateFieldSet(this, _Ora_spinner, cli_spinners_1.default[spinner], "f"); } else { throw new Error("There is no built-in spinner named '".concat(spinner, "'. See https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json for a full list.")); } }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "text", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_text, "f"); }, set: function (value) { tslib_1.__classPrivateFieldSet(this, _Ora_text, value || '', "f"); this.updateLineCount(); }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "prefixText", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f"); }, set: function (value) { tslib_1.__classPrivateFieldSet(this, _Ora_prefixText, value || '', "f"); this.updateLineCount(); }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "isSpinning", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_id, "f") !== undefined; }, enumerable: false, configurable: true }); // TODO: Use private methods when targeting Node.js 14. Ora.prototype.getFullPrefixText = function (prefixText, postfix) { if (prefixText === void 0) { prefixText = tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f"); } if (postfix === void 0) { postfix = ' '; } if (typeof prefixText === 'string' && prefixText !== '') { return prefixText + postfix; } if (typeof prefixText === 'function') { return prefixText() + postfix; } return ''; }; Ora.prototype.updateLineCount = function () { var columns = tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").columns || 80; var fullPrefixText = this.getFullPrefixText(tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f"), '-'); tslib_1.__classPrivateFieldSet(this, _Ora_lineCount, 0, "f"); var linesStr = (0, logger_1.stripAnsi)("".concat(' '.repeat(tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f")) + fullPrefixText, "--").concat(tslib_1.__classPrivateFieldGet(this, _Ora_text, "f"))); var lines = linesStr.split('\n'); // for (const line of lines) { // this.#lineCount += Math.max(1, Math.ceil(wcwidth(line) / columns)); // } for (var i = 0; i < lines.length; i++) { tslib_1.__classPrivateFieldSet(this, _Ora_lineCount, tslib_1.__classPrivateFieldGet(this, _Ora_lineCount, "f") + Math.max(1, Math.ceil((0, wcwidth_1.default)(lines[i]) / columns)), "f"); } }; Object.defineProperty(Ora.prototype, "isEnabled", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_isEnabled, "f") && !tslib_1.__classPrivateFieldGet(this, _Ora_isSilent, "f"); }, set: function (value) { if (typeof value !== 'boolean') { throw new TypeError('The `isEnabled` option must be a boolean'); } tslib_1.__classPrivateFieldSet(this, _Ora_isEnabled, value, "f"); }, enumerable: false, configurable: true }); Object.defineProperty(Ora.prototype, "isSilent", { get: function () { return tslib_1.__classPrivateFieldGet(this, _Ora_isSilent, "f"); }, set: function (value) { if (typeof value !== 'boolean') { throw new TypeError('The `isSilent` option must be a boolean'); } tslib_1.__classPrivateFieldSet(this, _Ora_isSilent, value, "f"); }, enumerable: false, configurable: true }); Ora.prototype.frame = function () { var _a; var frames = tslib_1.__classPrivateFieldGet(this, _Ora_spinner, "f").frames; var frame = frames[tslib_1.__classPrivateFieldGet(this, _Ora_frameIndex, "f")]; if (this.color) { frame = chalk_1.default[this.color](frame); } tslib_1.__classPrivateFieldSet(this, _Ora_frameIndex, tslib_1.__classPrivateFieldSet(this, _Ora_frameIndex, (_a = tslib_1.__classPrivateFieldGet(this, _Ora_frameIndex, "f"), ++_a), "f") % frames.length, "f"); var fullPrefixText = typeof tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f") === 'string' && tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f") !== '' ? "".concat(tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f"), " ") : ''; var fullText = typeof this.text === 'string' ? " ".concat(this.text) : ''; return fullPrefixText + frame + fullText; }; Ora.prototype.clear = function () { if (!tslib_1.__classPrivateFieldGet(this, _Ora_isEnabled, "f") || !tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").isTTY) { return this; } tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").cursorTo(0); for (var index = 0; index < tslib_1.__classPrivateFieldGet(this, _Ora_linesToClear, "f"); index++) { if (index > 0) { tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").moveCursor(0, -1); } tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").clearLine(1); } if (tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f") || this.lastIndent !== tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f")) { tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").cursorTo(tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f")); } this.lastIndent = tslib_1.__classPrivateFieldGet(this, _Ora_indent, "f"); tslib_1.__classPrivateFieldSet(this, _Ora_linesToClear, 0, "f"); return this; }; Ora.prototype.render = function () { if (tslib_1.__classPrivateFieldGet(this, _Ora_isSilent, "f")) { return this; } this.clear(); tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").write(this.frame()); tslib_1.__classPrivateFieldSet(this, _Ora_linesToClear, tslib_1.__classPrivateFieldGet(this, _Ora_lineCount, "f"), "f"); return this; }; Ora.prototype.start = function (text) { if (text) { this.text = text; } if (tslib_1.__classPrivateFieldGet(this, _Ora_isSilent, "f")) { return this; } if (!tslib_1.__classPrivateFieldGet(this, _Ora_isEnabled, "f")) { if (this.text) { tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").write("- ".concat(this.text, "\n")); } return this; } if (this.isSpinning) { return this; } if (tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").hideCursor) { cli_cursor_1.default.hide(tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f")); } if (tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").discardStdin && process.stdin.isTTY) { tslib_1.__classPrivateFieldSet(this, _Ora_isDiscardingStdin, true, "f"); stdinDiscarder.start(); } this.render(); tslib_1.__classPrivateFieldSet(this, _Ora_id, setInterval(this.render.bind(this), this.interval), "f"); return this; }; Ora.prototype.stop = function () { if (!tslib_1.__classPrivateFieldGet(this, _Ora_isEnabled, "f")) { return this; } clearInterval(tslib_1.__classPrivateFieldGet(this, _Ora_id, "f")); tslib_1.__classPrivateFieldSet(this, _Ora_id, undefined, "f"); tslib_1.__classPrivateFieldSet(this, _Ora_frameIndex, 0, "f"); this.clear(); if (tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").hideCursor) { cli_cursor_1.default.show(tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f")); } if (tslib_1.__classPrivateFieldGet(this, _Ora_options, "f").discardStdin && process.stdin.isTTY && tslib_1.__classPrivateFieldGet(this, _Ora_isDiscardingStdin, "f")) { stdinDiscarder.stop(); tslib_1.__classPrivateFieldSet(this, _Ora_isDiscardingStdin, false, "f"); } return this; }; Ora.prototype.succeed = function (text) { return this.stopAndPersist({ symbol: log_symbols_1.default.success, text: text }); }; Ora.prototype.fail = function (text) { return this.stopAndPersist({ symbol: log_symbols_1.default.error, text: text }); }; Ora.prototype.warn = function (text) { return this.stopAndPersist({ symbol: log_symbols_1.default.warning, text: text }); }; Ora.prototype.info = function (text) { return this.stopAndPersist({ symbol: log_symbols_1.default.info, text: text }); }; Ora.prototype.stopAndPersist = function (options) { if (options === void 0) { options = {}; } if (tslib_1.__classPrivateFieldGet(this, _Ora_isSilent, "f")) { return this; } var prefixText = options.prefixText || tslib_1.__classPrivateFieldGet(this, _Ora_prefixText, "f"); var text = options.text || this.text; var fullText = typeof text === 'string' ? " ".concat(text) : ''; this.stop(); tslib_1.__classPrivateFieldGet(this, _Ora_stream, "f").write("".concat(this.getFullPrefixText(prefixText, ' ')).concat(options.symbol || ' ').concat(fullText, "\n")); return this; }; return Ora; }()); _Ora_linesToClear = new WeakMap(), _Ora_isDiscardingStdin = new WeakMap(), _Ora_lineCount = new WeakMap(), _Ora_frameIndex = new WeakMap(), _Ora_options = new WeakMap(), _Ora_spinner = new WeakMap(), _Ora_stream = new WeakMap(), _Ora_id = new WeakMap(), _Ora_initialInterval = new WeakMap(), _Ora_isEnabled = new WeakMap(), _Ora_isSilent = new WeakMap(), _Ora_indent = new WeakMap(), _Ora_text = new WeakMap(), _Ora_prefixText = new WeakMap(); function ora(options) { return new Ora(options); } exports.default = ora; function oraPromise(action, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { var actionIsFunction, actionIsPromise, _a, successText, failText, spinner, promise, result, opt1, error_1, opt2; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: actionIsFunction = typeof action === 'function'; actionIsPromise = typeof action.then === 'function'; if (!actionIsFunction && !actionIsPromise) { throw new TypeError('Parameter `action` must be a Function or a Promise'); } _a = typeof options === 'object' ? options : { successText: undefined, failText: undefined }, successText = _a.successText, failText = _a.failText; spinner = ora(options).start(); _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); promise = actionIsFunction ? action(spinner) : action; return [4 /*yield*/, promise]; case 2: result = _b.sent(); opt1 = typeof successText === 'string' ? successText : successText(result); spinner.succeed(successText === undefined ? undefined : opt1); return [2 /*return*/, result]; case 3: error_1 = _b.sent(); opt2 = typeof failText === 'string' ? failText : failText(error_1); spinner.fail(failText === undefined ? undefined : opt2); throw error_1; case 4: return [2 /*return*/]; } }); }); } exports.oraPromise = oraPromise; var ASCII_ETX_CODE = 0x03; // Ctrl+C emits this code var StdinDiscarder = /** @class */ (function () { function StdinDiscarder() { var _this = this; _StdinDiscarder_requests.set(this, 0); _StdinDiscarder_mutedStream.set(this, new bl_1.BufferListStream()); _StdinDiscarder_ourEmit.set(this, void 0); _StdinDiscarder_rl.set(this, void 0); tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_mutedStream, "f").pipe(process.stdout); var self = this; //eslint-disable-line tslib_1.__classPrivateFieldSet(this, _StdinDiscarder_ourEmit, function (event, data) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } var stdin = process.stdin; if (tslib_1.__classPrivateFieldGet(self, _StdinDiscarder_requests, "f") > 0 || stdin.emit === tslib_1.__classPrivateFieldGet(self, _StdinDiscarder_ourEmit, "f")) { if (event === 'keypress') { // Fixes readline behavior return; } if (event === 'data' && data.includes(ASCII_ETX_CODE)) { process.emit('SIGINT'); } Reflect.apply(tslib_1.__classPrivateFieldGet(self, _StdinDiscarder_ourEmit, "f"), _this, tslib_1.__spreadArray([event, data], args, true)); } else { Reflect.apply(process.stdin.emit, _this, tslib_1.__spreadArray([event, data], args, true)); } }, "f"); } StdinDiscarder.prototype.start = function () { var _a; tslib_1.__classPrivateFieldSet(this, _StdinDiscarder_requests, (_a = tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_requests, "f"), _a++, _a), "f"); if (tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_requests, "f") === 1) { this._realStart(); } }; StdinDiscarder.prototype.stop = function () { var _a; if (tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_requests, "f") <= 0) { throw new Error('`stop` called more times than `start`'); } tslib_1.__classPrivateFieldSet(this, _StdinDiscarder_requests, (_a = tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_requests, "f"), _a--, _a), "f"); if (tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_requests, "f") === 0) { this._realStop(); } }; // TODO: Use private methods when targeting Node.js 14. StdinDiscarder.prototype._realStart = function () { var _this = this; // No known way to make it work reliably on Windows if (process.platform === 'win32') { return; } tslib_1.__classPrivateFieldSet(this, _StdinDiscarder_rl, readline_1.default.createInterface({ input: process.stdin, output: tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_mutedStream, "f"), }), "f"); tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_rl, "f").on('SIGINT', function () { if (process.listenerCount('SIGINT') === 0) { process.emit('SIGINT'); } else { tslib_1.__classPrivateFieldGet(_this, _StdinDiscarder_rl, "f").close(); process.kill(process.pid, 'SIGINT'); } }); }; StdinDiscarder.prototype._realStop = function () { if (process.platform === 'win32') { return; } tslib_1.__classPrivateFieldGet(this, _StdinDiscarder_rl, "f").close(); tslib_1.__classPrivateFieldSet(this, _StdinDiscarder_rl, undefined, "f"); }; return StdinDiscarder; }()); _StdinDiscarder_requests = new WeakMap(), _StdinDiscarder_mutedStream = new WeakMap(), _StdinDiscarder_ourEmit = new WeakMap(), _StdinDiscarder_rl = new WeakMap(); //# sourceMappingURL=ora.js.map