Skip to content

Instantly share code, notes, and snippets.

@supersheep
Created March 9, 2013 08:56
Show Gist options
  • Save supersheep/5123549 to your computer and use it in GitHub Desktop.
Save supersheep/5123549 to your computer and use it in GitHub Desktop.
mkdir sync as `mkdir -p`
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