Skip to content

Instantly share code, notes, and snippets.

@macx
Created April 15, 2014 06:52
Show Gist options
  • Save macx/10708418 to your computer and use it in GitHub Desktop.
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
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