-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- now exports a sync() function for previous functionality - is async by default
- Loading branch information
1 parent
371df59
commit 135b040
Showing
4 changed files
with
87 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
var fs = require('fs') | ||
var path = require('path') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
module.exports = function (filepath, options) { | ||
options = options || {} | ||
|
||
if (!filepath) return false | ||
function fileExits (filepath, options, done = function () {}) { | ||
if (typeof options === 'function') { | ||
done = options | ||
options = {} | ||
} | ||
|
||
var root = options.root | ||
var fullpath = (root) ? path.join(root, filepath) : filepath | ||
fs.stat(fullPath(filepath, options), (err, stats) => done(err, stats.isFile())) | ||
} | ||
|
||
fileExits.sync = function fileExistsSync (filepath = '', options = {}) { | ||
try { | ||
return fs.statSync(fullpath).isFile(); | ||
return fs.statSync(fullPath(filepath, options)).isFile() | ||
} | ||
catch (e) { | ||
|
||
// Check exception. If ENOENT - no such file or directory ok, file doesn't exist. | ||
// Check exception. If ENOENT - no such file or directory ok, file doesn't exist. | ||
// Otherwise something else went wrong, we don't have rights to access the file, ... | ||
if (e.code != 'ENOENT') | ||
throw e; | ||
|
||
return false; | ||
if (e.code != 'ENOENT') { | ||
throw e | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
function fullPath (filepath, options = {}) { | ||
const root = options.root | ||
return (root) ? path.join(root, filepath) : filepath | ||
} | ||
|
||
module.exports = fileExits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,45 @@ | ||
var fileExists = require('../') | ||
var test = require('tape') | ||
var fs = require('fs') | ||
var mkdirp = require('mkdirp') | ||
var rmdir = require('rmdir') | ||
const fileExists = require('../') | ||
const test = require('tape') | ||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const rmdir = require('rmdir') | ||
const async = require('async') | ||
|
||
test('file exists', function (t) { | ||
test('async', t => { | ||
mkdirp.sync('.tmp') | ||
fs.writeFileSync('.tmp/index.html', 'test', 'utf8') | ||
|
||
t.ok(fileExists('.tmp/index.html'), 'file does exist') | ||
t.ok(fileExists('/index.html', {root: '.tmp'}), 'file exists in given root directory') | ||
t.notOk(fileExists('.tmp'), 'directory is not a file') | ||
|
||
rmdir('.tmp', function () { | ||
t.end() | ||
async.parallel([ | ||
done => { | ||
fileExists('.tmp/index.html', (err, exists) => { | ||
t.ok(exists, 'file does exist') | ||
done() | ||
}) | ||
}, | ||
done => { | ||
fileExists('/index.html', {root: '.tmp'}, (err, exists) => { | ||
t.ok(exists, 'file exists in given root directory') | ||
done() | ||
}) | ||
}, | ||
done => { | ||
fileExists('.tmp', (err, exists) => { | ||
t.notOk(exists, 'directory is not a file') | ||
done() | ||
}) | ||
} | ||
], err => { | ||
rmdir('.tmp', () => t.end()) | ||
}) | ||
}) | ||
|
||
test('sync', t => { | ||
mkdirp.sync('.tmp') | ||
fs.writeFileSync('.tmp/index.html', 'test', 'utf8') | ||
|
||
t.ok(fileExists.sync('.tmp/index.html'), 'file does exist') | ||
t.ok(fileExists.sync('/index.html', {root: '.tmp'}), 'file exists in given root directory') | ||
t.notOk(fileExists.sync('.tmp'), 'directory is not a file') | ||
|
||
rmdir('.tmp', () => t.end()) | ||
}) |