You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
4 years ago
|
/*!
|
||
|
* 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;
|
||
|
};
|