-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
75 lines (56 loc) · 1.71 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
68
69
70
71
72
73
74
75
'use strict';
const through = require('through2');
const path = require('path');
const PluginError = require('plugin-error');
const yaml = require('js-yaml');
const merge = require('lodash.merge');
const File = require('vinyl');
module.exports = function (file, opt) {
if (!file) {
throw new PluginError('gulp-yaml-merge', 'Missing file option for gulp-yaml-merge');
}
opt = opt || {};
let loadOptions = opt.load || {};
const dumpOptions = opt.dump || {};
let latestFile;
let outData = {};
if (typeof file !== 'string' && typeof file.path !== 'string') {
throw new PluginError('gulp-yaml-merge', 'Missing path in file options for gulp-yaml-merge');
}
function bufferContents(file, enc, cb) {
// ignore empty files
if (file.isNull()) {
cb();
return;
}
// we don't do streams (yet)
if (file.isStream()) {
this.emit('error', new PluginError('gulp-yaml-merge', 'Streaming not supported'));
cb();
return;
}
latestFile = file;
// pass file path for yaml error handler
loadOptions = merge(loadOptions, { filename: file.path });
try {
const data = yaml.safeLoad(file.contents, loadOptions);
outData = merge(outData, data);
} catch (err) {
this.emit('error', new PluginError('gulp-yaml-merge', err));
}
cb();
}
function endStream(cb) {
let outFile;
if (typeof file === 'string') {
outFile = latestFile.clone({ contents: false });
outFile.path = path.join(latestFile.base, file);
} else {
outFile = new File(file);
}
outFile.contents = Buffer.from(yaml.safeDump(outData, dumpOptions));
this.push(outFile);
cb();
}
return through.obj(bufferContents, endStream);
};