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

including new emptydir module #74

Closed
wants to merge 10 commits into from
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ fs.writeJson('./package.json', {name: 'fs-extra'}, function(err){
});
```

### emptyDir(path[, callback])

Recursively removes files in a directory while leaving the directory tree intact.

Example:

```javascript
var fs = require('fs-extra');
/*
* errs will be an array with all the errors that occurred
* Otherwise null on success
*/
fs.emptyDir('./my_site', function (errs) {
if (errs) {
errs.forEach(function (err) {
console.log(err.path + " failed to be deleted due to " + err.code);
});
return;
}
console.log("All files removed successfully");
});
```
More details for [emptydir](https://github.com/GochoMugo/emptydir "Project repo")...


Roadmap
Expand Down Expand Up @@ -343,6 +366,7 @@ If you want to contribute, please add a test. Also, don't change the version in
- [1] [Jim Higson](https://github.com/jimhigson)
- [1] [PatrickJS](https://github.com/gdi2290)
- [1] [Michael Tiller](https://github.com/xogeny)
- [1] [Gocho Mugo](https://github.com/GochoMugo)
- `<your name here>`


Expand Down
7 changes: 7 additions & 0 deletions lib/emptydir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict"

var emptydir = require('emptydir');

module.exports.emptyDir = emptydir.emptyDir
module.exports.emptyDirs = emptydir.emptyDirs
module.exports.emptyDirsSync = emptydir.emptyDirsSync
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ fs.writeJSON = jsonFile.writeFile;
fs.writeJsonSync = jsonFile.writeFileSync;
fs.writeJSONSync = jsonFile.writeFileSync;

// emptydir
var emptydir = require('./emptydir');
fs.emptyDir = emptydir.emptyDir;
fs.emptyDirs = emptydir.emptyDirs;
fs.emptyDirsSync = emptydir.emptyDirsSync;


module.exports = fs

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"create",
"text",
"output",
"move"
"move",
"empty",
"emptydir"
],
"author": "JP Richardson <[email protected]>",
"licenses": [
Expand All @@ -41,7 +43,8 @@
"ncp": "^0.5.1",
"mkdirp": "^0.5.0",
"jsonfile": "^1.2.0",
"rimraf": "^2.2.8"
"rimraf": "^2.2.8",
"emptydir": "0.0.0"
},
"devDependencies": {
"mocha": "*",
Expand Down
105 changes: 105 additions & 0 deletions test/emptydir.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
var assert = require('assert'),
fs = require('fs'),
emptydir = require('emptydir');


var test_folder = './test';
var dirs = [
test_folder,
test_folder + '/dirA',
test_folder + '/dirB',
test_folder + '/dirB/dirC'
];
dirs.forEach(function (dir) {
if (! fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
});

// Recreate the files
var original_file = './try_file';
file = fs.openSync(original_file, 'w');
fs.closeSync(file);
var files = [
test_folder + '/file1',
test_folder + '/dirA/file2',
test_folder + '/dirB/dirC/file3'
];
beforeEach(function () {
files.forEach(function (file) {
fs.linkSync(original_file, file);
});
});


/*Creates a super folder that wiill raise excepton if tried to be deleted from*/
var super_folder = './delete_me';
if (!fs.existsSync(super_folder)) fs.mkdirSync(super_folder, 000);

describe('emptyDir', function () {
it('should call callback once with err as null', function (done) {
var calls = 0;
emptydir.emptyDir(test_folder, function (errs) {
calls++;
assert.equal(1, calls, 'called more than once');
assert.equal(null, errs, 'some unexpected error occurred');
done();
});
});
it('should pass an array of errs', function (done) {
emptydir.emptyDir(super_folder, function (errs) {
assert.ok(errs.length, 'error failed to be passed');
done();
});
});
});


describe('emptyDirs', function () {
it('should be called per file and always pass path, no error should be passed to callback here', function (done) {
var calls = 0;
emptydir.emptyDirs(test_folder, function(err, path) {
calls++;
assert.equal(null, err, 'some error was passed');
assert(path, 'path not passed to callback');
if (calls === 3) done();
});
});
it('should get an err', function (done) {
emptydir.emptyDirs(super_folder, function (err, path) {
assert(err, 'Error did not get passed');
done();
});
});
});


describe('emptyDirsSync', function () {
it('should return an empty array here', function () {
errs = emptydir.emptyDirsSync(test_folder);
assert.equal(0, errs.length);
});
it('should return an array with one error', function () {
errs = emptydir.emptyDirsSync(super_folder);
assert.equal(1, errs.length);
});
});


// Destroy all files
afterEach(function () {
files.forEach(function (file) {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
});
});

// Destroy all Direcotories
after(function () {
for (var i = dirs.length - 1; i <= 0; i--) {
if (fs.existsSync(dirs[i])) {
fs.rmdirSync(dirs[i]);
}
}
});