Created
April 15, 2014 06:52
-
-
Save macx/10708418 to your computer and use it in GitHub Desktop.
Recursively created directories along a path. It will create each dir in given string ´foo/bar/doo´. Source: http://goo.gl/bvcJk
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
var fs = require('fs'); | |
var path = require('path'); | |
fs.mkdirParent = function(dirPath, mode, callback) { | |
//Call the standard fs.mkdir | |
fs.mkdir(dirPath, mode, function(error) { | |
//When it fail in this way, do the custom steps | |
if (error && error.errno === 34) { | |
//Create all the parents recursively | |
fs.mkdirParent(path.dirname(dirPath), mode, callback); | |
//And then the directory | |
fs.mkdirParent(dirPath, mode, callback); | |
} | |
//Manually run the callback since we used our own callback to do all these | |
callback && callback(error); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment