Skip to content

Commit

Permalink
Add "exists" function to "fs" module
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Dec 19, 2016
1 parent 05bce3b commit 8618a38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ var path = require('path')
* @module
*/
module.exports = {
/**
* Custom implementation of [q-io/fs#exists](http://documentup.com/kriskowal/q-io#lexistsPath)
* to avoid dependency on q-io
* @param {string} existsPath the path to check
* @returns {Promise<boolean>} a promise for the existance (true/false) of the file/dir at the path
*/
exists: function list (existsPath) {
return new Q.Promise(function (resolve, reject) {
fs.access(existsPath, function (err) {
resolve(!err)
})
})
},

/**
* Custom implementation of [q-io/fs#listTree](http://documentup.com/kriskowal/q-io#listtreepath-guardpath-stat)
* to avoid dependency on q-io
Expand Down
14 changes: 14 additions & 0 deletions test/fs-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ describe('m-io/fs', function () {
return x.sort()
}

describe('the exists function', function () {
it('should return true if dir path exists', function () {
return mfs.exists('test/fixtures/dir').should.eventually.equal(true)
})

it('should return true if file path exists', function () {
return mfs.exists('test/fixtures/dir/file.txt').should.eventually.equal(true)
})

it('should return false if dir path does not exist', function () {
return mfs.exists('test/fixtures/no-dir-or-file').should.eventually.equal(false)
})
})

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([
Expand Down

0 comments on commit 8618a38

Please sign in to comment.