-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.js
88 lines (70 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const logUpdate = require('log-update');
const chalk = require('chalk');
const figures = require('figures');
const indentString = require('indent-string');
const cliTruncate = require('cli-truncate');
const stripAnsi = require('strip-ansi');
const utils = require('./lib/utils');
const renderHelper = (tasks, options, level) => {
level = level || 0;
let output = [];
for (const task of tasks) {
if (task.isEnabled()) {
const skipped = task.isSkipped() ? ` ${chalk.dim('[skipped]')}` : '';
output.push(indentString(` ${utils.getSymbol(task, options)} ${task.title}${skipped}`, level, ' '));
if ((task.isPending() || task.isSkipped() || task.hasFailed()) && utils.isDefined(task.output)) {
let data = task.output;
if (typeof data === 'string') {
data = stripAnsi(data.trim().split('\n').filter(Boolean).pop());
if (data === '') {
data = undefined;
}
}
if (utils.isDefined(data)) {
const out = indentString(`${figures.arrowRight} ${data}`, level, ' ');
output.push(` ${chalk.gray(cliTruncate(out, process.stdout.columns - 3))}`);
}
}
if ((task.isPending() || task.hasFailed() || options.collapse === false) && (task.hasFailed() || options.showSubtasks !== false) && task.subtasks.length > 0) {
output = output.concat(renderHelper(task.subtasks, options, level + 1));
}
}
}
return output.join('\n');
};
const render = (tasks, options) => {
logUpdate(renderHelper(tasks, options));
};
class UpdateRenderer {
constructor(tasks, options) {
this._tasks = tasks;
this._options = Object.assign({
showSubtasks: true,
collapse: true,
clearOutput: false
}, options);
}
render() {
if (this._id) {
// Do not render if we are already rendering
return;
}
this._id = setInterval(() => {
render(this._tasks, this._options);
}, 100);
}
end(err) {
if (this._id) {
clearInterval(this._id);
this._id = undefined;
}
render(this._tasks, this._options);
if (this._options.clearOutput && err === undefined) {
logUpdate.clear();
} else {
logUpdate.done();
}
}
}
module.exports = UpdateRenderer;