-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (52 loc) · 1.59 KB
/
index.js
File metadata and controls
67 lines (52 loc) · 1.59 KB
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
'use strict';
var extend = require('node.extend');
var through = require('through');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var PLUGIN_NAME = 'gulp-extend';
module.exports = function(fileName, deep, jsonSpace) {
if (!fileName) {
throw new PluginError(PLUGIN_NAME, PLUGIN_NAME + ': Missing fileName parameter');
}
var buffer = [];
var firstFile = null;
deep = (deep !== undefined) ? deep : true;
buffer.push(deep); // first argument
function bufferContents(file) {
if (file.isNull()) {
return this.queue(file);
}
if (file.isStream()) {
return this.emit('error', new PluginError(PLUGIN_NAME, PLUGIN_NAME + ': Streaming not supported'));
}
if (!firstFile) {
firstFile = file;
}
var jsonContent;
try {
jsonContent = JSON.parse(file.contents.toString('utf8'));
} catch (e) {
jsonContent = {};
console.log('[' + gutil.colors.red('gulp-extend') + '] File "' + file.path + '" has errors and was skipped!');
}
buffer.push(jsonContent);
}
function endStream() {
if (buffer.length === 1) {
return this.emit('end');
}
var joinedContents = extend.apply(this, buffer);
var joinedPath = path.join(firstFile.base, fileName);
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: new Buffer(JSON.stringify(joinedContents, null, jsonSpace))
});
this.emit('data', joinedFile);
this.emit('end');
}
return through(bufferContents, endStream);
};