UNPKG

3.63 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Spinners = require("cli-spinners");
4const logFigures = require("figures");
5const chalk_1 = require("chalk");
6const task_manager_1 = require("./task-manager");
7const wcwidth = require("wcwidth");
8const stripAnsi = require("strip-ansi");
9class CliLoading {
10 constructor(props) {
11 this._loadingFrameMap = new Map();
12 this.linesToClear = 0;
13 this.props = Object.assign({ indent: 3, symbolMap: {}, grayState: null, stream: process.stderr }, props);
14 }
15 count(uid) {
16 let count = this._loadingFrameMap.get(uid) || 0;
17 this._loadingFrameMap.set(uid, count + 1);
18 return count;
19 }
20 renderDepsTree(depsTree, output = []) {
21 let indent = Array(depsTree.depth * this.props.indent)
22 .fill(' ')
23 .join('');
24 let frames = Spinners.dots.frames;
25 let symbol = Object.assign({ [task_manager_1.TaskState.waiting]: chalk_1.default.gray(logFigures.ellipsis), [task_manager_1.TaskState.pending]: chalk_1.default.blueBright(logFigures.arrowRight), [task_manager_1.TaskState.loading]: chalk_1.default.cyan(frames[this.count(depsTree.uid) % frames.length]), [task_manager_1.TaskState.succeeded]: chalk_1.default.green(logFigures.tick), [task_manager_1.TaskState.failed]: chalk_1.default.red(logFigures.cross), [task_manager_1.TaskState.skipped]: chalk_1.default.yellow(logFigures.info) }, this.props.symbolMap)[depsTree.state];
26 let color = (this.props.grayState || [task_manager_1.TaskState.waiting]).indexOf(depsTree.state) >= 0
27 ? f => chalk_1.default.gray(f)
28 : f => f;
29 let skipped = depsTree.state === task_manager_1.TaskState.skipped;
30 output.push(`${indent}${symbol} ${color(depsTree.task.name)}${skipped ? chalk_1.default.gray(` [skipped]`) : ''}`);
31 for (const child of depsTree.asyncDeps.concat(depsTree.syncDeps)) {
32 this.renderDepsTree(child, output);
33 }
34 return output;
35 }
36 render() {
37 let columns = this.props.stream.columns || 80;
38 let rows = this.props.stream.rows || Infinity;
39 let output = this.renderDepsTree(this.props.depsTree);
40 let isFirstRendering = this.linesToClear === 0;
41 output.push('');
42 let outputLineCounts = output.map(line => Math.max(1, Math.ceil(wcwidth(stripAnsi(line)) / columns)));
43 this.clear();
44 this.linesToClear = 0;
45 for (let i = outputLineCounts.length - 1; i >= 0; i--) {
46 const count = outputLineCounts[i];
47 this.linesToClear += count;
48 if (this.linesToClear > rows) {
49 this.linesToClear -= count;
50 if (!isFirstRendering) {
51 output = output.slice(i + 1);
52 }
53 break;
54 }
55 }
56 this.props.stream.write(output.join('\n'));
57 }
58 clear() {
59 if (!this.props.stream.isTTY) {
60 return this;
61 }
62 for (let i = 0; i < this.linesToClear; i++) {
63 if (i > 0) {
64 this.props.stream.moveCursor(0, -1);
65 }
66 this.props.stream.clearLine();
67 this.props.stream.cursorTo(0);
68 }
69 this.linesToClear = 0;
70 return this;
71 }
72 start() {
73 if (this.id)
74 return;
75 this.id = setInterval(() => {
76 this.render();
77 }, 100);
78 }
79 stop() {
80 this.id && clearInterval(this.id);
81 this.id = undefined;
82 this.render();
83 }
84}
85exports.CliLoading = CliLoading;
86//# sourceMappingURL=cli-loading.js.map
\No newline at end of file