Skip to content

Commit

Permalink
Merge pull request #8 from blaffoy/custom-reporter-parameter
Browse files Browse the repository at this point in the history
Add support for a custom reporter function, defaulting to console.log
  • Loading branch information
simmo authored Nov 14, 2016
2 parents 2b95622 + 4cb85e1 commit 07ab1c3
Showing 1 changed file with 66 additions and 60 deletions.
126 changes: 66 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,70 @@ var chalk = require('chalk'),
prettyTime = require('pretty-hrtime'),
table = require('text-table');

module.exports = function(gulp) {
function formatTable(data) {
var timeDiff = data.totalTime[0] + (data.totalTime[1] / 1e9); // 1 times 10 to the power of 9

function ordinalSuffixOf(i) {
var j = i % 10,
k = i % 100;

if (j === 1 && k !== 11) {
return i + 'st';
}
if (j === 2 && k !== 12) {
return i + 'nd';
}
if (j === 3 && k !== 13) {
return i + 'rd';
}
return i + 'th';
}

// Sort table by duration
data.stats.sort(function(a, b) {
if (a.durationFloat === b.durationFloat) {
return 0;
}
return (a.durationFloat > b.durationFloat) ? -1 : 1;
});

var outHead = ['Task', 'Time', '% of total', 'Finished'].map(function(heading) {
return chalk.bold.underline(heading);
});

var outList = data.stats.map(function(entry) {
return [
entry.task,
chalk.cyan(entry.duration),
chalk.magenta(Math.round((entry.durationFloat / timeDiff) * 100) + '%'),
ordinalSuffixOf(entry.finishIndex + 1),
];
});

var outTable = [outHead].concat(outList);

var tableOpts = {
align: ['l', 'r', 'r', 'r'],
stringLength: function(str) {
var r = new RegExp('\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)', 'g');
return str.replace(r, '').length;
}
};

return table(outTable, tableOpts);
}

function defaultReporter(data) {
// Overview
console.log('\nGulp ran', chalk.cyan.bold(data.stats.length + ' task' + (data.stats.length === 1 ? '' : 's')), 'in', chalk.cyan.bold(prettyTime(data.totalTime)));

// Display table
console.log('\n' + formatTable(data) + '\n');
}

module.exports = function(gulp, reporter) {

reporter = typeof reporter !== 'undefined' ? reporter : defaultReporter;

var tableData, startTime;

Expand Down Expand Up @@ -36,66 +99,9 @@ module.exports = function(gulp) {
});

gulp.on('stop', function () {
var totalTime = process.hrtime(startTime),
timeDiff = totalTime[0] + (totalTime[1] / 1e9); // 1 times 10 to the power of 9

// Format table
function formatTable(data) {
function ordinalSuffixOf(i) {
var j = i % 10,
k = i % 100;

if (j === 1 && k !== 11) {
return i + 'st';
}
if (j === 2 && k !== 12) {
return i + 'nd';
}
if (j === 3 && k !== 13) {
return i + 'rd';
}
return i + 'th';
}

// Sort table by duration
data.sort(function(a, b) {
if (a.durationFloat === b.durationFloat) {
return 0;
}
return (a.durationFloat > b.durationFloat) ? -1 : 1;
});

var outHead = ['Task', 'Time', '% of total', 'Finished'].map(function(heading) {
return chalk.bold.underline(heading);
});

var outList = data.map(function(entry) {
return [
entry.task,
chalk.cyan(entry.duration),
chalk.magenta(Math.round((entry.durationFloat / timeDiff) * 100) + '%'),
ordinalSuffixOf(entry.finishIndex + 1),
];
});

var outTable = [outHead].concat(outList);

var tableOpts = {
align: ['l', 'r', 'r', 'r'],
stringLength: function(str) {
var r = new RegExp('\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)', 'g');
return str.replace(r, '').length;
}
};

return table(outTable, tableOpts);
}

// Overview
console.log('\nGulp ran', chalk.cyan.bold(tableData.length + ' task' + (tableData.length === 1 ? '' : 's')), 'in', chalk.cyan.bold(prettyTime(totalTime)));
var totalTime = process.hrtime(startTime);

// Display table
console.log('\n' + formatTable(tableData) + '\n');
reporter({startTime: startTime, totalTime: totalTime, stats: tableData});

reset();
});
Expand Down

0 comments on commit 07ab1c3

Please sign in to comment.