fictorial (owner)

Revisions

gist: 232548 Download_button fork
public
Public Clone URL: git://gist.github.com/232548.git
Embed All Files: show embed
JavaScript #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 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;
    }
  });
}