@aws-amplify/cli-internal
Version:
Amplify CLI
135 lines • 4.21 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.SpinningLogger = void 0;
const amplify_prompts_1 = require("@aws-amplify/amplify-prompts");
const chalk_1 = __importDefault(require("chalk"));
class SpinningLogger {
constructor(prefix, options) {
var _a;
this.prefix = prefix;
this.segments = [];
this.spinnerActive = false;
this.debugMode = (_a = options === null || options === void 0 ? void 0 : options.debug) !== null && _a !== void 0 ? _a : amplify_prompts_1.isDebug;
this.spinner = new amplify_prompts_1.AmplifySpinner();
process.on('SIGINT', () => {
if (this.spinnerActive) {
this.spinner.stop();
}
process.exit(130);
});
}
start(text) {
this.segments.length = 0;
this.segments.push(text);
if (this.debugMode) {
this.printLine(text, '→');
return;
}
this.spinnerActive = true;
this.spinner.start(this.buildSpinnerText());
}
stop() {
this.segments.length = 0;
if (!this.debugMode && this.spinnerActive) {
this.spinner.stop();
this.spinnerActive = false;
}
}
succeed(text) {
this.segments.length = 0;
if (this.debugMode) {
this.printLine(text, '•');
return;
}
if (this.spinnerActive) {
this.spinner.stop(text, true);
this.spinnerActive = false;
}
}
failed(text) {
this.segments.length = 0;
if (this.debugMode) {
this.printLine(text, '•');
return;
}
if (this.spinnerActive) {
this.spinner.stop(text, false);
this.spinnerActive = false;
}
}
push(text) {
this.segments.push(text);
if (this.debugMode) {
this.printLine(text, '→');
return;
}
if (this.spinnerActive) {
this.spinner.resetMessage(this.buildSpinnerText());
}
}
pop() {
this.segments.pop();
if (!this.debugMode && this.spinnerActive && this.segments.length > 0) {
this.spinner.resetMessage(this.buildSpinnerText());
}
}
info(message) {
this.withSpinnerPaused(() => this.printLine(message, '•'));
}
debug(message) {
if (this.debugMode) {
amplify_prompts_1.printer.debug(this.formatLine(message, '·'));
}
}
warn(message) {
if (this.debugMode) {
amplify_prompts_1.printer.warn(this.formatLine(message, '·'));
return;
}
this.withSpinnerPaused(() => amplify_prompts_1.printer.warn(this.formatLine(message, '·')));
}
withSpinnerPaused(fn) {
if (!this.spinnerActive || this.debugMode) {
fn();
return;
}
this.spinner.stop();
fn();
this.spinner.start(this.buildSpinnerText());
}
pause() {
if (this.spinnerActive && !this.debugMode) {
this.spinnerActive = false;
this.spinner.stop();
}
}
resume() {
if (!this.spinnerActive && !this.debugMode) {
this.spinner.start(this.buildSpinnerText());
}
}
async withSpinnerPausedAsync(fn) {
if (!this.spinnerActive || this.debugMode) {
await fn();
return;
}
this.spinner.stop();
await fn();
this.spinner.start(this.buildSpinnerText());
}
buildSpinnerText() {
return this.segments.join(SpinningLogger.SEPARATOR);
}
formatLine(message, bullet) {
return `[${new Date().toISOString()}] [${chalk_1.default.bold(this.prefix)}] ${bullet} ${message}`;
}
printLine(message, bullet) {
amplify_prompts_1.printer.info(this.formatLine(message, bullet));
}
}
exports.SpinningLogger = SpinningLogger;
SpinningLogger.SEPARATOR = ' → ';
//# sourceMappingURL=spinning-logger.js.map