forked from sindresorhus/time-grunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (123 loc) · 3.73 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
'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 barChar = require('figures').square;
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) {
var now = new Date();
var startTimePretty = dateTime();
var startTime = now.getTime();
var prevTime = startTime;
var prevTaskName = 'loading tasks';
var tableData = [];
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 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 (isNaN(avg) || (avg < 0.01 && !grunt.option('verbose'))) {
return;
}
return [shorten(row[0]), chalk.blue(prettyMs(row[1])), chalk.blue(createBar(avg))];
}).reduce(function (acc, row) {
if (row) {
acc.push(row);
return acc;
}
return acc;
}, []);
tableDataProcessed.push([chalk.magenta('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' + chalk.underline('Execution Time') + chalk.gray(' (' + startTimePretty + ')'));
log(formatTable(tableData) + '\n');
process.exit(exitCode);
});
};