]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/fs-plus/node_modules/mkdirp/index.js
fda6de8a2c2313676c3f28a0b4cd77a1bbfdff28
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / fs-plus / 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, mode, f, made) {
7 if (typeof mode === 'function' || mode === undefined) {
8 f = mode;
9 mode = 0777 & (~process.umask());
10 }
11 if (!made) made = null;
12
13 var cb = f || function () {};
14 if (typeof mode === 'string') mode = parseInt(mode, 8);
15 p = path.resolve(p);
16
17 fs.mkdir(p, mode, function (er) {
18 if (!er) {
19 made = made || p;
20 return cb(null, made);
21 }
22 switch (er.code) {
23 case 'ENOENT':
24 mkdirP(path.dirname(p), mode, function (er, made) {
25 if (er) cb(er, made);
26 else mkdirP(p, mode, cb, made);
27 });
28 break;
29
30 // In the case of any other error, just see if there's a dir
31 // there already. If so, then hooray! If not, then something
32 // is borked.
33 default:
34 fs.stat(p, function (er2, stat) {
35 // if the stat fails, then that's super weird.
36 // let the original error be the failure reason.
37 if (er2 || !stat.isDirectory()) cb(er, made)
38 else cb(null, made);
39 });
40 break;
41 }
42 });
43 }
44
45 mkdirP.sync = function sync (p, mode, made) {
46 if (mode === undefined) {
47 mode = 0777 & (~process.umask());
48 }
49 if (!made) made = null;
50
51 if (typeof mode === 'string') mode = parseInt(mode, 8);
52 p = path.resolve(p);
53
54 try {
55 fs.mkdirSync(p, mode);
56 made = made || p;
57 }
58 catch (err0) {
59 switch (err0.code) {
60 case 'ENOENT' :
61 made = sync(path.dirname(p), mode, made);
62 sync(p, mode, made);
63 break;
64
65 // In the case of any other error, just see if there's a dir
66 // there already. If so, then hooray! If not, then something
67 // is borked.
68 default:
69 var stat;
70 try {
71 stat = fs.statSync(p);
72 }
73 catch (err1) {
74 throw err0;
75 }
76 if (!stat.isDirectory()) throw err0;
77 break;
78 }
79 }
80
81 return made;
82 };