UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const logUpdate = require("log-update");
4const colors_1 = require("./colors");
5const tasks_1 = require("./tasks");
6const utils_1 = require("./utils");
7class StreamOutputStrategy {
8 constructor({ stream = process.stdout, colors = colors_1.NO_COLORS }) {
9 this.stream = stream;
10 this.colors = colors;
11 }
12 createTaskChain() {
13 const { failure, success, weak } = this.colors;
14 const chain = new tasks_1.TaskChain();
15 chain.on('next', task => {
16 task.on('end', result => {
17 if (result.success) {
18 this.stream.write(`${success(tasks_1.ICON_SUCCESS)} ${task.msg} ${weak(`in ${utils_1.formatHrTime(result.elapsedTime)}`)}`);
19 }
20 else {
21 this.stream.write(`${failure(tasks_1.ICON_FAILURE)} ${task.msg} ${failure(weak('- failed!'))}`);
22 }
23 });
24 });
25 return chain;
26 }
27}
28exports.StreamOutputStrategy = StreamOutputStrategy;
29class LogUpdateOutputStrategy {
30 constructor({ stream = process.stdout, colors = colors_1.NO_COLORS } = {}) {
31 this.stream = stream;
32 this.colors = colors;
33 this.logUpdate = logUpdate.create(stream);
34 }
35 redrawLine(msg = '') {
36 this.logUpdate(msg);
37 }
38 createTaskChain() {
39 const { failure, strong, success, weak } = this.colors;
40 const chain = new tasks_1.TaskChain({ taskOptions: { tickInterval: 50 } });
41 chain.on('next', task => {
42 task.on('end', result => {
43 if (result.success) {
44 this.stream.write(`${success(tasks_1.ICON_SUCCESS)} ${task.msg} ${weak(`in ${utils_1.formatHrTime(result.elapsedTime)}`)}\n`);
45 }
46 else {
47 this.stream.write(`${failure(tasks_1.ICON_FAILURE)} ${task.msg} ${failure(weak('- failed!'))}\n`);
48 }
49 });
50 const spinner = new tasks_1.Spinner();
51 task.on('tick', () => {
52 const progress = task.progressRatio ? (task.progressRatio * 100).toFixed(2) : '';
53 const frame = spinner.frame();
54 this.redrawLine(`${strong(frame)} ${task.msg}${progress ? ' (' + strong(String(progress) + '%') + ')' : ''} `);
55 });
56 task.on('clear', () => {
57 this.logUpdate.clear();
58 });
59 });
60 chain.on('end', () => {
61 this.logUpdate.done();
62 });
63 return chain;
64 }
65}
66exports.LogUpdateOutputStrategy = LogUpdateOutputStrategy;