Skip to content

Commit

Permalink
Merge 59a9604 into a557ff1
Browse files Browse the repository at this point in the history
  • Loading branch information
mix1 committed Feb 1, 2016
2 parents a557ff1 + 59a9604 commit 8f37790
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/filesystem.js
@@ -1,6 +1,24 @@
'use strict';

var fs = require('fs');
var exec = require('child_process').exec;

/**
* Asyncronous function to remove all files and (including non-empty) directories from path
*
* @param {String} path
* @param {Function} callback(err)
*/
module.exports.removeDirectoryContents = function (path, callback)
{
if (path.substr(-1, 1) !== '/') {
path += '/';
}

exec('rm -r ' + path + '*', function (err) {
callback(err);
});
};

/**
* Synchronous function for recursively walking through a directory and returning an array of files inside the directory
Expand Down
20 changes: 20 additions & 0 deletions test/filesystem.js
Expand Up @@ -25,4 +25,24 @@ describe('Filesystem', function () {
fs.rmdirSync('tmp');
});
});

describe('#removeDirectoryContents', function () {
it('Should remove all files and folders from directory', function () {
fs.mkdirSync('tmp');
fs.mkdirSync('tmp/test1');
fs.mkdirSync('tmp/test2');
fs.writeFileSync('tmp/test1/file.js', '');
fs.writeFileSync('tmp/test2/file.js', '');

filesystem.removeDirectoryContents('tmp/test1', function() {
assert.deepEqual(fs.readdirSync('tmp/test1').length, 0);

filesystem.removeDirectoryContents('tmp/', function() {
assert.deepEqual(fs.readdirSync('tmp').length, 0);

fs.rmdirSync('tmp');
});
});
});
});
});

0 comments on commit 8f37790

Please sign in to comment.