JezK
Edit File: base-reporter.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; exports.stringifyLangArgs = stringifyLangArgs; function _regenerator() { var data = _interopRequireDefault(require("@babel/runtime/regenerator")); _regenerator = function _regenerator() { return data; }; return data; } function _asyncToGenerator2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); _asyncToGenerator2 = function _asyncToGenerator2() { return data; }; return data; } function _classCallCheck2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); _classCallCheck2 = function _classCallCheck2() { return data; }; return data; } function _createClass2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); _createClass2 = function _createClass2() { return data; }; return data; } function _defineProperty2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function _defineProperty2() { return data; }; return data; } function _format() { var data = require("./format.js"); _format = function _format() { return data; }; return data; } function languages() { var data = _interopRequireWildcard(require("./lang/index.js")); languages = function languages() { return data; }; return data; } function _os() { var data = _interopRequireDefault(require("os")); _os = function _os() { return data; }; return data; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* eslint no-unused-vars: 0 */ var isCI = require('ci-info').isCI; var util = require('util'); var EventEmitter = require('events').EventEmitter; function stringifyLangArgs(args) { return args.map(function (val) { if (val != null && val.inspect) { return val.inspect(); } else { try { var str = JSON.stringify(val) || val + ''; // should match all literal line breaks and // "u001b" that follow an odd number of backslashes and convert them to ESC // we do this because the JSON.stringify process has escaped these characters return str.replace(/((?:^|[^\\])(?:\\{2})*)\\u001[bB]/g, "$1\x1B").replace(/[\\]r[\\]n|([\\])?[\\]n/g, function (match, precededBacklash) { // precededBacklash not null when "\n" is preceded by a backlash ("\\n") // match will be "\\n" and we don't replace it with os.EOL return precededBacklash ? match : _os()["default"].EOL; }); } catch (e) { return util.inspect(val); } } }); } var BaseReporter = /*#__PURE__*/function () { function BaseReporter() { var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; (0, _classCallCheck2()["default"])(this, BaseReporter); (0, _defineProperty2()["default"])(this, "formatter", void 0); (0, _defineProperty2()["default"])(this, "language", void 0); (0, _defineProperty2()["default"])(this, "stdout", void 0); (0, _defineProperty2()["default"])(this, "stderr", void 0); (0, _defineProperty2()["default"])(this, "stdin", void 0); (0, _defineProperty2()["default"])(this, "isTTY", void 0); (0, _defineProperty2()["default"])(this, "emoji", void 0); (0, _defineProperty2()["default"])(this, "noProgress", void 0); (0, _defineProperty2()["default"])(this, "isVerbose", void 0); (0, _defineProperty2()["default"])(this, "isSilent", void 0); (0, _defineProperty2()["default"])(this, "nonInteractive", void 0); (0, _defineProperty2()["default"])(this, "format", void 0); (0, _defineProperty2()["default"])(this, "peakMemoryInterval", void 0); (0, _defineProperty2()["default"])(this, "peakMemory", void 0); (0, _defineProperty2()["default"])(this, "startTime", void 0); var lang = 'en'; this.language = lang; this.stdout = opts.stdout || process.stdout; this.stderr = opts.stderr || process.stderr; this.stdin = opts.stdin || this._getStandardInput(); this.emoji = !!opts.emoji; this.nonInteractive = !!opts.nonInteractive; this.noProgress = !!opts.noProgress || isCI; this.isVerbose = !!opts.verbose; // $FlowFixMe: this is valid! this.isTTY = this.stdout.isTTY; this.peakMemory = 0; this.startTime = Date.now(); this.format = _format().defaultFormatter; } (0, _createClass2()["default"])(BaseReporter, [{ key: "lang", value: function lang(key) { var msg = languages()[this.language][key] || languages().en[key]; if (!msg) { throw new ReferenceError("No message defined for language key ".concat(key)); } // stringify args for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var stringifiedArgs = stringifyLangArgs(args); // replace $0 placeholders with args return msg.replace(/\$(\d+)/g, function (str, i) { return stringifiedArgs[i]; }); } /** * `stringifyLangArgs` run `JSON.stringify` on strings too causing * them to appear quoted. This marks them as "raw" and prevents * the quoting and escaping */ }, { key: "rawText", value: function rawText(str) { return { inspect: function inspect() { return str; } }; } }, { key: "verbose", value: function verbose(msg) { if (this.isVerbose) { this._verbose(msg); } } }, { key: "verboseInspect", value: function verboseInspect(val) { if (this.isVerbose) { this._verboseInspect(val); } } }, { key: "_verbose", value: function _verbose(msg) {} }, { key: "_verboseInspect", value: function _verboseInspect(val) {} }, { key: "_getStandardInput", value: function _getStandardInput() { var standardInput; // Accessing stdin in a win32 headless process (e.g., Visual Studio) may throw an exception. try { standardInput = process.stdin; } catch (e) { console.warn(e.message); delete process.stdin; // $FlowFixMe: this is valid! process.stdin = new EventEmitter(); standardInput = process.stdin; } return standardInput; } }, { key: "initPeakMemoryCounter", value: function initPeakMemoryCounter() { var _this = this; this.checkPeakMemory(); this.peakMemoryInterval = setInterval(function () { _this.checkPeakMemory(); }, 1000); // $FlowFixMe: Node's setInterval returns a Timeout, not a Number this.peakMemoryInterval.unref(); } }, { key: "checkPeakMemory", value: function checkPeakMemory() { var _process$memoryUsage = process.memoryUsage(), heapTotal = _process$memoryUsage.heapTotal; if (heapTotal > this.peakMemory) { this.peakMemory = heapTotal; } } }, { key: "close", value: function close() { if (this.peakMemoryInterval) { clearInterval(this.peakMemoryInterval); this.peakMemoryInterval = null; } } }, { key: "getTotalTime", value: function getTotalTime() { return Date.now() - this.startTime; } // TODO }, { key: "list", value: function list(key, items, hints) {} // Outputs basic tree structure to console }, { key: "tree", value: function tree(key, obj) { var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, _ref$force = _ref.force, force = _ref$force === void 0 ? false : _ref$force; } // called whenever we begin a step in the CLI. }, { key: "step", value: function step(current, total, message, emoji) {} // a error message has been triggered. this however does not always meant an abrupt // program end. }, { key: "error", value: function error(message) {} // an info message has been triggered. this provides things like stats and diagnostics. }, { key: "info", value: function info(message) {} // a warning message has been triggered. }, { key: "warn", value: function warn(message) {} // a success message has been triggered. }, { key: "success", value: function success(message) {} // a simple log message // TODO: rethink the {force} parameter. In the meantime, please don't use it (cf comments in #4143). }, { key: "log", value: function log(message) { var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref2$force = _ref2.force, force = _ref2$force === void 0 ? false : _ref2$force; } // a shell command has been executed }, { key: "command", value: function command(_command) {} // inspect and pretty-print any value }, { key: "inspect", value: function inspect(value) {} // the screen shown at the very start of the CLI }, { key: "header", value: function header(command, pkg) {} // the screen shown at the very end of the CLI }, { key: "footer", value: function footer(showPeakMemory) {} // a table structure }, { key: "table", value: function table(head, body) {} // security audit action to resolve advisories }, { key: "auditAction", value: function auditAction(recommendation) {} // security audit requires manual review }, { key: "auditManualReview", value: function auditManualReview() {} // security audit advisory }, { key: "auditAdvisory", value: function auditAdvisory(resolution, _auditAdvisory) {} // summary for security audit report }, { key: "auditSummary", value: function auditSummary(auditMetadata) {} // render an activity spinner and return a function that will trigger an update }, { key: "activity", value: function activity() { return { tick: function tick(name) {}, end: function end() {} }; } // }, { key: "activitySet", value: function activitySet(total, workers) { return { spinners: Array(workers).fill({ clear: function clear() {}, setPrefix: function setPrefix() {}, tick: function tick() {}, end: function end() {} }), end: function end() {} }; } // }, { key: "question", value: function question(_question) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return Promise.reject(new Error('Not implemented')); } // }, { key: "questionAffirm", value: function () { var _questionAffirm = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee(question) { var condition, answer; return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: condition = true; // trick eslint if (!this.nonInteractive) { _context.next = 3; break; } return _context.abrupt("return", true); case 3: if (!condition) { _context.next = 15; break; } _context.next = 6; return this.question(question); case 6: answer = _context.sent; answer = answer.toLowerCase(); if (!(answer === 'y' || answer === 'yes')) { _context.next = 10; break; } return _context.abrupt("return", true); case 10: if (!(answer === 'n' || answer === 'no')) { _context.next = 12; break; } return _context.abrupt("return", false); case 12: this.error('Invalid answer for question'); _context.next = 3; break; case 15: return _context.abrupt("return", false); case 16: case "end": return _context.stop(); } }, _callee, this); })); function questionAffirm(_x) { return _questionAffirm.apply(this, arguments); } return questionAffirm; }() // prompt the user to select an option from an array }, { key: "select", value: function select(header, question, options) { return Promise.reject(new Error('Not implemented')); } // render a progress bar and return a function which when called will trigger an update }, { key: "progress", value: function progress(total) { return function () {}; } // utility function to disable progress bar }, { key: "disableProgress", value: function disableProgress() { this.noProgress = true; } // }, { key: "prompt", value: function prompt(message, choices) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return Promise.reject(new Error('Not implemented')); } }]); return BaseReporter; }(); exports["default"] = BaseReporter; //# sourceMappingURL=base-reporter.js.map