-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (40 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
const through = require('through2')
const yaml = require('js-yaml')
const refs = require('json-refs')
const PluginError = require('plugin-error')
module.exports = function (options = {}, load = null) {
load = load || function (content, path = null) {
return yaml.safeLoad(content, { filename: path })
}
options = Object.assign({
filter: ['remote', 'relative'],
loaderOptions: {
processContent: (result, callback) => callback(null, load(result.text, result.path))
}
}, options)
refs.clearCache()
return through.obj(function (file, encoding, callback) {
if (file.isBuffer()) {
const opts = Object.assign({}, options)
opts.relativeBase = opts.relativeBase || file.dirname
opts.location = opts.location || file.path
Promise.resolve(file.contents.toString())
.then(content => load(content, file.path))
.then(json => refs.resolveRefs(json, opts))
.then(result => {
file.contents = Buffer.from(JSON.stringify(result.resolved))
file.extname = '.json'
callback(null, file)
}, error => {
callback(pluginError(error), file)
})
} else if (file.isStream()) {
callback(pluginError('Streaming not supported'), file)
} else {
callback(null, file)
}
})
}
function pluginError (message) {
return new PluginError('gulp-refs', message)
}