forked from sindresorhus/time-grunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (157 loc) · 4.76 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
var chalk = require('chalk');
var table = require('text-table');
var hooker = require('hooker');
var dateTime = require('date-time');
var prettyMs = require('pretty-ms');
var numberIsNan = require('number-is-nan');
var argv = process.argv.slice(2);
var write = process.stdout.write.bind(process.stdout);
function log(str) {
write(str + '\n', 'utf8');
}
module.exports = function (grunt, cb) {
var barChar = process.platform === 'win32' ? '■' : '▇';
var now = new Date();
var startTimePretty = dateTime();
var startTime = now.getTime();
var prevTime = startTime;
var prevTaskName = 'loading tasks';
var tableData = [];
var styleOptions = grunt.config('time')
? (grunt.config('time').options || {})
: {};
var defaultDisplayStyle = {
'header': chalk.yellow.bold,
'task': chalk.blue,
'bar': chalk.blue,
'footer': chalk.green.bold
};
if (argv.indexOf('--help') !== -1 ||
argv.indexOf('-h') !== -1 ||
// for `quiet-grunt`
argv.indexOf('--quiet') !== -1 ||
argv.indexOf('-q') !== -1 ||
argv.indexOf('--version') !== -1 ||
argv.indexOf('-V') !== -1) {
return;
}
// crazy hack to work around stupid node-exit
// Can this be removed now that node-exit#4 has been resolved?
// https://github.com/cowboy/node-exit/issues/4
var originalExit = process.exit;
function exit(exitCode) {
clearInterval(interval);
process.emit('timegruntexit', exitCode);
exit = function () {};
}
var interval = setInterval(function () {
process.exit = exit;
}, 100);
process.exit = exit;
hooker.hook(grunt.log, 'header', function () {
var name = grunt.task.current.nameArgs;
var diff = Date.now() - prevTime;
if (prevTaskName && prevTaskName !== name) {
tableData.push([prevTaskName, diff]);
}
prevTime = Date.now();
prevTaskName = name;
});
function formatInputStyle(section) {
//avoid use of 'slice' as it prevents V8 customizations
var args = new Array(arguments.length);
for(var i = 1; i < arguments.length; i++) {
args[i] = arguments[i];
}
if(Object.keys(styleOptions).indexOf(section) === -1) {
return defaultDisplayStyle[section](args.join(''));
}
var parsedStyle = Object.keys(chalk.styles).filter(function(style) {
return (style === styleOptions[section]);
});
return parsedStyle ?
chalk[parsedStyle.join('')](args.join(''))
: defaultDisplayStyle[section](args.join(''));
}
function formatTable(tableData) {
var totalTime = Date.now() - startTime;
var longestTaskName = tableData.reduce(function (acc, row) {
var avg = row[1] / totalTime;
if (avg < 0.01 && !grunt.option('verbose')) {
return acc;
}
return Math.max(acc, row[0].length);
}, 0);
var maxColumns = process.stdout.columns || 80;
var maxBarWidth;
if (longestTaskName > maxColumns / 2) {
maxBarWidth = (maxColumns - 20) / 2;
} else {
maxBarWidth = maxColumns - (longestTaskName + 20);
}
function shorten(taskName) {
var nameLength = taskName.length;
if (nameLength <= maxBarWidth) {
return taskName;
}
var partLength = Math.floor((maxBarWidth - 3) / 2);
var start = taskName.substr(0, partLength + 1);
var end = taskName.substr(nameLength - partLength);
return start.trim() + '...' + end.trim();
}
function createBar(percentage) {
var rounded = Math.round(percentage * 100);
if (rounded === 0) {
return '0%';
}
var barLength = Math.ceil(maxBarWidth * percentage) + 1;
var bar = new Array(barLength).join(barChar);
return bar + ' ' + rounded + '%';
}
var tableDataProcessed = tableData.map(function (row) {
var avg = row[1] / totalTime;
if (numberIsNan(avg) || (avg < 0.01 && !grunt.option('verbose'))) {
return;
}
return [formatInputStyle('task', shorten(row[0])),
formatInputStyle('bar', prettyMs(row[1])),
formatInputStyle('bar', createBar(avg))];
}).reduce(function (acc, row) {
if (row) {
acc.push(row);
return acc;
}
return acc;
}, []);
tableDataProcessed.push([formatInputStyle('footer', 'Total : ', prettyMs(totalTime))]);
return table(tableDataProcessed, {
align: ['l', 'r', 'l'],
stringLength: function (str) {
return chalk.stripColor(str).length;
}
});
}
process.on('SIGINT', function () {
process.exit();
});
process.once('timegruntexit', function (exitCode) {
clearInterval(interval);
process.exit = originalExit;
hooker.unhook(grunt.log, 'header');
var diff = Date.now() - prevTime;
if (prevTaskName) {
tableData.push([prevTaskName, diff]);
}
// `grunt.log.header` should be unhooked above, but in some cases it's not
log('\n\n' + formatInputStyle('header', 'Execution Time ( ', startTimePretty, ' )'));
log(formatTable(tableData) + '\n');
if (cb) {
cb(tableData, function () {
process.exit(exitCode);
});
return;
}
process.exit(exitCode);
});
};