Sha256: 96d66389ab762a192563c9f65de67868cf1215d1f623c32630c547702bef3628

Contents?: true

Size: 1.66 KB

Versions: 45

Compression:

Stored size: 1.66 KB

Contents

var common = require('./common');
var fs = require('fs');
var path = require('path');

// Recursively creates 'dir'
function mkdirSyncRecursive(dir) {
  var baseDir = path.dirname(dir);

  // Base dir exists, no recursion necessary
  if (fs.existsSync(baseDir)) {
    fs.mkdirSync(dir, parseInt('0777', 8));
    return;
  }

  // Base dir does not exist, go recursive
  mkdirSyncRecursive(baseDir);

  // Base dir created, can create dir
  fs.mkdirSync(dir, parseInt('0777', 8));
}

//@
//@ ### mkdir([options,] dir [, dir ...])
//@ ### mkdir([options,] dir_array)
//@ Available options:
//@
//@ + `-p`: full path (will create intermediate dirs if necessary)
//@
//@ Examples:
//@
//@ ```javascript
//@ mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
//@ mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
//@ ```
//@
//@ Creates directories.
function _mkdir(options, dirs) {
  options = common.parseOptions(options, {
    'p': 'fullpath'
  });
  if (!dirs)
    common.error('no paths given');

  if (typeof dirs === 'string')
    dirs = [].slice.call(arguments, 1);
  // if it's array leave it as it is

  dirs.forEach(function(dir) {
    if (fs.existsSync(dir)) {
      if (!options.fullpath)
          common.error('path already exists: ' + dir, true);
      return; // skip dir
    }

    // Base dir does not exist, and no -p option given
    var baseDir = path.dirname(dir);
    if (!fs.existsSync(baseDir) && !options.fullpath) {
      common.error('no such file or directory: ' + baseDir, true);
      return; // skip dir
    }

    if (options.fullpath)
      mkdirSyncRecursive(dir);
    else
      fs.mkdirSync(dir, parseInt('0777', 8));
  });
} // mkdir
module.exports = _mkdir;

Version data entries

45 entries across 45 versions & 2 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.21.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.20.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.19.1 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.19.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.18.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.17.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.16.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.15.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.14.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.13.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.12.2 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.12.1 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.12.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.11.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.10.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.9.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.8.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.7.0 node_modules/shelljs/src/mkdir.js
govuk_publishing_components-17.6.1 node_modules/shelljs/src/mkdir.js