Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / write / index.js
1 /*!
2  * write <https://github.com/jonschlinkert/write>
3  *
4  * Copyright (c) 2014-2017, Jon Schlinkert.
5  * Released under the MIT License.
6  */
7
8 'use strict';
9
10 var fs = require('fs');
11 var path = require('path');
12 var mkdirp = require('mkdirp');
13
14 /**
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.
19  *
20  * ```js
21  * var writeFile = require('write');
22  * writeFile('foo.txt', 'This is content...', function(err) {
23  *   if (err) console.log(err);
24  * });
25  *
26  * // promise
27  * writeFile('foo.txt', 'This is content...')
28  *   .then(function() {
29  *     // do stuff
30  *   });
31  * ```
32  * @name writeFile
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.
37  * @api public
38  */
39
40 function writeFile(filepath, data, options, cb) {
41   if (typeof options === 'function') {
42     cb = options;
43     options = {};
44   }
45
46   if (typeof cb !== 'function') {
47     return writeFile.promise.apply(null, arguments);
48   }
49
50   if (typeof filepath !== 'string') {
51     cb(new TypeError('expected filepath to be a string'));
52     return;
53   }
54
55   mkdirp(path.dirname(filepath), options, function(err) {
56     if (err) {
57       cb(err);
58       return;
59     }
60     fs.writeFile(filepath, data, options, cb);
61   });
62 };
63
64 /**
65  * The promise version of [writeFile](#writefile). Returns a promise.
66  *
67  * ```js
68  * var writeFile = require('write');
69  * writeFile.promise('foo.txt', 'This is content...')
70  *   .then(function() {
71  *     // do stuff
72  *   });
73  * ```
74  * @name .promise
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][]
78  * @return {Promise}
79  * @api public
80  */
81
82 writeFile.promise = function(filepath, val, options) {
83   if (typeof filepath !== 'string') {
84     return Promise.reject(new TypeError('expected filepath to be a string'));
85   }
86
87   return new Promise(function(resolve, reject) {
88     mkdirp(path.dirname(filepath), options, function(err) {
89       if (err) {
90         reject(err);
91         return;
92       }
93
94       fs.writeFile(filepath, val, options, function(err) {
95         if (err) {
96           reject(err);
97           return;
98         }
99         resolve(val);
100       });
101     });
102   });
103 };
104
105 /**
106  * The synchronous version of [writeFile](#writefile). Returns undefined.
107  *
108  * ```js
109  * var writeFile = require('write');
110  * writeFile.sync('foo.txt', 'This is content...');
111  * ```
112  * @name .sync
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}
117  * @api public
118  */
119
120 writeFile.sync = function(filepath, data, options) {
121   if (typeof filepath !== 'string') {
122     throw new TypeError('expected filepath to be a string');
123   }
124   mkdirp.sync(path.dirname(filepath), options);
125   fs.writeFileSync(filepath, data, options);
126 };
127
128 /**
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)
133  * object.
134  *
135  * ```js
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() {
141  *     // do stuff
142  *   });
143  * ```
144  * @name .stream
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)).
148  * @api public
149  */
150
151 writeFile.stream = function(filepath, options) {
152   mkdirp.sync(path.dirname(filepath), options);
153   return fs.createWriteStream(filepath, options);
154 };
155
156 /**
157  * Expose `writeFile`
158  */
159
160 module.exports = writeFile;