Skip to content
Browse files

Add mkdirp module

  • Loading branch information...
1 parent 7bb6f99 commit 1019818454d1359c09c9b85d580462b0f52d4183 @piscisaureus committed Mar 5, 2012
View
6 node_modules/mkdirp/README.markdown
@@ -10,7 +10,7 @@ pow.js
------
var mkdirp = require('mkdirp');
- mkdirp('/tmp/foo/bar/baz', 0755, function (err) {
+ mkdirp('/tmp/foo/bar/baz', function (err) {
if (err) console.error(err)
else console.log('pow!')
});
@@ -31,12 +31,16 @@ mkdirp(dir, mode, cb)
Create a new directory and any necessary subdirectories at `dir` with octal
permission string `mode`.
+If `mode` isn't specified, it defaults to `0777 & (~process.umask())`.
+
mkdirp.sync(dir, mode)
----------------------
Synchronously create a new directory and any necessary subdirectories at `dir`
with octal permission string `mode`.
+If `mode` isn't specified, it defaults to `0777 & (~process.umask())`.
+
install
=======
View
2 node_modules/mkdirp/examples/pow.js
@@ -1,6 +1,6 @@
var mkdirp = require('mkdirp');
-mkdirp('/tmp/foo/bar/baz', 0755, function (err) {
+mkdirp('/tmp/foo/bar/baz', function (err) {
if (err) console.error(err)
else console.log('pow!')
});
View
20 node_modules/mkdirp/index.js
@@ -4,7 +4,10 @@ var fs = require('fs');
module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
function mkdirP (p, mode, f) {
- if (mode === undefined) throw new Error('mode not specified');
+ if (typeof mode === 'function' || mode === undefined) {
+ f = mode;
+ mode = 0777 & (~process.umask());
+ }
var cb = f || function () {};
if (typeof mode === 'string') mode = parseInt(mode, 8);
@@ -25,7 +28,6 @@ function mkdirP (p, mode, f) {
// if the stat fails, then that's super weird.
// let the original EEXIST be the failure reason.
if (er2 || !stat.isDirectory()) cb(er)
- else if ((stat.mode & 0777) !== mode) fs.chmod(p, mode, cb);
else cb();
});
break;
@@ -38,7 +40,9 @@ function mkdirP (p, mode, f) {
}
mkdirP.sync = function sync (p, mode) {
- if (mode === undefined) throw new Error('mode not specified');
+ if (mode === undefined) {
+ mode = 0777 & (~process.umask());
+ }
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
@@ -63,16 +67,6 @@ mkdirP.sync = function sync (p, mode) {
throw err0
}
if (!stat.isDirectory()) throw err0;
- else if ((stat.mode & 0777) !== mode) {
- try {
- fs.chmodSync(p, mode);
- }
- catch (err) {
- if (err && err.code === 'EPERM') return null;
- else throw err;
- }
- return null;
- }
else return null;
break;
default :
View
2 node_modules/mkdirp/package.json
@@ -1,7 +1,7 @@
{
"name" : "mkdirp",
"description" : "Recursively mkdir, like `mkdir -p`",
- "version" : "0.2.1",
+ "version" : "0.3.0",
"author" : "James Halliday <mail@substack.net> (http://substack.net)",
"main" : "./index",
"keywords" : [
View
1 node_modules/mkdirp/test/chmod.js
@@ -32,7 +32,6 @@ test('chmod', function (t) {
fs.stat(file, function (er, stat) {
t.ifError(er, 'should exist');
t.ok(stat && stat.isDirectory(), 'should be directory');
- t.equal(stat && stat.mode & 0777, mode, 'should be 0755');
t.end();
});
});
View
28 node_modules/mkdirp/test/umask.js
@@ -0,0 +1,28 @@
+var mkdirp = require('../');
+var path = require('path');
+var fs = require('fs');
+var test = require('tap').test;
+
+test('implicit mode from umask', 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('/');
+
+ mkdirp(file, function (err) {
+ if (err) t.fail(err);
+ else path.exists(file, function (ex) {
+ if (!ex) t.fail('file not created')
+ else fs.stat(file, function (err, stat) {
+ if (err) t.fail(err)
+ else {
+ t.equal(stat.mode & 0777, 0777 & (~process.umask()));
+ t.ok(stat.isDirectory(), 'target not a directory');
+ t.end();
+ }
+ })
+ })
+ });
+});
View
27 node_modules/mkdirp/test/umask_sync.js
@@ -0,0 +1,27 @@
+var mkdirp = require('../');
+var path = require('path');
+var fs = require('fs');
+var test = require('tap').test;
+
+test('umask sync modes', 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('/');
+
+ var err = mkdirp.sync(file);
+ if (err) t.fail(err);
+ else path.exists(file, function (ex) {
+ if (!ex) t.fail('file not created')
+ else fs.stat(file, function (err, stat) {
+ if (err) t.fail(err)
+ else {
+ t.equal(stat.mode & 0777, (0777 & (~process.umask())));
+ t.ok(stat.isDirectory(), 'target not a directory');
+ t.end();
+ }
+ })
+ })
+});

0 comments on commit 1019818

Please sign in to comment.
Something went wrong with that request. Please try again.