UNPKG

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