diff --git a/README.md b/README.md index 7281d8b..c172619 100644 --- a/README.md +++ b/README.md @@ -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 })); }); ``` diff --git a/index.js b/index.js index 6328411..2fded85 100644 --- a/index.js +++ b/index.js @@ -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')); @@ -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) {