Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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