index.js
1.26 KB
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
/*!
* output-file-sync | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/output-file-sync
*/
'use strict';
var dirname = require('path').dirname;
var writeFileSync = require('graceful-fs').writeFileSync;
var inspect = require('util').inspect;
var objectAssign = require('object-assign');
var mkdirpSync = require('mkdirp').sync;
module.exports = function outputFileSync(filePath, data, options) {
if (typeof filePath !== 'string') {
throw new TypeError(
inspect(filePath) +
' is not a string. Expected a file path to write a file.'
);
}
if (filePath === '') {
throw new Error('Expected a file path to write a file, but received an empty string instead.');
}
options = options || {};
var mkdirpOptions;
if (typeof options === 'string') {
mkdirpOptions = null;
} else if (options.dirMode) {
mkdirpOptions = objectAssign({}, options, {mode: options.dirMode});
} else {
mkdirpOptions = options;
}
var writeFileOptions;
if (options.fileMode) {
writeFileOptions = objectAssign({}, options, {mode: options.fileMode});
} else {
writeFileOptions = options;
}
var createdDirPath = mkdirpSync(dirname(filePath), mkdirpOptions);
writeFileSync(filePath, data, writeFileOptions);
return createdDirPath;
};