-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (97 loc) · 2.7 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
/*!
* write-data <https://github.com/jonschlinkert/write-data>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var path = require('path');
var json = require('write-json');
var yaml = require('write-yaml');
var extend = require('extend-shallow');
var isObject = require('isobject');
/**
* Asynchronously write JSON or YAML to disk, creating any
* intermediary directories if they don't exist. Data
* type is determined by the `dest` file extension.
*
* ```js
* writeData('foo.yml', {foo: "bar"}, function(err) {
* // do stuff with err
* });
* ```
* @param {String} `dest` (required) Destination filepath.
* @param {Object} `data` (required) The data object to write.
* @param {Options} `options` (optional)
* @param {Function} `cb` (required) Callback function
*/
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 path to be a string'));
return;
}
if (!isObject(data)) {
cb(new TypeError('expected data to be an object'));
return;
}
var opts = extend({ indent: 2, safe: true }, options);
var ext = opts.ext || path.extname(dest);
if (ext.charAt(0) !== '.') {
ext = '.' + ext;
}
switch (ext) {
case '.yml':
case '.yaml':
yaml(dest, data, opts, cb);
break;
case '.json':
json(dest, data, opts, cb);
break;
default: {
cb(new Error('unsupported file extension: ' + ext));
break;
}
}
};
/**
* Synchronously write JSON or YAML to disk, creating any
* intermediary directories if they don't exist. Data
* type is determined by the `dest` file extension.
*
* ```js
* writeData.sync('foo.yml', {foo: "bar"});
* ```
* @param {String} `dest` (required) Destination filepath.
* @param {Object} `data` (required) The data object to write.
* @param {Options} `options` (optional)
*/
module.exports.sync = function(dest, data, options) {
if (typeof dest !== 'string') {
throw new TypeError('expected dest path to be a string');
}
if (!isObject(data)) {
throw new TypeError('expected data to be an object');
}
var opts = extend({ indent: 2, safe: true }, options);
var ext = opts.ext || path.extname(dest);
if (ext.charAt(0) !== '.') {
ext = '.' + ext;
}
switch (ext) {
case '.yml':
case '.yaml':
return yaml.sync(dest, data, opts);
case '.json':
return json.sync(dest, data, opts);
default: {
throw new Error('unsupported file extension: ' + ext);
}
}
};