-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
110 lines (82 loc) · 2.77 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict'
const yaml = require('js-yaml')
const through = require('through2')
const BufferStreams = require('bufferstreams')
const replaceExt = require('replace-ext')
const PluginError = require('plugin-error')
const assign = require('object-assign')
const PLUGIN_NAME = 'gulp-yaml'
module.exports = function (options) {
options = assign({}, options)
options.schema = getSchema(options)
options.ext = options.ext || '.json'
const providedFilename = options.filename
return through.obj(function (file, enc, callback) {
options.filename = providedFilename || file.path
const stream = this
file.contents = getFileContents(file, options, stream)
stream.push(file)
callback()
})
}
function getSchema (options) {
if (options.safe === false) { return yaml.DEFAULT_FULL_SCHEMA }
if (options.safe === true || options.schema === undefined) { return yaml.DEFAULT_SAFE_SCHEMA }
if (options.schema instanceof yaml.Schema) { return options.schema }
const schemaName = typeof options.schema === 'string' ? options.schema.toUpperCase() : options.schema
if (yaml[schemaName] !== undefined) { return yaml[schemaName] }
throw getError('Schema ' + schemaName + ' is not valid')
}
function getFileContents (file, options, stream) {
if (file.isNull()) {
return file.contents
}
if (file.isBuffer()) {
return getBufferContents(file, options, stream)
}
if (file.isStream()) {
return getStreamContents(file, options, stream)
}
}
function getBufferContents (file, options, stream) {
const parsed = convertYaml(file, file.contents, options)
if (parsed instanceof PluginError) {
stream.emit('error', parsed)
return Buffer.from('')
}
return parsed
}
function getStreamContents (file, options, stream) {
const streamer = new BufferStreams(function (err, buf, callback) {
if (err) {
stream.emit('error', getError(err))
return callback()
}
const parsed = convertYaml(file, buf, options)
if (parsed instanceof PluginError) {
stream.emit('error', parsed)
return callback()
}
callback(null, parsed)
})
return file.contents.pipe(streamer)
}
function convertYaml (file, buf, options) {
if (buf.length === 0) {
return getError('File ' + file.path + ' is empty. YAML loader cannot load empty content')
}
try {
file.path = replaceExt(file.path, options.ext)
return yaml2json(buf, options)
} catch (error) {
return getError(error)
}
}
function yaml2json (buffer, options) {
const ymlDocument = options.safe ? yaml.safeLoad(buffer, options) : yaml.load(buffer, options)
const jsonValue = JSON.stringify(ymlDocument, options.replacer, options.space)
return Buffer.from(jsonValue)
}
function getError (error) {
return new PluginError(PLUGIN_NAME, error, { showStack: true })
}