UNPKG

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