Skip to content

Instantly share code, notes, and snippets.

@hongru
Created May 28, 2012 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hongru/2817910 to your computer and use it in GitHub Desktop.
Save hongru/2817910 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