Skip to content

Instantly share code, notes, and snippets.

@Ajnasz
Created May 23, 2012 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ajnasz/2774326 to your computer and use it in GitHub Desktop.
Save Ajnasz/2774326 to your computer and use it in GitHub Desktop.
Create directories recusively with nodejs
var fs = require('fs');
function mkdir_p_async(directory, position, callback) {
position = position || 0;
var dir = require('path').normalize(directory).split('/');
if (position >= dir.length) {
callback();
return;
}
var done = false;
var f = dir.slice(0, position + 1).join('/');
fs.stat(f, function (er, stat) {
if (er) { // dir not exists
fs.mkdir(f, function (er) {
mkdir_p_async(directory, position + 1, callback);
});
} else {
mkdir_p_async(directory, position + 1, callback);
}
});
}
function mkdirThese(these, cb) {
if (these.length > 0) {
var dir = these.shift();
mkdir_p_async(dir, 0, function () {
mkdirThese(these, cb);
});
} else {
cb();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment