1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const logUpdate = require("log-update");
|
4 | const colors_1 = require("./colors");
|
5 | const tasks_1 = require("./tasks");
|
6 | const utils_1 = require("./utils");
|
7 | class 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 | }
|
28 | exports.StreamOutputStrategy = StreamOutputStrategy;
|
29 | class 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 | }
|
66 | exports.LogUpdateOutputStrategy = LogUpdateOutputStrategy;
|