-
-
Save StuPig/3985571 to your computer and use it in GitHub Desktop.
mkdir -p for node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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