forked from crissdev/gulp-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (104 loc) · 3.87 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
111
112
113
114
115
116
117
118
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var yaml = require('js-yaml');
var xtend = require('xtend');
var BufferStreams = require('bufferstreams');
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-yaml-sort';
function yamlSort(buffer, options) {
var defaultDumpOptions = {
'sortKeys': true,
'lineWidth': options.lineWidth
};
var contents = buffer.toString('utf8');
var ymlOptions = {schema: options.schema, filename: options.filename};
var ymlDocument = options.safe ? yaml.safeLoad(contents, ymlOptions) : yaml.load(contents, ymlOptions);
var orderedDocument = yaml.dump(ymlDocument, defaultDumpOptions);
if(options.check_ordered) {
defaultDumpOptions.sortKeys = false;
var ymlDocument = yaml.dump(ymlDocument, defaultDumpOptions);
if(ymlDocument !== orderedDocument) {
throw new Error('File: ' + options.filename + ' must be processed');
}
}
return new Buffer(orderedDocument);
}
function parseSchema(schema) {
switch (schema) {
case 'DEFAULT_SAFE_SCHEMA':
case 'default_safe_schema':
return yaml.DEFAULT_SAFE_SCHEMA;
case 'DEFAULT_FULL_SCHEMA':
case 'default_full_schema':
return yaml.DEFAULT_FULL_SCHEMA;
case 'CORE_SCHEMA':
case 'core_schema':
return yaml.CORE_SCHEMA;
case 'JSON_SCHEMA':
case 'json_schema':
return yaml.JSON_SCHEMA;
case 'FAILSAFE_SCHEMA':
case 'failsafe_schema':
return yaml.FAILSAFE_SCHEMA;
}
throw new PluginError(PLUGIN_NAME, 'Schema ' + schema + ' is not valid');
}
module.exports = function(options) {
options = xtend({safe: true, replacer: null, space: null}, options);
var providedFilename = options.filename;
if (!options.lineWidth) {
options.lineWidth = 100000;
}
if (!options.schema) {
options.schema = options.safe ? yaml.DEFAULT_SAFE_SCHEMA : yaml.DEFAULT_FULL_SCHEMA;
}
else {
options.schema = parseSchema(options.schema);
}
return through.obj(function(file, enc, callback) {
if (!providedFilename) {
options.filename = file.path;
}
if (file.isBuffer()) {
if (file.contents.length === 0) {
this.emit('error', new PluginError(PLUGIN_NAME, 'File ' + file.path +
' is empty. YAML loader cannot load empty content'));
return callback();
}
try {
file.contents = yamlSort(file.contents, options);
}
catch (error) {
this.emit('error', new PluginError(PLUGIN_NAME, error, {showStack: true}));
return callback();
}
}
else if (file.isStream()) {
var _this = this;
var streamer = new BufferStreams(function(err, buf, cb) {
if (err) {
_this.emit('error', new PluginError(PLUGIN_NAME, err, {showStack: true}));
}
else {
if (buf.length === 0) {
_this.emit('error', new PluginError(PLUGIN_NAME, 'File ' + file.path +
' is empty. YAML loader cannot load empty content'));
}
else {
try {
var parsed = yamlSort(buf, options);
cb(null, parsed);
}
catch (error) {
_this.emit('error', new PluginError(PLUGIN_NAME, error, {showStack: true}));
}
}
}
});
file.contents = file.contents.pipe(streamer);
}
this.push(file);
callback();
});
};