all files / src/ index.js

100% Statements 9/9
100% Branches 2/2
100% Functions 3/3
100% Lines 9/9
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 48 49                                                                               
'use strict';
 
/**
 * module dependencies
 */
var fs = require( 'fs' );
var Promise = require( 'bluebird' );
 
/**
 * wraps node’s `fs.writeFile()`, in a bluebird ( v 3.4.6 ) promise that resolves with `true` if
 * successful or rejects with the `Error` returned by `fs.writeFile()`; both results need to be
 * handled by the code calling this module
 *
 * @param {string|buffer|number} file filename or file descriptor
 * @param {string|buffer} data
 * @param {object|string} [options]
 * @param {string|null} [options.encoding = 'utf-8']
 * @param {number} [options.mode = 0o666]
 * @param {string} [options.flag = 'w']
 *
 * @returns {bluebird}
 */
module.exports = function writeFile( file, data, options ) {
  return new Promise(
    /**
     * @param {Function} resolve
     * @param {Function} reject
     */
    function ( resolve, reject ) {
      fs.writeFile(
        file,
        data,
        options,
        /**
         * @param {Error} [err]
         */
        function callback( err ) {
          if ( err ) {
            reject( err );
            return;
          }
 
          resolve( true );
        }
      );
    }
  );
};