All files / libs/fs mkdirp.js

100% Statements 26/26
100% Branches 16/16
100% Functions 1/1
100% Lines 24/24
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70                  1x 1x   1x                       1x 13x 8x     13x 13x   13x 11x   13x   13x   13x 13x 9x   4x     2x 2x 2x 2x             2x 2x           2x       12x    
/**
 *
 * @module      libs/fs/mkdirp
 * @createdAt   2016-08-05
 *
 * @copyright   Copyright (c) 2016 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 */
 
var fs = require('fs')
var path = require('path')
 
var o777 = parseInt('0777', 8)
 
/**
 * 创建目录
 *
 * 如果上级目录不存在,也会创建,类似于 Linux 的 mkdirp
 * @param   {String}        p        要创建的目录
 * @param   {Object|Number} opts      配置或者直接指定要创建的目录的
 * @param   {Number}        opts.mode
 * @param   {Object}        opts.fs
 * @return  {String}
 */
module.exports = function mkdirp(p, opts, made) {
  if (!opts || typeof opts !== 'object') {
    opts = { mode: opts }
  }
 
  var mode = opts.mode
  var xfs = opts.fs || fs
 
  if (mode === undefined) {
    mode = o777 & (~process.umask())
  }
  if (!made) made = null
 
  p = path.resolve(p)
 
  try {
    xfs.mkdirSync(p, mode)
    made = made || p
  } catch (err0) {
    switch (err0.code) {
      case 'ENOENT':
        /* istanbul ignore if */
        if (path.dirname(p) === p) throw err0
        made = mkdirp(path.dirname(p), opts, made)
        mkdirp(p, opts, made)
        break
 
      // In the case of any other error, just see if there's a dir
      // there already.  If so, then hooray!  If not, then something
      // is borked.
      default:
        var stat
        try {
          stat = xfs.statSync(p)
        } catch (err1) {
          /* istanbul ignore next */
          throw err0
        }
        /* istanbul ignore else */
        if (!stat.isDirectory()) throw err0
    }
  }
 
  return made
}