npmtest-auto-install (v0.0.1)

Code coverage report for node-npmtest-auto-install/auto-install/node_modules/ora/index.js

Statements: 41.51% (22 / 53)      Branches: 51.72% (15 / 29)      Functions: 40% (4 / 10)      Lines: 41.51% (22 / 53)      Ignored: none     

All files » node-npmtest-auto-install/auto-install/node_modules/ora/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106  1 1 1 1       85           85           85 85   85       85 85 85 85 85 85 85                                                             85 85                   85 85                                                 1 85      
'use strict';
const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const cliSpinners = require('cli-spinners');
const logSymbols = require('log-symbols');
 
class Ora {
	constructor(options) {
		Iif (typeof options === 'string') {
			options = {
				text: options
			};
		}
 
		this.options = Object.assign({
			text: '',
			color: 'cyan',
			stream: process.stderr
		}, options);
 
		const sp = this.options.spinner;
		this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary
 
		Iif (this.spinner.frames === undefined) {
			throw new Error('Spinner must define `frames`');
		}
 
		this.text = this.options.text;
		this.color = this.options.color;
		this.interval = this.options.interval || this.spinner.interval || 100;
		this.stream = this.options.stream;
		this.id = null;
		this.frameIndex = 0;
		this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI);
	}
	frame() {
		const frames = this.spinner.frames;
		let frame = frames[this.frameIndex];
 
		if (this.color) {
			frame = chalk[this.color](frame);
		}
 
		this.frameIndex = ++this.frameIndex % frames.length;
 
		return frame + ' ' + this.text;
	}
	clear() {
		if (!this.enabled) {
			return this;
		}
 
		this.stream.clearLine();
		this.stream.cursorTo(0);
 
		return this;
	}
	render() {
		this.clear();
		this.stream.write(this.frame());
 
		return this;
	}
	start() {
		Eif (!this.enabled || this.id) {
			return this;
		}
 
		cliCursor.hide();
		this.render();
		this.id = setInterval(this.render.bind(this), this.interval);
 
		return this;
	}
	stop() {
		Eif (!this.enabled) {
			return this;
		}
 
		clearInterval(this.id);
		this.id = null;
		this.frameIndex = 0;
		this.clear();
		cliCursor.show();
 
		return this;
	}
	succeed() {
		return this.stopAndPersist(logSymbols.success);
	}
	fail() {
		return this.stopAndPersist(logSymbols.error);
	}
	stopAndPersist(symbol) {
		this.stop();
		this.stream.write(`${symbol || ' '} ${this.text}\n`);
 
		return this;
	}
}
 
module.exports = function (opts) {
	return new Ora(opts);
};