all files / src/ index.js

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
100% Lines 13/13
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 50 51 52 53 54 55 56                                                                                     
'use strict';
 
/**
 * module dependencies
 */
var fs = require( 'fs' );
var Promise = require( 'bluebird' );
 
/**
 * promise wrapper for node’s fs.mkdir() that ignoes EEXIST by default
 *
 * wraps node’s fs.mkdir(), in a bluebird promise that resolves with `true` if
 * successful or rejects with the `Error` returned by fs.mkdir(); both results need to be
 * handled by the code calling this module
 *
 * @param {string|buffer} path
 * @param {number} [mode = 0o777]
 * @param {boolean} [ignore = true] ignore `EEXIST` directory errors returned by `fs.mkdir()`
 *
 * @returns {Promise}
 */
module.exports = function mkdir( path, mode, ignore ) {
  ignore = typeof ignore === 'boolean'
    ? ignore
    : true;
 
  return new Promise(
    /**
     * @param {Function} resolve
     * @param {Function} reject
     */
    function ( resolve, reject ) {
      fs.mkdir(
        path,
        mode,
        /**
         * @param {Error} [err]
         */
        function callback( err ) {
          if ( err ) {
            if ( err.code === 'EEXIST' && ignore ) {
              resolve( true );
              return;
            }
 
            reject( err );
            return;
          }
 
          resolve( true );
        }
      );
    }
  );
};