-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (35 loc) · 969 Bytes
/
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
'use strict';
var util = require('gulp-util'),
through = require('through2'),
path = require('path'),
fs = require('fs'),
_ = require('lodash');
var PLUGIN_NAME = 'gulp-append-data';
module.exports = function (options) {
options = _.extend({
property: 'data',
getRelativePath: function(file) {
return util.replaceExtension(path.basename(file.path), '.json');
}
}, options || {});
return through.obj(function (file, enc, cb) {
var relPath, absPath;
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new util.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
relPath = options.getRelativePath(file);
absPath = path.resolve(path.dirname(file.path), relPath);
try {
file[options.property] = JSON.parse(fs.readFileSync(absPath));
} catch (err) {
// this.emit('error', new util.PluginError(PLUGIN_NAME, err));
}
this.push(file);
cb();
});
};