-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (104 loc) · 3.05 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var cursor = require('cli-cursor');
var extend = require('extend');
var pipeline = require('progress-pipeline');
var Charm = require('charm');
var chalk = require('chalk');
function template(ctx) {
var name = ctx.name || '#' + (ctx._index + 1);
name = chalk[ctx.color || 'blue'](name);
if (ctx._error) {
return chalk.red('\u2717') +
' ' + name + ': '+ ctx._job.title +
chalk.red(' failed with error: ' +
ctx._error.message);
}
if ( // the last job has finished
ctx._jobFinished &&
ctx._jobIndex === ctx._totalJobs - 1
) {
return chalk.green('\u2713') + ' ' + name;
}
return ' ' + name + ': ' +
(ctx._jobIndex+1) + '/' + ctx._totalJobs + ' ' +
ctx._job.title || 'job #' + ctx._jobIndex;
}
module.exports = function board(options) {
var pipelines = [];
var currLine;
var pending = 0;
options = options || {};
var charm = Charm();
cursor.hide();
function update(id, p, ctx) {
var t = p.options.template || options.template || template;
var line = t(ctx);
charm
.column(0)
.move(0, id - currLine)
.write(line)
.erase('end');
currLine = id;
}
function pipeEnds() {
if (--pending === 0) {
charm
.move(0, pipelines.length - currLine)
.column(0)
.destroy();
}
}
function makeHandlers(p, id) {
function contextFromEvent(ev) {
return {
_index: id,
_job: ev.job,
_jobFinished: ev.jobFinished,
_jobIndex: ev.jobIndex,
_totalJobs: ev.totalJobs
};
}
p.on('error', function(error) {
var ctx = extend(p.options.context, contextFromEvent(error));
ctx._jobResult = undefined;
ctx._error = error;
ctx._jobFinished = false;
update(id, p, ctx);
pipeEnds();
});
p.on('data', function(data) {
var ctx = extend(p.options.context, contextFromEvent(data));
ctx._jobResult = data.result;
ctx._error = undefined;
update(id, p, ctx);
if (data.jobFinished) {
if (data.jobIndex + 1 === data.totalJobs) {
pipeEnds();
}
}
});
}
charm.on('end', function() {
cursor.show();
});
charm.add = function(p, options) {
if (p.length && typeof p[0] === 'function') {
p = pipeline(p);
}
if (typeof options === 'string') {
options = {
context: {
name: options
}
};
}
p.options = options || {};
p.options.context = p.options.context || {};
var id = pipelines.push(p) - 1;
console.log(p.name);
makeHandlers(p, id);
currLine = id+1;
pending++;
return charm;
};
return charm;
};