diff --git a/fs.js b/fs.js index 25ca524..6f47770 100644 --- a/fs.js +++ b/fs.js @@ -27,6 +27,17 @@ module.exports = { listTree: function listTree (directoryPath, filter) { return walk(directoryPath, filter, []) }, + + /** + * Custom implementation of [q-io/fs#list](http://documentup.com/kriskowal/q-io#listpath) + * to avoid dependency on q-io + * @param {string} directoryPath the base path + * @returns {Promise} a promise for the collector, that is fulfilled with a list of directory entries + */ + list: function list (directoryPath) { + return Q.ninvoke(fs, 'readdir', directoryPath) + }, + /** * Replacement for [q-io/fs#makeTree](http://documentup.com/kriskowal/q-io#maketreepath-mode) * @param {string} aPath the directory to be created @@ -35,9 +46,11 @@ module.exports = { makeTree: function makeTree (aPath, mode) { return Q.nfcall(require('mkdirp'), aPath, {mode: mode}) }, + removeTree: function removeTree (aPath) { return Q.nfcall(require('rimraf'), aPath) }, + /** * Replacement for [q-io/fs#read](http://documentup.com/kriskowal/q-io#readpath-options) * @param aPath diff --git a/test/fixtures/dir/file.txt b/test/fixtures/dir/file.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/dir/subdir/donotdelete.txt b/test/fixtures/dir/subdir/donotdelete.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/fs-spec.js b/test/fs-spec.js index ee8f136..b072321 100644 --- a/test/fs-spec.js +++ b/test/fs-spec.js @@ -23,14 +23,22 @@ chai.use(chaiAsPromised) chai.should() describe('m-io/fs', function () { - describe('the list-tree function', function () { - var sort = function (x) { - return x.sort() - } + var sort = function (x) { + return x.sort() + } + + describe('the list function', function () { + it('should list all entries in a directory', function () { + return mfs.list('test/fixtures/dir').then(sort).should.eventually.deep.equal([ + 'file.txt', + 'subdir' + ]) + }) + }) + describe('the list-tree function', function () { it('should return a file listing as array', function () { - return mfs.listTree('test/fixtures').then(sort).should.eventually.deep.equal([ - 'test/fixtures', + return mfs.listTree('test/fixtures/tree').then(sort).should.eventually.deep.equal([ 'test/fixtures/tree', 'test/fixtures/tree/a', 'test/fixtures/tree/a/b', @@ -44,7 +52,7 @@ describe('m-io/fs', function () { var filter = function (name, stat) { return stat.isFile() } - return mfs.listTree('test/fixtures', filter).then(sort).should.eventually.deep.equal([ + return mfs.listTree('test/fixtures/tree', filter).then(sort).should.eventually.deep.equal([ 'test/fixtures/tree/a/b/c.txt', 'test/fixtures/tree/a/bb/cc.txt' ]) @@ -54,8 +62,7 @@ describe('m-io/fs', function () { var filter = function (name, stat) { return name !== 'test/fixtures/tree/a/bb' } - return mfs.listTree('test/fixtures', filter).then(sort).should.eventually.deep.equal([ - 'test/fixtures', + return mfs.listTree('test/fixtures/tree', filter).then(sort).should.eventually.deep.equal([ 'test/fixtures/tree', 'test/fixtures/tree/a', 'test/fixtures/tree/a/b', @@ -68,8 +75,7 @@ describe('m-io/fs', function () { var filter = function (name, stat) { return name === 'test/fixtures/tree/a/bb' ? null : true } - return mfs.listTree('test/fixtures', filter).then(sort).should.eventually.deep.equal([ - 'test/fixtures', + return mfs.listTree('test/fixtures/tree', filter).then(sort).should.eventually.deep.equal([ 'test/fixtures/tree', 'test/fixtures/tree/a', 'test/fixtures/tree/a/b',