Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ gulp.task('js-beautify', function() {
mode: 'VERIFY_AND_WRITE'
}))
.pipe(diff(/* 'target directory to diff against', defaults to diff against original source file */))
// emit an error on finding diffs
.pipe(diff.reporter({ fail: true }));
// fail: emit an error on finding diffs
// compact: only show changed and surrounding lines
.pipe(diff.reporter({ fail: true, compact: true }));
});
```
60 changes: 54 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ var reporter = function reporter(opts) {
if (file.diff && Object.keys(file.diff).length) {
if (!opts.quiet) {
console.log('\n' + clc.underline(file.path), '\n');
console.log(file.diff.map(formatLine).join(''));

var flag = flagParts();
var formatLine = lineFormatter(opts);
console.log(file.diff
.map(flag)
.map(formatLine).join('')
);
}
if (opts.fail) {
this.emit('error', pluginError('Files differ'));
Expand All @@ -67,11 +73,53 @@ var reporter = function reporter(opts) {
});
};

function formatLine(part, i) {
var indent = ' ';
return (!i ? indent : '') + part.value.split('\n').map(function(ln) {
return clc[colorLine(part)](ln);
}).join('\n' + indent);
function flagParts() {
var previous = null;

function changed(part) {
return part.removed || part.added;
}

return function(part) {
if (changed(part) && previous !== null && !changed(previous))
previous.before = true;

if (!changed(part) && previous !== null && changed(previous))
part.after = true;

previous = part;
return part;
};
}

function trimPart(part, compact) {
var lines = part.value.split('\n');
var res = [];
if (!compact) return lines;

if (part.added || part.removed)
return lines;

if (part.before && part.after && lines < 10)
return lines;

if (part.after)
res = res.concat(lines.slice(0, 5)).concat(['...']);

if (part.before)
res = res.concat(['...']).concat(lines.slice(-5));

return res;
}

function lineFormatter(opts) {
return function formatLine(part, i) {
var indent = ' ';
return (!i ? indent : '') + trimPart(part, opts.compact)
.map(function(ln) {
return clc[colorLine(part)](ln);
}).join('\n' + indent);
};
}

function colorLine(ln) {
Expand Down