UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TTYOutputRedrawer = exports.TTYOutputStrategy = exports.StreamOutputStrategy = void 0;
4const utils_terminal_1 = require("@ionic/utils-terminal");
5const colors_1 = require("./colors");
6const tasks_1 = require("./tasks");
7const utils_1 = require("./utils");
8class StreamOutputStrategy {
9 constructor({ stream = process.stdout, colors = colors_1.NO_COLORS }) {
10 this.stream = stream;
11 this.colors = colors;
12 }
13 write(msg) {
14 return this.stream.write(msg);
15 }
16 createTaskChain() {
17 const { failure, success, weak } = this.colors;
18 const chain = new tasks_1.TaskChain();
19 chain.on('next', task => {
20 task.on('end', result => {
21 if (result.success) {
22 this.write(`${success(tasks_1.ICON_SUCCESS)} ${task.msg} ${weak(`in ${utils_1.formatHrTime(result.elapsedTime)}`)}\n`);
23 }
24 else {
25 this.write(`${failure(tasks_1.ICON_FAILURE)} ${task.msg} ${failure(weak('- failed!'))}\n`);
26 }
27 });
28 });
29 return chain;
30 }
31}
32exports.StreamOutputStrategy = StreamOutputStrategy;
33class TTYOutputStrategy extends StreamOutputStrategy {
34 constructor({ stream = process.stdout, colors = colors_1.NO_COLORS } = {}) {
35 super({ stream, colors });
36 this.stream = stream;
37 this.redrawer = new TTYOutputRedrawer({ stream });
38 }
39 createTaskChain() {
40 const { failure, strong, success, weak } = this.colors;
41 const chain = new tasks_1.TaskChain({ taskOptions: { tickInterval: 50 } });
42 chain.on('next', task => {
43 task.on('end', result => {
44 if (result.success) {
45 this.write(`${success(tasks_1.ICON_SUCCESS)} ${task.msg} ${weak(`in ${utils_1.formatHrTime(result.elapsedTime)}`)}\n`);
46 }
47 else {
48 this.write(`${failure(tasks_1.ICON_FAILURE)} ${task.msg} ${failure(weak('- failed!'))}\n`);
49 }
50 });
51 const spinner = new tasks_1.Spinner();
52 task.on('tick', () => {
53 const progress = task.progressRatio ? (task.progressRatio * 100).toFixed(2) : '';
54 const frame = spinner.frame();
55 this.redrawer.redraw(`${strong(frame)} ${task.msg}${progress ? ' (' + strong(String(progress) + '%') + ')' : ''} `);
56 });
57 task.on('clear', () => {
58 this.redrawer.clear();
59 });
60 });
61 chain.on('end', () => {
62 this.redrawer.end();
63 });
64 return chain;
65 }
66}
67exports.TTYOutputStrategy = TTYOutputStrategy;
68class TTYOutputRedrawer {
69 constructor({ stream = process.stdout }) {
70 this.stream = stream;
71 }
72 get width() {
73 return this.stream.columns || 80;
74 }
75 redraw(msg) {
76 utils_terminal_1.Cursor.hide();
77 this.stream.write(utils_terminal_1.EscapeCode.eraseLines(1) + msg.replace(/[\r\n]+$/, ''));
78 }
79 clear() {
80 this.stream.write(utils_terminal_1.EscapeCode.eraseLines(1));
81 }
82 end() {
83 utils_terminal_1.Cursor.show();
84 }
85}
86exports.TTYOutputRedrawer = TTYOutputRedrawer;