UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const events_1 = require("events");
4const isWindows = process.platform === 'win32';
5exports.ICON_SUCCESS = isWindows ? '√' : '✔';
6exports.ICON_FAILURE = isWindows ? '×' : '✖';
7const SPINNER_FRAMES = isWindows ?
8 ['-', '\\', '|', '/'] :
9 ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
10class Spinner {
11 constructor(frames = SPINNER_FRAMES) {
12 this.frames = frames;
13 this.i = 0;
14 }
15 frame() {
16 return this.frames[this.i = ++this.i % this.frames.length];
17 }
18}
19exports.Spinner = Spinner;
20class Task extends events_1.EventEmitter {
21 constructor({ msg = '', tickInterval } = {}) {
22 super();
23 this.running = false;
24 this._msg = '';
25 this._success = false;
26 this.msg = msg;
27 this.tickInterval = tickInterval;
28 }
29 get msg() {
30 return this._msg;
31 }
32 set msg(msg) {
33 this._msg = msg;
34 this.tick();
35 }
36 start() {
37 if (!this.running && this.tickInterval) {
38 this.intervalId = setInterval(() => { this.tick(); }, this.tickInterval);
39 }
40 this.running = true;
41 this._startTime = process.hrtime();
42 return this;
43 }
44 tick() {
45 this.emit('tick');
46 return this;
47 }
48 progress(prog, total) {
49 this.progressRatio = prog / total;
50 this.tick();
51 return this;
52 }
53 clear() {
54 if (this.intervalId) {
55 clearInterval(this.intervalId);
56 this.intervalId = undefined;
57 }
58 this.emit('clear');
59 return this;
60 }
61 end() {
62 this.running = false;
63 this.tick();
64 this.clear();
65 this.emit('end', {
66 elapsedTime: process.hrtime(this._startTime),
67 success: this._success,
68 });
69 return this;
70 }
71 succeed() {
72 if (this.running) {
73 this._success = true;
74 this.end();
75 this.emit('success');
76 }
77 return this;
78 }
79 fail() {
80 if (this.running) {
81 this._success = false;
82 this.end();
83 this.emit('failure');
84 }
85 return this;
86 }
87}
88exports.Task = Task;
89class TaskChain extends events_1.EventEmitter {
90 constructor({ taskOptions = {} } = {}) {
91 super();
92 this.tasks = [];
93 this.taskOptions = taskOptions;
94 }
95 next(msg) {
96 return this.nextTask(this.createTask({ msg, ...this.taskOptions }));
97 }
98 createTask(options) {
99 return new Task(options);
100 }
101 nextTask(task) {
102 if (this.current) {
103 this.current.succeed();
104 }
105 this.tasks.push(task);
106 this.current = task;
107 task.start();
108 this.emit('next', task);
109 return task;
110 }
111 end() {
112 const task = this.current;
113 if (task) {
114 task.succeed();
115 }
116 this.current = undefined;
117 this.emit('end', task);
118 return this;
119 }
120 fail() {
121 const task = this.current;
122 if (task) {
123 task.fail();
124 }
125 this.emit('failure', task);
126 return this;
127 }
128 cleanup() {
129 for (const task of this.tasks) {
130 if (task.running) {
131 task.fail();
132 }
133 else {
134 task.clear();
135 }
136 }
137 return this;
138 }
139}
140exports.TaskChain = TaskChain;