[Pkg-javascript-commits] [SCM] NodeJS library to recursively create directories branch, upstream, updated. upstream/0.3.0-1-ge0f65c0
David Paleino
dapal at debian.org
Sun Apr 8 20:45:07 UTC 2012
The following commit has been merged in the upstream branch:
commit e0f65c00f2cd4f97167772d3f9d9881735266808
Author: David Paleino <dapal at debian.org>
Date: Sun Apr 8 22:34:24 2012 +0200
Imported Upstream version 0.3.1
diff --git a/.gitignore b/.npmignore
similarity index 100%
rename from .gitignore
rename to .npmignore
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..f1d0f13
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
diff --git a/README.markdown b/README.markdown
index b4dd75f..40de04f 100644
--- a/README.markdown
+++ b/README.markdown
@@ -3,6 +3,8 @@ mkdirp
Like `mkdir -p`, but in node.js!
+[![build status](https://secure.travis-ci.org/substack/node-mkdirp.png)](http://travis-ci.org/substack/node-mkdirp)
+
example
=======
@@ -33,6 +35,9 @@ permission string `mode`.
If `mode` isn't specified, it defaults to `0777 & (~process.umask())`.
+`cb(err, made)` fires with the error or the first directory `made`
+that had to be created, if any.
+
mkdirp.sync(dir, mode)
----------------------
@@ -41,6 +46,8 @@ with octal permission string `mode`.
If `mode` isn't specified, it defaults to `0777 & (~process.umask())`.
+Returns the first directory that had to be created, if any.
+
install
=======
diff --git a/index.js b/index.js
index 25f43ad..6d81c62 100644
--- a/index.js
+++ b/index.js
@@ -3,23 +3,27 @@ var fs = require('fs');
module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
-function mkdirP (p, mode, f) {
+function mkdirP (p, mode, f, made) {
if (typeof mode === 'function' || mode === undefined) {
f = mode;
mode = 0777 & (~process.umask());
}
+ if (!made) made = 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;
@@ -27,53 +31,53 @@ function mkdirP (p, mode, f) {
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;
}
});
}
-mkdirP.sync = function sync (p, mode) {
+mkdirP.sync = function sync (p, mode, made) {
if (mode === undefined) {
mode = 0777 & (~process.umask());
}
+ if (!made) made = null;
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
-
+
try {
- fs.mkdirSync(p, mode)
+ fs.mkdirSync(p, mode);
+ made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
- var err1 = sync(path.dirname(p), mode)
- if (err1) throw err1;
- else return sync(p, mode);
+ made = sync(path.dirname(p), mode, made);
+ sync(p, mode, made);
break;
-
+
case 'EEXIST' :
var stat;
try {
stat = fs.statSync(p);
}
catch (err1) {
- throw err0
+ throw err0;
}
if (!stat.isDirectory()) throw err0;
- else return null;
break;
default :
throw err0
break;
}
}
-
- return null;
+
+ return made;
};
diff --git a/package.json b/package.json
index 1bf9ac7..2e95490 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name" : "mkdirp",
"description" : "Recursively mkdir, like `mkdir -p`",
- "version" : "0.3.0",
+ "version" : "0.3.1",
"author" : "James Halliday <mail at substack.net> (http://substack.net)",
"main" : "./index",
"keywords" : [
@@ -16,7 +16,7 @@
"test" : "tap test/*.js"
},
"devDependencies" : {
- "tap" : "0.0.x"
+ "tap" : "~0.2.4"
},
"license" : "MIT/X11",
"engines": { "node": "*" }
diff --git a/test/return.js b/test/return.js
new file mode 100644
index 0000000..bce68e5
--- /dev/null
+++ b/test/return.js
@@ -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);
+ });
+ });
+});
diff --git a/test/return_sync.js b/test/return_sync.js
new file mode 100644
index 0000000..7c222d3
--- /dev/null
+++ b/test/return_sync.js
@@ -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);
+});
diff --git a/test/sync.js b/test/sync.js
index e0e389d..7530cad 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -8,12 +8,17 @@ test('sync', function (t) {
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('/');
-
- var err = mkdirp.sync(file, 0755);
- if (err) t.fail(err);
- else path.exists(file, function (ex) {
+
+ try {
+ mkdirp.sync(file, 0755);
+ } catch (err) {
+ t.fail(err);
+ return t.end();
+ }
+
+ path.exists(file, function (ex) {
if (!ex) t.fail('file not created')
else fs.stat(file, function (err, stat) {
if (err) t.fail(err)
@@ -22,6 +27,6 @@ test('sync', function (t) {
t.ok(stat.isDirectory(), 'target not a directory');
t.end();
}
- })
- })
+ });
+ });
});
diff --git a/test/umask_sync.js b/test/umask_sync.js
index 83cba56..4bd5376 100644
--- a/test/umask_sync.js
+++ b/test/umask_sync.js
@@ -8,12 +8,17 @@ test('umask sync modes', function (t) {
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('/');
-
- var err = mkdirp.sync(file);
- if (err) t.fail(err);
- else path.exists(file, function (ex) {
+
+ try {
+ mkdirp.sync(file, 0755);
+ } catch (err) {
+ t.fail(err);
+ return t.end();
+ }
+
+ path.exists(file, function (ex) {
if (!ex) t.fail('file not created')
else fs.stat(file, function (err, stat) {
if (err) t.fail(err)
@@ -22,6 +27,6 @@ test('umask sync modes', function (t) {
t.ok(stat.isDirectory(), 'target not a directory');
t.end();
}
- })
- })
+ });
+ });
});
--
NodeJS library to recursively create directories
More information about the Pkg-javascript-commits
mailing list