-
Notifications
You must be signed in to change notification settings - Fork 1
/
write.js
33 lines (25 loc) · 856 Bytes
/
write.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
module.exports = function write(data, opts) {
opts = Object.assign({}, this._opts, opts);
// Figure out which stringifier we can use
var strfiers = opts.writeFormats[opts.ext];
var strfierName, strfier, dataStr, strfierErr;
for(strfierName in strfiers) {
// Attempt to require the stringifier
try {
strfier = require(strfierName);
} catch (err) { continue; }
//Attempt to stringify the data
try {
dataStr = strfiers[strfierName](strfier, data, opts.parserOpts);
} catch (err) { strfierErr = err; continue; }
require('fs').writeFileSync(this._path, dataStr, opts.encoding);
return;
}
if (!strfier) {
throw new Error('Stringifier for filetype '+opts.ext+' not installed. '
+ 'You can "npm install --save" one of the following: '
+ Object.keys(strfiers).join(' '));
} else {
throw strfierErr;
}
};