Skip to content

Instantly share code, notes, and snippets.

@StuPig
Forked from hongru/mkdir_p.js
Created October 31, 2012 07:14
Show Gist options
  • Save StuPig/3985571 to your computer and use it in GitHub Desktop.
Save StuPig/3985571 to your computer and use it in GitHub Desktop.
mkdir -p for node
/* mkdir -p for node */
var fs = require('fs'),
path = require('path');
function mkdirpSync (pathes, mode) {
mode = mode || 0777;
var dirs = pathes.trim().split('/');
if (dirs[0] == '.') {
// ./aaa
dirs.shift();
}
if (dirs[0] == '..') {
// ../aaa
dirs.splice(0, 2, dirs[0] + '/' + dirs[1]);
}
dirs.length && mkdir(dirs.shift());
// mkdir
function mkdir (d) {
if (!path.existsSync(d)) {
fs.mkdirSync(d, mode);
}
dirs.length && mkdir(d + '/' + dirs.shift());
}
}
// eg
//mkdirpSync('hongru/me');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment