Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a limit option to mkdirs() #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ fs.createFile(file, function(err) {



### mkdirs(dir, callback)
### mkdirs(dir, [limit], callback)

Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`.
Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`. If `limit` is provided, `mkdirs` will create at most the specified number of parent directories (0 or more.)

Alias: `mkdirp()`
Alias: `mkdirp()` (but no "`limit`" support)

Sync: `mkdirsSync()` / `mkdirpSync()`


Examples:

Equivalent to mkdir -p
```javascript
var fs = require('fs-extra');

Expand All @@ -121,6 +122,39 @@ fs.mkdirs('/tmp/some/long/path/that/prob/doesnt/exist', function(err){
fs.mkdirsSync('/tmp/another/path');
```

Using the limit:
```javascript
var fs = require('fs-extra');

var root = path.join('usr', 'local', 'lib'); // forgot the initial '/'
var outdir = path.join(root, 'mylib', 'base');
// Okay to make 'mylib', but fail if we try to recreate the root
fs.mkdirs(outdir, 1, function(err) {
if (err) {
console.error(err);
}
else {
console.log('success!');
}
});
```

Concise logic to ensure a directory exists
```javascript
var fs = require('fs-extra');

fs.mkdirsSync('mydir', 0);

// Equivalent logic:
try {
fs.mkdirSync('mydir');
} catch (err) {
if (err !== 'EEXIST') {
throw err;
}
}
```


### outputFile(file, data, callback)

Expand Down Expand Up @@ -254,6 +288,7 @@ Contributors
- [JP Richardson](https://github.com/jprichardson)
- [Mike McNeil](https://github.com/mikermcneil)
- [Ian Crowther](https://github.com/iancrowther)
- [Joshua Richardson](https://github.com/jric)
- `<your name here>`


Expand Down
53 changes: 51 additions & 2 deletions lib/mkdir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var R = path.resolve;

module.exports.mkdirs = mkdirp;
module.exports.mkdirsSync = mkdirp.sync;
// mkdirs(dir, [limit], cb)
// limit: maximum number of parent directories to make
var mkdirs = function (dir, cb) {
var limit = -1;
if (arguments.length > 2) {
limit = cb;
cb = arguments[2];
}
if (limit < 0 || limit === null) { mkdirp(dir, cb); }
else {
var components = R(dir).split(path.sep);
if (components.length && components[components.length - 1] === '')
{ components.pop(); }
var parent_must_exist = components.slice(0, components.length-limit-1).join(path.sep);
if (parent_must_exist == '') { mkdirp(dir, cb); }
else {
fs.stat(parent_must_exist, function (err, st) {
if (err) { cb(err); }
else if (!st.isDirectory())
{ cb(new Error('not a directory: ' + parent_must_exist)); }
else { mkdirp(dir, cb); }
});
}
}
}

// mkdirsSync(dir, [limit])
var mkdirsSync = function (dir) {
var limit = -1;
if (arguments.length > 1) { limit = arguments[1]; }
if (limit < 0 || limit === null) { mkdirp.sync(dir); }
else {
var components = R(dir).split(path.sep);
if (components.length && components[components.length - 1] === '')
{ components.pop(); }
var parent_must_exist = components.slice(0, components.length-limit-1).join(path.sep);
if (parent_must_exist == '') { mkdirp.sync(dir); }
else {
var st = fs.statSync(parent_must_exist);
if (!st.isDirectory())
{ throw new Error('not a directory: ' + parent_must_exist); }
mkdirp.sync(dir);
}
}
}

module.exports.mkdirs = mkdirs;
module.exports.mkdirsSync = mkdirsSync;


107 changes: 104 additions & 3 deletions test/mkdir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,67 @@ var fs = require('../lib')

var TEST_DIR = ''

var testMkdirsWithLimit = function(limit, should_pass, done) {
var newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa');
// should work the same way with dangling seperators
var another = path.join(TEST_DIR, 'dfdf', 'ffff', 'bbb', '');

F (fs.existsSync(newDir));
F (fs.existsSync(another));

fs.mkdirs(newDir, limit, function(err) {
if (should_pass) {
if (err !== null) { throw new Error('unexpected error: ' + err); }
T (fs.existsSync(newDir));
} else {
F (err === null);
F (fs.existsSync(newDir));
}
fs.mkdirs(another, limit, function(err) {
if (should_pass) {
if (err !== null) { throw new Error('unexpected error: ' + err); }
T (fs.existsSync(another));
} else {
F (err === null);
F (fs.existsSync(another));
}
done();
});
});
};

var testMkdirsSyncWithLimit = function(limit, should_pass, done) {
var newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa');
// should work the same way with dangling seperators
var another = path.join(TEST_DIR, 'dfdf', 'ffff', 'bbb', '');

F (fs.existsSync(newDir));
F (fs.existsSync(another));

try {
fs.mkdirsSync(newDir, limit);
} catch (err) {
if (should_pass) { throw new Error('unexpected error: ' + err); }
}
if (should_pass) {
T (fs.existsSync(newDir));
} else {
F (fs.existsSync(newDir));
}
try {
fs.mkdirsSync(another, limit);
} catch (err) {
if (should_pass) { throw new Error('unexpected error: ' + err); }
}
if (should_pass) {
T (fs.existsSync(another));
} else {
F (fs.existsSync(another));
}

done();
}

describe('fs-extra', function() {
beforeEach(function() {
TEST_DIR = testutil.createTestDir('fs-extra')
Expand All @@ -24,10 +85,9 @@ describe('fs-extra', function() {
})

it('should make the entire directory path', function(done) {
var dir = path.join(path.tempdir(), 'tmp-' + Date.now() + Math.random())
, newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa');
var newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa');

F (fs.existsSync(dir));
F (fs.existsSync(newDir));

fs.mkdirs(newDir, function(err) {
T (err === null);
Expand All @@ -36,6 +96,27 @@ describe('fs-extra', function() {
done();
});
})

it('should make the entire directory path with limit 2', function(done) {
testMkdirsWithLimit(2, true, done);
})

it('should make the entire directory path with limit -1', function(done) {
testMkdirsWithLimit(-1, true, done);
})

it('should make the entire directory path with limit null', function(done) {
testMkdirsWithLimit(null, true, done);
})

it('should refuse to make the entire directory path with limit 0', function(done) {
testMkdirsWithLimit(0, false, done);
})

it('should refuse to make the entire directory path with limit 1', function(done) {
testMkdirsWithLimit(1, false, done);
})

})

describe('+ mkdirsSync()', function() {
Expand All @@ -59,6 +140,26 @@ describe('fs-extra', function() {

done();
})

it('should make the entire directory path with limit 2', function(done) {
testMkdirsSyncWithLimit(2, true, done);
})

it('should make the entire directory path with limit -1', function(done) {
testMkdirsSyncWithLimit(-1, true, done);
})

it('should make the entire directory path with limit null', function(done) {
testMkdirsSyncWithLimit(null, true, done);
})

it('should refuse to make the entire directory path with limit 0', function(done) {
testMkdirsSyncWithLimit(0, false, done);
})

it('should refuse to make the entire directory path with limit 1', function(done) {
testMkdirsSyncWithLimit(1, false, done);
})
})

})
Expand Down