Permalink
Browse files

Return the first directory made

  • Loading branch information...
1 parent dfb42c7 commit 4fb92165ccc75732028347bc1001ddb28bec5752 @isaacs isaacs committed Mar 28, 2012
Showing with 72 additions and 13 deletions.
  1. +23 −13 index.js
  2. +25 −0 test/return.js
  3. +24 −0 test/return_sync.js
View
@@ -8,32 +8,38 @@ function mkdirP (p, mode, f) {
f = mode;
mode = 0777 & (~process.umask());
}
-
+
+ // secret passalong argument.
+ var made = arguments[3] || null;
+
var cb = f || function () {};
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
fs.mkdir(p, mode, function (er) {
- if (!er) return cb();
+ if (!er) {
+ made = made || p;
+ return cb(null, made);
+ }
switch (er.code) {
case 'ENOENT':
- mkdirP(path.dirname(p), mode, function (er) {
- if (er) cb(er);
- else mkdirP(p, mode, cb);
+ mkdirP(path.dirname(p), mode, function (er, made) {
+ if (er) cb(er, made);
+ else mkdirP(p, mode, cb, made);
});
break;
case 'EEXIST':
fs.stat(p, function (er2, stat) {
// if the stat fails, then that's super weird.
// let the original EEXIST be the failure reason.
- if (er2 || !stat.isDirectory()) cb(er)
- else cb();
+ if (er2 || !stat.isDirectory()) cb(er, made)
+ else cb(null, made);
});
break;
default:
- cb(er);
+ cb(er, made);
break;
}
});
@@ -43,18 +49,22 @@ mkdirP.sync = function sync (p, mode) {
if (mode === undefined) {
mode = 0777 & (~process.umask());
}
-
+
+ // secret passalong argument
+ var made = arguments[2] || null;
+
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
try {
fs.mkdirSync(p, mode);
+ made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
- sync(path.dirname(p), mode);
- sync(p, mode);
+ made = sync(path.dirname(p), mode, made);
+ sync(p, mode, made);
break;
case 'EEXIST' :
@@ -72,6 +82,6 @@ mkdirP.sync = function sync (p, mode) {
break;
}
}
-
- return null;
+
+ return made;
};
View
@@ -0,0 +1,25 @@
+var mkdirp = require('../');
+var path = require('path');
+var fs = require('fs');
+var test = require('tap').test;
+
+test('return value', function (t) {
+ t.plan(4);
+ var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+ var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+ var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+
+ var file = '/tmp/' + [x,y,z].join('/');
+
+ // should return the first dir created.
+ // By this point, it would be profoundly surprising if /tmp didn't
+ // already exist, since every other test makes things in there.
+ mkdirp(file, function (err, made) {
+ t.ifError(err);
+ t.equal(made, '/tmp/' + x);
+ mkdirp(file, function (err, made) {
+ t.ifError(err);
+ t.equal(made, null);
+ });
+ });
+});
View
@@ -0,0 +1,24 @@
+var mkdirp = require('../');
+var path = require('path');
+var fs = require('fs');
+var test = require('tap').test;
+
+test('return value', function (t) {
+ t.plan(2);
+ var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+ var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+ var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
+
+ var file = '/tmp/' + [x,y,z].join('/');
+
+ // should return the first dir created.
+ // By this point, it would be profoundly surprising if /tmp didn't
+ // already exist, since every other test makes things in there.
+ // Note that this will throw on failure, which will fail the test.
+ var made = mkdirp.sync(file);
+ t.equal(made, '/tmp/' + x);
+
+ // making the same file again should have no effect.
+ made = mkdirp.sync(file);
+ t.equal(made, null);
+});

0 comments on commit 4fb9216

Please sign in to comment.