forked from hoho/gulp-dedupe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (52 loc) · 2.17 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
/*!
* gulp-dedupe, https://github.com/hoho/gulp-dedupe
* (c) 2014 Marat Abdullin, MIT license
*/
'use strict';
var through = require('through');
var PluginError = require('plugin-error');
var path = require('path');
var defaults = require('lodash.defaults');
module.exports = function(options) {
var filesMap = {};
options = defaults(options || {}, {
error: false, // Throw an error in case of duplicate.
same: true, // Throw an error in case duplicates have different contents.
diff: false // Supply duplicates with different content error with actual diff.
});
function bufferContents(file) {
if (file.isNull()) { return; }
if (file.isStream()) { return this.emit('error', new PluginError('gulp-dedupe', 'Streaming not supported')); }
var fullpath = path.resolve(file.path),
f;
if ((f = filesMap[fullpath])) {
if (options.error) {
this.emit('error', new PluginError('gulp-dedupe', 'Duplicate `' + file.path + '`'));
} else if (options.same && file.contents.toString() !== f.contents.toString()) {
var errorDiff = [];
if (options.diff) {
require('colors');
var diff = require('diff').diffChars(file.contents.toString(), f.contents.toString());
errorDiff.push(':\n');
diff.forEach(function(part){
// green for additions, red for deletions
// grey for common parts
var color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
errorDiff.push(part.value[color]);
});
}
errorDiff = errorDiff.join('');
this.emit('error', new PluginError('gulp-dedupe', 'Duplicate file `' + file.path + '` with different contents' + errorDiff));
}
return;
} else {
filesMap[fullpath] = file;
}
this.emit('data', file);
}
function endStream() {
this.emit('end');
}
return through(bufferContents, endStream);
};