-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (56 loc) · 1.84 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
/**
* gulp-yaml-data index.js
*
* @author Otto Rask <ojrask@gmail.com>
*/
var through = require('through2');
var PluginError = require('gulp-util/lib/PluginError');
var yaml = require('js-yaml');
var fs = require('fs');
module.exports = function (options)
{
'use strict';
// Defaults.
options = Object.assign({
property: 'data',
override: false
}, options);
// Enforce src option. Either string or array is allowed.
if (
typeof options.src !== 'string' &&
typeof options.src !== 'object' &&
typeof options.src.isArray !== 'function'
) {
throw new PluginError(
'gulp-yaml-data',
new TypeError('Invalid or missing sourceFile option given for gulp-yaml-data')
);
}
// Read a YAML file to an object and return it.
var readYamlFile = function (file) {
return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
};
// Transform current stream to include a YAML data file.
var transformStream = function (file, enc, cb) {
var yaml_files = options.src;
if (typeof yaml_files === 'string') {
yaml_files = [yaml_files];
}
var yaml_data = {};
// Read all files in order, and override if later files contain same values.
for (var i = 0; i < yaml_files.length; i++) {
yaml_data = Object.assign(yaml_data, readYamlFile(yaml_files[i]));
}
var current_data = file[options.property];
var new_data = null;
// Merge with possible previous data.
if (options.override) {
new_data = Object.assign(current_data, yaml_data);
} else {
new_data = Object.assign(yaml_data, current_data);
}
file[options.property] = new_data;
cb(null, file);
};
return through.obj(transformStream);
};