forked from jonschlinkert/write-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (55 loc) · 1.48 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
/*!
* write-yaml <https://github.com/jonschlinkert/write-yaml>
*
* Copyright (c) 2014-2015, 2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var yaml = require('js-yaml');
var extend = require('extend-shallow');
var write = require('write');
module.exports = function(dest, data, options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
}
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
if (typeof dest !== 'string') {
cb(new TypeError('expected dest to be a string'));
return;
}
if (typeof data !== 'object') {
cb(new TypeError('expected data to be an object'));
return;
}
write(dest, toYaml(data, options), cb);
};
/**
* Sync method
*/
module.exports.sync = function(dest, data, options) {
if (typeof dest !== 'string') {
throw new TypeError('expected dest to be a string');
}
if (typeof data !== 'object') {
throw new TypeError('expected data to be an object');
}
try {
return write.sync(dest, toYaml(data, options));
} catch (err) {
err.reason = 'write-yaml: failed to write "' + dest + '": ';
throw err;
}
};
/**
* Convert data to yaml with the specified
* defaults and user-defined options
*/
function toYaml(data, options) {
var defaults = {indent: 2, skipInvalid: false, flowLevel: -1};
var opts = extend(defaults, options);
var fn = opts.safe ? yaml.safeDump : yaml.dump;
return fn(data, opts);
}