UNPKG

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