Skip to content

Commit

Permalink
Add "list" function to "fs.js" module
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Dec 19, 2016
1 parent 48a6e81 commit 3014ec6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
13 changes: 13 additions & 0 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>} 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
Expand All @@ -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
Expand Down
Empty file added test/fixtures/dir/file.txt
Empty file.
Empty file.
28 changes: 17 additions & 11 deletions test/fs-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
])
Expand All @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 3014ec6

Please sign in to comment.