Created
May 23, 2012 09:49
-
-
Save Ajnasz/2774326 to your computer and use it in GitHub Desktop.
Create directories recusively with nodejs
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'); | |
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