Skip to content

Commit 4fb9216

Browse files
committed
Return the first directory made
1 parent dfb42c7 commit 4fb9216

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

index.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,38 @@ function mkdirP (p, mode, f) {
88
f = mode;
99
mode = 0777 & (~process.umask());
1010
}
11-
11+
12+
// secret passalong argument.
13+
var made = arguments[3] || null;
14+
1215
var cb = f || function () {};
1316
if (typeof mode === 'string') mode = parseInt(mode, 8);
1417
p = path.resolve(p);
1518

1619
fs.mkdir(p, mode, function (er) {
17-
if (!er) return cb();
20+
if (!er) {
21+
made = made || p;
22+
return cb(null, made);
23+
}
1824
switch (er.code) {
1925
case 'ENOENT':
20-
mkdirP(path.dirname(p), mode, function (er) {
21-
if (er) cb(er);
22-
else mkdirP(p, mode, cb);
26+
mkdirP(path.dirname(p), mode, function (er, made) {
27+
if (er) cb(er, made);
28+
else mkdirP(p, mode, cb, made);
2329
});
2430
break;
2531

2632
case 'EEXIST':
2733
fs.stat(p, function (er2, stat) {
2834
// if the stat fails, then that's super weird.
2935
// let the original EEXIST be the failure reason.
30-
if (er2 || !stat.isDirectory()) cb(er)
31-
else cb();
36+
if (er2 || !stat.isDirectory()) cb(er, made)
37+
else cb(null, made);
3238
});
3339
break;
3440

3541
default:
36-
cb(er);
42+
cb(er, made);
3743
break;
3844
}
3945
});
@@ -43,18 +49,22 @@ mkdirP.sync = function sync (p, mode) {
4349
if (mode === undefined) {
4450
mode = 0777 & (~process.umask());
4551
}
46-
52+
53+
// secret passalong argument
54+
var made = arguments[2] || null;
55+
4756
if (typeof mode === 'string') mode = parseInt(mode, 8);
4857
p = path.resolve(p);
4958

5059
try {
5160
fs.mkdirSync(p, mode);
61+
made = made || p;
5262
}
5363
catch (err0) {
5464
switch (err0.code) {
5565
case 'ENOENT' :
56-
sync(path.dirname(p), mode);
57-
sync(p, mode);
66+
made = sync(path.dirname(p), mode, made);
67+
sync(p, mode, made);
5868
break;
5969

6070
case 'EEXIST' :
@@ -72,6 +82,6 @@ mkdirP.sync = function sync (p, mode) {
7282
break;
7383
}
7484
}
75-
76-
return null;
85+
86+
return made;
7787
};

test/return.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var mkdirp = require('../');
2+
var path = require('path');
3+
var fs = require('fs');
4+
var test = require('tap').test;
5+
6+
test('return value', function (t) {
7+
t.plan(4);
8+
var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
9+
var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
10+
var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
11+
12+
var file = '/tmp/' + [x,y,z].join('/');
13+
14+
// should return the first dir created.
15+
// By this point, it would be profoundly surprising if /tmp didn't
16+
// already exist, since every other test makes things in there.
17+
mkdirp(file, function (err, made) {
18+
t.ifError(err);
19+
t.equal(made, '/tmp/' + x);
20+
mkdirp(file, function (err, made) {
21+
t.ifError(err);
22+
t.equal(made, null);
23+
});
24+
});
25+
});

test/return_sync.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var mkdirp = require('../');
2+
var path = require('path');
3+
var fs = require('fs');
4+
var test = require('tap').test;
5+
6+
test('return value', function (t) {
7+
t.plan(2);
8+
var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
9+
var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
10+
var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
11+
12+
var file = '/tmp/' + [x,y,z].join('/');
13+
14+
// should return the first dir created.
15+
// By this point, it would be profoundly surprising if /tmp didn't
16+
// already exist, since every other test makes things in there.
17+
// Note that this will throw on failure, which will fail the test.
18+
var made = mkdirp.sync(file);
19+
t.equal(made, '/tmp/' + x);
20+
21+
// making the same file again should have no effect.
22+
made = mkdirp.sync(file);
23+
t.equal(made, null);
24+
});

0 commit comments

Comments
 (0)