2024-10-17 21:31:23 +03:00

218 lines
6.3 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Spinner = void 0;
const spinnies_1 = __importDefault(require("@trufflesuite/spinnies"));
const spinnies = new spinnies_1.default();
class Spinner {
constructor(name, optionsOrText) {
this.name = name;
const options = typeof optionsOrText === "string"
? { text: optionsOrText }
: optionsOrText;
if (options.text === undefined) {
throw new Error("Spinner text cannot be undefined");
}
spinnies.add(this.name, options);
}
/**
* Stops all spinners
* @param status the terminal state of the spinners that have been stopped (one of "stopped", "succeed", or "fail").
*/
static stopAll(status) {
spinnies.stopAll(status);
}
/**
* @returns false if all spinners have succeeded, failed or have been stopped
*/
static hasActiveSpinners() {
return spinnies.hasActiveSpinners();
}
/**
* Removes this spinner from display. After this method is called, other modifications to this spinner object have no effect.
*/
remove() {
if (!spinnies.pick(this.name)) {
return;
}
spinnies.remove(this.name);
}
stop(textOrOptions) {
if (!spinnies.pick(this.name)) {
return;
}
spinnies.stop(this.name, textOrOptions);
}
succeed(textOrOptions) {
if (!spinnies.pick(this.name)) {
return;
}
spinnies.succeed(this.name, textOrOptions);
}
fail(textOrOptions) {
if (!spinnies.pick(this.name)) {
return;
}
spinnies.fail(this.name, textOrOptions);
}
warn(textOrOptions) {
if (!spinnies.pick(this.name)) {
return;
}
spinnies.warn(this.name, textOrOptions);
}
/**
* @returns string the spinner prefix used on success
*/
static get succeedPrefix() {
return spinnies.options.succeedPrefix;
}
/**
* Updates the spinner success decoration. Updates apply to all spinning
* spinners, and do not apply to spinners that have already stopped.
*/
static set succeedPrefix(value) {
spinnies.options.succeedPrefix = value;
}
/**
* @returns string the spinner prefix used on failure
*/
static get failPrefix() {
return spinnies.options.failPrefix;
}
/**
* Updates the spinner fail decoration. Updates apply to all spinning
* spinners, and do not apply to spinners that have already stopped.
*/
static set failPrefix(value) {
spinnies.options.failPrefix = value;
}
/**
* @returns string the text currently displayed by this spinner, without the
* spinner decoration
*/
get text() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.text;
}
/**
* Updates the text displayed by this spinner
*/
set text(value) {
this._mutateOptions("text", value);
}
/**
* @returns the indent level of this spinner, expressed as a number of spaces
*/
get indent() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.indent;
}
/**
* Sets the indent level of this spinner, expressed as a number of spaces
*/
set indent(value) {
this._mutateOptions("indent", value);
}
/**
* @returns string the `chalk` color of this spinner's text
*/
get textColor() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.textColor;
}
/**
* updates the `chalk` color of this spinner's text
*/
set textColor(value) {
this._mutateOptions("textColor", value);
}
/**
* @returns string the `chalk` color of this spinner decoration
*/
get prefixColor() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.prefixColor;
}
/**
* updates the `chalk` color of this spinner's decoration
*/
set prefixColor(value) {
this._mutateOptions("prefixColor", value);
}
/**
* @returns string the prefix used when this spinner is stopped
*/
get stoppedPrefix() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.stoppedPrefix;
}
/**
* updates the prefix used when this spinner is stopped
*/
set stoppedPrefix(value) {
this._mutateOptions("stoppedPrefix", value);
}
/**
* @returns string the prefix used on success
*/
get succeedPrefix() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.succeedPrefix;
}
/**
* updates the prefix used on success
*/
set succeedPrefix(value) {
this._mutateOptions("succeedPrefix", value);
}
/**
* @returns string the prefix used on failure
*/
get failPrefix() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.failPrefix;
}
/**
* updates the prefix used on failure
*/
set failPrefix(value) {
this._mutateOptions("failPrefix", value);
}
/**
* @returns string the prefix used on warn
*/
get warnPrefix() {
var _a;
return (_a = spinnies.pick(this.name)) === null || _a === void 0 ? void 0 : _a.warnPrefix;
}
/**
* updates the prefix used on warn
*/
set warnPrefix(value) {
this._mutateOptions("warnPrefix", value);
}
/**
* @returns boolean `true` when the spinner is active and spinning, otherwise
* `false`
*/
get isSpinning() {
const options = spinnies.pick(this.name);
if (!options) {
return false;
}
return options.status === "spinning";
}
_mutateOptions(key, value) {
const options = spinnies.pick(this.name);
if (!options) {
return;
}
options[key] = value;
spinnies.update(this.name, options);
}
}
exports.Spinner = Spinner;
//# sourceMappingURL=spinner.js.map