Created
March 9, 2013 08:56
-
-
Save supersheep/5123549 to your computer and use it in GitHub Desktop.
mkdir sync as `mkdir -p`
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
function isDirectory(file){ | |
return fs.existsSync(file) && fs.statSync(file).isDirectory(); | |
}; | |
function mkdirSync(dir){ | |
if(!isDirectory(dir)){ | |
var SPLITTER = path.sep, | |
splits = dir.split(SPLITTER), | |
directory_stack = [], | |
last, | |
pop; | |
do{ | |
// last must not be a directory, so pop first | |
pop = splits.pop(); | |
pop && directory_stack.push(pop); | |
}while(splits.length && !isDirectory(last = splits.join(SPLITTER))); | |
while(directory_stack.length){ | |
last = path.join(last, directory_stack.pop()); | |
fs.mkdirSync(last); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment