Created
November 12, 2009 03:03
-
-
Save fictorial/232548 to your computer and use it in GitHub Desktop.
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
/** | |
* Tries to create the directories of the given path | |
* if they do not exist already. | |
*/ | |
this.mkpath = function(path, mode) { | |
var parts = path.split('/'); | |
if (!parts || parts.length == 0) | |
return; | |
mode = mode || 0750; | |
var current_path = ""; | |
parts.forEach(function(part) { | |
if (!part || part.length == 0) | |
return; | |
current_path += "/" + part; | |
try { | |
if (posix.stat(current_path).wait().isDirectory()) | |
return; | |
} catch (e) { | |
try { | |
posix.mkdir(current_path, mode).wait(); | |
var stats = posix.stat(current_path).wait(); | |
} catch (e) { | |
} | |
if (!stats || !stats.isDirectory()) | |
throw "failed to mkpath: current path is: " + current_path; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment