a1742b20694fe38589145e7d68d3cd0df91012c9
[yaffs-website] / node_modules / extract-zip / node_modules / mkdirp / index.js
1 var path = require('path');
2 var fs = require('fs');
3
4 module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
5
6 function mkdirP (p, opts, f, made) {
7     if (typeof opts === 'function') {
8         f = opts;
9         opts = {};
10     }
11     else if (!opts || typeof opts !== 'object') {
12         opts = { mode: opts };
13     }
14     
15     var mode = opts.mode;
16     var xfs = opts.fs || fs;
17     
18     if (mode === undefined) {
19         mode = 0777 & (~process.umask());
20     }
21     if (!made) made = null;
22     
23     var cb = f || function () {};
24     p = path.resolve(p);
25     
26     xfs.mkdir(p, mode, function (er) {
27         if (!er) {
28             made = made || p;
29             return cb(null, made);
30         }
31         switch (er.code) {
32             case 'ENOENT':
33                 mkdirP(path.dirname(p), opts, function (er, made) {
34                     if (er) cb(er, made);
35                     else mkdirP(p, opts, cb, made);
36                 });
37                 break;
38
39             // In the case of any other error, just see if there's a dir
40             // there already.  If so, then hooray!  If not, then something
41             // is borked.
42             default:
43                 xfs.stat(p, function (er2, stat) {
44                     // if the stat fails, then that's super weird.
45                     // let the original error be the failure reason.
46                     if (er2 || !stat.isDirectory()) cb(er, made)
47                     else cb(null, made);
48                 });
49                 break;
50         }
51     });
52 }
53
54 mkdirP.sync = function sync (p, opts, made) {
55     if (!opts || typeof opts !== 'object') {
56         opts = { mode: opts };
57     }
58     
59     var mode = opts.mode;
60     var xfs = opts.fs || fs;
61     
62     if (mode === undefined) {
63         mode = 0777 & (~process.umask());
64     }
65     if (!made) made = null;
66
67     p = path.resolve(p);
68
69     try {
70         xfs.mkdirSync(p, mode);
71         made = made || p;
72     }
73     catch (err0) {
74         switch (err0.code) {
75             case 'ENOENT' :
76                 made = sync(path.dirname(p), opts, made);
77                 sync(p, opts, made);
78                 break;
79
80             // In the case of any other error, just see if there's a dir
81             // there already.  If so, then hooray!  If not, then something
82             // is borked.
83             default:
84                 var stat;
85                 try {
86                     stat = xfs.statSync(p);
87                 }
88                 catch (err1) {
89                     throw err0;
90                 }
91                 if (!stat.isDirectory()) throw err0;
92                 break;
93         }
94     }
95
96     return made;
97 };