Don't not send an error on isFile, even when the file doesn't exists

This commit is contained in:
Juan Cruz Viotti 2014-11-07 12:09:43 -04:00
parent 89bb0d0b21
commit afe438ff82
2 changed files with 6 additions and 8 deletions

View File

@ -5,25 +5,23 @@ async = require('async')
exports.isValidPath = (p) -> exports.isValidPath = (p) ->
return _.isString(p) return _.isString(p)
exports.isFile = (file, callback) -> exports.isFile = (file, outerCallback) ->
async.waterfall([ async.waterfall([
# Check if the file exists # Check if the file exists
(callback) -> (callback) ->
fs.exists file, (exists) -> fs.exists file, (exists) ->
if not exists
# Return false anyway to avoid confusions
return callback(new Error('File doesn\'t exists'), false)
return callback(null, exists) return callback(null, exists)
# Get file stats # Get file stats
(exists, callback) -> (exists, callback) ->
if not exists
return outerCallback(null, false)
fs.stat(file, callback) fs.stat(file, callback)
# Check if it's a file # Check if it's a file
(stats, callback) -> (stats, callback) ->
return callback(null, !!stats.isFile()) return callback(null, !!stats.isFile())
], callback) ], outerCallback)

View File

@ -56,7 +56,7 @@ describe 'FsUtils', ->
it 'should return false if file doesn\'t exists', (done) -> it 'should return false if file doesn\'t exists', (done) ->
fsUtils.isFile '/nonexistentfile', (error, isFile) -> fsUtils.isFile '/nonexistentfile', (error, isFile) ->
expect(error).to.exist expect(error).to.not.exist
expect(isFile).to.be.false expect(isFile).to.be.false
done() done()