index.js
function writeFile(filepath, data, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = {};
  }

  if (typeof cb !== 'function') {
    return writeFile.promise.apply(null, arguments);
  }

  if (typeof filepath !== 'string') {
    cb(new TypeError('expected filepath to be a string'));
    return;
  }

  mkdirp(path.dirname(filepath), options, function(err) {
    if (err) {
      cb(err);
      return;
    }

    var preparedData = data;
    if (options && options.ensureNewLine && data.slice(-1) !== "\n") {
      preparedData += "\n";
    }

    fs.writeFile(filepath, preparedData, options, cb);
  });
}
function writeFile(filepath, data, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = {};
  }

  if (typeof cb !== 'function') {
    return writeFile.promise.apply(null, arguments);
  }

  if (typeof filepath !== 'string') {
    cb(new TypeError('expected filepath to be a string'));
    return;
  }

  mkdirp(path.dirname(filepath), options, function(err) {
    if (err) {
      cb(err);
      return;
    }

    var preparedData = data;
    if (options && options.ensureNewLine && data.slice(-1) !== "\n") {
      preparedData += "\n";
    }

    fs.writeFile(filepath, preparedData, options, cb);
  });
}
function writeFile ( filepath , data , options , cb ) { if ( typeof options === 'function' ) { cb = options ; options = { } ; } if ( typeof cb !== 'function' ) { return writeFile . promise . apply ( null , arguments ) ; } if ( typeof filepath !== 'string' ) { cb ( new TypeError ( 'expected filepath to be a string' ) ) ; return ; } mkdirp ( path . dirname ( filepath ) , options , function ( err ) { if ( err ) { cb ( err ) ; return ; } var preparedData = data ; if ( options && options . ensureNewLine && data . slice ( - 1 ) !== "\n" ) { preparedData += "\n" ; } fs . writeFile ( filepath , preparedData , options , cb ) ; } ) ; }
Asynchronously writes data to a file, replacing the file if it already
exists and creating any intermediate directories if they don't already
exist. Data can be a string or a buffer. Returns a promise if a callback
function is not passed.

```js
var writeFile = require('write');
writeFile('foo.txt', 'This is content...', function(err) {
if (err) console.log(err);
});

// promise
writeFile('foo.txt', 'This is content...')
.then(function() {
// do stuff
});
```
@name writeFile
@param {string|Buffer|integer} `filepath` filepath or file descriptor.
@param {string|Buffer|Uint8Array} `data` String to write to disk.
@param {object} `options` Options to pass to [fs.writeFile][fs]{#fs_fs_writefile_file_data_options_callback} and/or [mkdirp][]
@param {Function} `callback` (optional) If no callback is provided, a promise is returned.
@api public
Asynchronously writes data to a file replacing the file if it already exists and creating any intermediate directories if they don t already exist . Data can be a string or a buffer . Returns a promise if a callback function is not passed .