-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
59 lines (51 loc) · 1.36 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
'use strict';
var path = require('path');
var compareJson = require('compare-json');
var PluginError = require('plugin-error');
var fancyLog = require('fancy-log');
var through = require('through2');
var _ = require('lodash');
module.exports = function(options) {
var failed = false;
var opts = _.defaults({}, options, {
failOnError: false
});
// We're using gulp so log output using `fancy-log`
opts.output = fancyLog.info;
opts.error = function(msg) {
if (opts.failOnError ) {
failed = true;
}
fancyLog.info(msg);
};
var filePathCache = [];
/**
* Cache all filenames.
*/
function bufferContents(file, enc, cb) {
// Do not support streams
if (file.isStream()) {
cb(new PluginError('gulp-compare-json', 'Streaming not supported'));
return;
}
var resolvedPath = path.resolve(file.path);
filePathCache.push(resolvedPath);
cb();
}
/**
* When all filenames have been buffered, execute compare-json.
*/
function endStream(cb) {
try {
compareJson(filePathCache, opts);
if (failed) {
cb(new PluginError('gulp-compare-json', 'Comparing json files failed', {showStack: false}));
} else {
cb(null);
}
} catch (err) {
cb(new PluginError('gulp-compare-json', err, {showStack: true}));
}
}
return through.obj(bufferContents, endStream);
}