123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- 'use strict';
- const chalk = require('chalk');
- const cliCursor = require('cli-cursor');
- const cliSpinners = require('cli-spinners');
- const logSymbols = require('log-symbols');
- const stripAnsi = require('strip-ansi');
- const wcwidth = require('wcwidth');
- const TEXT = Symbol('text');
- const PREFIX_TEXT = Symbol('prefixText');
- class Ora {
- constructor(options) {
- if (typeof options === 'string') {
- options = {
- text: options
- };
- }
- this.options = Object.assign({
- text: '',
- color: 'cyan',
- stream: process.stderr
- }, options);
- this.spinner = this.options.spinner;
- this.color = this.options.color;
- this.hideCursor = this.options.hideCursor !== false;
- this.interval = this.options.interval || this.spinner.interval || 100;
- this.stream = this.options.stream;
- this.id = null;
- this.isEnabled = typeof this.options.isEnabled === 'boolean' ? this.options.isEnabled : ((this.stream && this.stream.isTTY) && !process.env.CI);
- // Set *after* `this.stream`
- this.text = this.options.text;
- this.prefixText = this.options.prefixText;
- this.linesToClear = 0;
- this.indent = this.options.indent;
- }
- get indent() {
- return this._indent;
- }
- set indent(indent = 0) {
- if (!(indent >= 0 && Number.isInteger(indent))) {
- throw new Error('The `indent` option must be an integer from 0 and up');
- }
- this._indent = indent;
- }
- get spinner() {
- return this._spinner;
- }
- set spinner(spinner) {
- this.frameIndex = 0;
- if (typeof spinner === 'object') {
- if (spinner.frames === undefined) {
- throw new Error('The given spinner must have a `frames` property');
- }
- this._spinner = spinner;
- } else if (process.platform === 'win32') {
- this._spinner = cliSpinners.line;
- } else if (spinner === undefined) {
- // Set default spinner
- this._spinner = cliSpinners.dots;
- } else if (cliSpinners[spinner]) {
- this._spinner = cliSpinners[spinner];
- } else {
- throw new Error(`There is no built-in spinner named '${spinner}'. See https://github.com/sindresorhus/cli-spinners/blob/master/spinners.json for a full list.`);
- }
- }
- get text() {
- return this[TEXT];
- }
- get prefixText() {
- return this[PREFIX_TEXT];
- }
- get isSpinning() {
- return this.id !== null;
- }
- updateLineCount() {
- const columns = this.stream.columns || 80;
- const fullPrefixText = (typeof this[PREFIX_TEXT] === 'string') ? this[PREFIX_TEXT] + '-' : '';
- this.lineCount = stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n').reduce((count, line) => {
- return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
- }, 0);
- }
- set text(value) {
- this[TEXT] = value;
- this.updateLineCount();
- }
- set prefixText(value) {
- this[PREFIX_TEXT] = value;
- this.updateLineCount();
- }
- frame() {
- const {frames} = this.spinner;
- let frame = frames[this.frameIndex];
- if (this.color) {
- frame = chalk[this.color](frame);
- }
- this.frameIndex = ++this.frameIndex % frames.length;
- const fullPrefixText = typeof this.prefixText === 'string' ? this.prefixText + ' ' : '';
- const fullText = typeof this.text === 'string' ? ' ' + this.text : '';
- return fullPrefixText + frame + fullText;
- }
- clear() {
- if (!this.isEnabled || !this.stream.isTTY) {
- return this;
- }
- for (let i = 0; i < this.linesToClear; i++) {
- if (i > 0) {
- this.stream.moveCursor(0, -1);
- }
- this.stream.clearLine();
- this.stream.cursorTo(this.indent);
- }
- this.linesToClear = 0;
- return this;
- }
- render() {
- this.clear();
- this.stream.write(this.frame());
- this.linesToClear = this.lineCount;
- return this;
- }
- start(text) {
- if (text) {
- this.text = text;
- }
- if (!this.isEnabled) {
- this.stream.write(`- ${this.text}\n`);
- return this;
- }
- if (this.isSpinning) {
- return this;
- }
- if (this.hideCursor) {
- cliCursor.hide(this.stream);
- }
- this.render();
- this.id = setInterval(this.render.bind(this), this.interval);
- return this;
- }
- stop() {
- if (!this.isEnabled) {
- return this;
- }
- clearInterval(this.id);
- this.id = null;
- this.frameIndex = 0;
- this.clear();
- if (this.hideCursor) {
- cliCursor.show(this.stream);
- }
- return this;
- }
- succeed(text) {
- return this.stopAndPersist({symbol: logSymbols.success, text});
- }
- fail(text) {
- return this.stopAndPersist({symbol: logSymbols.error, text});
- }
- warn(text) {
- return this.stopAndPersist({symbol: logSymbols.warning, text});
- }
- info(text) {
- return this.stopAndPersist({symbol: logSymbols.info, text});
- }
- stopAndPersist(options = {}) {
- const prefixText = options.prefixText || this.prefixText;
- const fullPrefixText = (typeof prefixText === 'string') ? prefixText + ' ' : '';
- const text = options.text || this.text;
- const fullText = (typeof text === 'string') ? ' ' + text : '';
- this.stop();
- this.stream.write(`${fullPrefixText}${options.symbol || ' '}${fullText}\n`);
- return this;
- }
- }
- const oraFactory = function (opts) {
- return new Ora(opts);
- };
- module.exports = oraFactory;
- // TODO: Remove this for the next major release
- module.exports.default = oraFactory;
- module.exports.promise = (action, options) => {
- if (typeof action.then !== 'function') {
- throw new TypeError('Parameter `action` must be a Promise');
- }
- const spinner = new Ora(options);
- spinner.start();
- action.then(
- () => {
- spinner.succeed();
- },
- () => {
- spinner.fail();
- }
- );
- return spinner;
- };
|