1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const logUpdate = require("log-update");
|
4 | const chalk = require("chalk");
|
5 | const indentString = require("indent-string");
|
6 | const Utils_1 = require("./Utils");
|
7 | const intdentText = (text, level, indentText = ' ', color, firstLinePrefix) => {
|
8 | if (typeof text !== 'string') {
|
9 | text = 'Message can not be displayed!';
|
10 | }
|
11 | const indentLength = level * indentString.length;
|
12 | const maxLength = 120;
|
13 | const maxTextLength = maxLength - indentLength;
|
14 | let textLeft = text;
|
15 | let tempText = '';
|
16 | const returnArray = [];
|
17 | let lastSpacePosition;
|
18 | let fullIdentText;
|
19 | let itteration = 0;
|
20 | do {
|
21 | tempText = textLeft.substring(0, maxTextLength);
|
22 | textLeft = textLeft.substring(maxTextLength);
|
23 | if (textLeft && tempText.charAt(maxTextLength - 1) !== ' ' && textLeft.charAt(0) !== ' ') {
|
24 | lastSpacePosition = tempText.lastIndexOf(' ');
|
25 | if (lastSpacePosition !== -1) {
|
26 | textLeft = (tempText.substring(lastSpacePosition) + textLeft).trim();
|
27 | tempText = tempText.substring(0, lastSpacePosition).trim();
|
28 | }
|
29 | }
|
30 | else {
|
31 | tempText = tempText.trim();
|
32 | textLeft = textLeft.trim();
|
33 | }
|
34 | fullIdentText = indentText.repeat(level + 1);
|
35 | if (firstLinePrefix) {
|
36 | if (itteration === 0) {
|
37 | fullIdentText = fullIdentText + firstLinePrefix;
|
38 | }
|
39 | else {
|
40 | fullIdentText = fullIdentText + ' '.repeat(firstLinePrefix.length);
|
41 | }
|
42 | }
|
43 | returnArray.push(`${chalk[color](fullIdentText + tempText)}`);
|
44 | itteration++;
|
45 | } while (textLeft);
|
46 | return returnArray;
|
47 | };
|
48 | const renderHelper = (tasks, options, level) => {
|
49 | level = level || 1;
|
50 | let output = [];
|
51 | for (const task of tasks) {
|
52 | if (task.isEnabled()) {
|
53 | if (options.separateTopTasks === true && level === 1) {
|
54 | output.push('');
|
55 | }
|
56 | output.push(indentString(` ${Utils_1.getSymbol(task, options)} ${task.title}`, level, ' '));
|
57 | if ((task.isPending() || task.isSkipped() || task.hasFailed()) &&
|
58 | Utils_1.isDefined(task.output)) {
|
59 | const data = task.output;
|
60 | if (Utils_1.isDefined(data)) {
|
61 | if (data.substr(0, 5) === 'Info:') {
|
62 | const arr = data.substr(6).split('\n');
|
63 | for (const item of arr) {
|
64 | output.push.apply(output, intdentText(item, level, ' ', 'grey', ' -> '));
|
65 | }
|
66 | }
|
67 | else if (data.substr(0, 6) === 'Error:') {
|
68 | const arr = data.substr(7).split('\n');
|
69 | for (const item of arr) {
|
70 | output.push.apply(output, intdentText(item, level, ' ', 'red', ' '));
|
71 | }
|
72 | }
|
73 | else {
|
74 | output.push.apply(output, intdentText(data, level, ' ', 'grey', ' -> '));
|
75 | }
|
76 | }
|
77 | }
|
78 | if ((task.isPending() || task.hasFailed() || options.collapse === false) &&
|
79 | (task.hasFailed() || options.showSubtasks !== false) &&
|
80 | task.subtasks.length > 0) {
|
81 | output = output.concat(renderHelper(task.subtasks, options, level + 1));
|
82 | }
|
83 | }
|
84 | }
|
85 | return output.join('\n');
|
86 | };
|
87 | const render = (tasks, options) => {
|
88 | logUpdate(renderHelper(tasks, options));
|
89 | };
|
90 | class JovoCliRenderer {
|
91 | constructor(tasks = [], options = {}) {
|
92 | this.nonTTY = false;
|
93 | this._tasks = tasks;
|
94 | this._options = Object.assign({
|
95 | showSubtasks: true,
|
96 | collapse: true,
|
97 | clearOutput: false,
|
98 | separateTopTasks: false,
|
99 | }, options);
|
100 | }
|
101 | render() {
|
102 | if (this._id) {
|
103 | return;
|
104 | }
|
105 | this._id = setInterval(() => {
|
106 | render(this._tasks, this._options);
|
107 | }, 100);
|
108 | }
|
109 | end(err) {
|
110 | if (this._id) {
|
111 | clearInterval(this._id);
|
112 | this._id = undefined;
|
113 | }
|
114 | render(this._tasks, this._options);
|
115 | if (this._options.clearOutput && err === undefined) {
|
116 | logUpdate.clear();
|
117 | }
|
118 | else {
|
119 | logUpdate.done();
|
120 | }
|
121 | }
|
122 | }
|
123 | exports.JovoCliRenderer = JovoCliRenderer;
|
124 |
|
\ | No newline at end of file |