Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created November 12, 2009 03:03
Show Gist options
  • Save fictorial/232548 to your computer and use it in GitHub Desktop.
Save fictorial/232548 to your computer and use it in GitHub Desktop.
/**
* 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