2 * write <https://github.com/jonschlinkert/write>
4 * Copyright (c) 2014-2017, Jon Schlinkert.
5 * Released under the MIT License.
10 var fs = require('fs');
11 var path = require('path');
12 var mkdirp = require('mkdirp');
15 * Asynchronously writes data to a file, replacing the file if it already
16 * exists and creating any intermediate directories if they don't already
17 * exist. Data can be a string or a buffer. Returns a promise if a callback
18 * function is not passed.
21 * var writeFile = require('write');
22 * writeFile('foo.txt', 'This is content...', function(err) {
23 * if (err) console.log(err);
27 * writeFile('foo.txt', 'This is content...')
33 * @param {string|Buffer|integer} `filepath` filepath or file descriptor.
34 * @param {string|Buffer|Uint8Array} `data` String to write to disk.
35 * @param {object} `options` Options to pass to [fs.writeFile][fs]{#fs_fs_writefile_file_data_options_callback} and/or [mkdirp][]
36 * @param {Function} `callback` (optional) If no callback is provided, a promise is returned.
40 function writeFile(filepath, data, options, cb) {
41 if (typeof options === 'function') {
46 if (typeof cb !== 'function') {
47 return writeFile.promise.apply(null, arguments);
50 if (typeof filepath !== 'string') {
51 cb(new TypeError('expected filepath to be a string'));
55 mkdirp(path.dirname(filepath), options, function(err) {
60 fs.writeFile(filepath, data, options, cb);
65 * The promise version of [writeFile](#writefile). Returns a promise.
68 * var writeFile = require('write');
69 * writeFile.promise('foo.txt', 'This is content...')
75 * @param {string|Buffer|integer} `filepath` filepath or file descriptor.
76 * @param {string|Buffer|Uint8Array} `val` String or buffer to write to disk.
77 * @param {object} `options` Options to pass to [fs.writeFile][fs]{#fs_fs_writefile_file_data_options_callback} and/or [mkdirp][]
82 writeFile.promise = function(filepath, val, options) {
83 if (typeof filepath !== 'string') {
84 return Promise.reject(new TypeError('expected filepath to be a string'));
87 return new Promise(function(resolve, reject) {
88 mkdirp(path.dirname(filepath), options, function(err) {
94 fs.writeFile(filepath, val, options, function(err) {
106 * The synchronous version of [writeFile](#writefile). Returns undefined.
109 * var writeFile = require('write');
110 * writeFile.sync('foo.txt', 'This is content...');
113 * @param {string|Buffer|integer} `filepath` filepath or file descriptor.
114 * @param {string|Buffer|Uint8Array} `data` String or buffer to write to disk.
115 * @param {object} `options` Options to pass to [fs.writeFileSync][fs]{#fs_fs_writefilesync_file_data_options} and/or [mkdirp][]
116 * @return {undefined}
120 writeFile.sync = function(filepath, data, options) {
121 if (typeof filepath !== 'string') {
122 throw new TypeError('expected filepath to be a string');
124 mkdirp.sync(path.dirname(filepath), options);
125 fs.writeFileSync(filepath, data, options);
129 * Uses `fs.createWriteStream` to write data to a file, replacing the
130 * file if it already exists and creating any intermediate directories
131 * if they don't already exist. Data can be a string or a buffer. Returns
132 * a new [WriteStream](https://nodejs.org/api/fs.html#fs_class_fs_writestream)
136 * var fs = require('fs');
137 * var writeFile = require('write');
138 * fs.createReadStream('README.md')
139 * .pipe(writeFile.stream('a/b/c/other-file.md'))
140 * .on('close', function() {
145 * @param {string|Buffer|integer} `filepath` filepath or file descriptor.
146 * @param {object} `options` Options to pass to [mkdirp][] and [fs.createWriteStream][fs]{#fs_fs_createwritestream_path_options}
147 * @return {Stream} Returns a new [WriteStream](https://nodejs.org/api/fs.html#fs_class_fs_writestream) object. (See [Writable Stream](https://nodejs.org/api/stream.html#stream_class_stream_writable)).
151 writeFile.stream = function(filepath, options) {
152 mkdirp.sync(path.dirname(filepath), options);
153 return fs.createWriteStream(filepath, options);
160 module.exports = writeFile;