@autorest/common
Version:
Autorest common utilities
141 lines • 4.88 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleLogger = exports.ConsoleLoggerSink = void 0;
const cli_progress_1 = __importDefault(require("cli-progress"));
const formatter_1 = require("./formatter");
const logger_1 = require("./logger");
const ProgressBarUnavailable = Symbol("NoBar");
/**
* Logger sink to output logs to the console.
*/
class ConsoleLoggerSink {
constructor(options = {}) {
var _a, _b;
this.options = options;
this.bars = [];
this.pendingLogs = [];
this.stream = (_a = options.stream) !== null && _a !== void 0 ? _a : process.stdout;
this.format = (_b = options.format) !== null && _b !== void 0 ? _b : "regular";
this.formatter = (0, formatter_1.createLogFormatter)(options.format, options);
}
log(log) {
const line = this.formatter.log(log);
if (this.currentProgressBar) {
this.pendingLogs.push(line);
}
else {
this.writeLine(line);
}
}
startProgress(initialName) {
if (this.format === "regular") {
return this.startProgressBar(initialName);
}
else {
return NoopProgress;
}
}
isTTY() {
return this.stream.isTTY;
}
startProgressBar(initialName) {
// If in a non TTY stream do not even start the progress bar.
if (!this.isTTY() && !this.options.progressNoTTYOutput) {
return {
update: (progress) => { },
stop: () => { },
};
}
if (this.currentProgressBar === undefined) {
this.currentProgressBar = new cli_progress_1.default.MultiBar({
hideCursor: true,
stream: this.stream,
noTTYOutput: this.options.progressNoTTYOutput,
format: "{name} [{bar}] {percentage}% | {value}/{total}",
forceRedraw: true, // without this the bar is flickering,
}, cli_progress_1.default.Presets.legacy);
}
const multiBar = this.currentProgressBar;
multiBar.on("redraw-pre", () => {
if (this.pendingLogs.length > 0) {
if ("clearLine" in this.stream) {
this.stream.clearLine(1);
}
}
while (this.pendingLogs.length > 0) {
this.writeLine(this.pendingLogs.shift());
}
});
multiBar.on("stop", () => {
this.currentProgressBar = undefined;
while (this.pendingLogs.length > 0) {
this.writeLine(this.pendingLogs.shift());
}
});
let bar;
const update = (progress) => {
var _a, _b;
if (bar === ProgressBarUnavailable) {
return;
}
const name = (_b = (_a = progress.name) !== null && _a !== void 0 ? _a : initialName) !== null && _b !== void 0 ? _b : "progress";
if (bar === undefined) {
const createdBar = multiBar.create(progress.total, 0, { name });
// Can return undefined if the stream is not TTY.
if (createdBar === undefined) {
bar = ProgressBarUnavailable;
return;
}
else {
bar = createdBar;
this.bars.push(bar);
}
}
else {
bar.setTotal(progress.total);
}
bar.update(progress.current, { name });
};
const stop = () => {
if (bar && bar !== ProgressBarUnavailable) {
bar.update(bar.getTotal());
bar.render();
bar.stop();
multiBar.remove(bar);
this.bars = this.bars.filter((x) => x !== bar);
}
if (this.bars.length === 0) {
multiBar.stop();
this.currentProgressBar = undefined;
}
};
return {
update,
stop,
};
}
writeLine(line) {
if (line !== undefined) {
this.stream.write(Buffer.from(`${line}\n`));
}
}
}
exports.ConsoleLoggerSink = ConsoleLoggerSink;
/**
* Simple logger which takes log info as it is and logs it.
* Doesn't resolve original source locations.
*/
class ConsoleLogger extends logger_1.AutorestSyncLogger {
constructor(options = {}) {
super({ sinks: [new ConsoleLoggerSink(options)] });
}
}
exports.ConsoleLogger = ConsoleLogger;
const NoopProgress = {
update: () => null,
stop: () => null,
};
//# sourceMappingURL=console-logger-sink.js.map