Skip to content

Commit

Permalink
Async by default
Browse files Browse the repository at this point in the history
- now exports a sync() function for previous functionality
- is async by default
  • Loading branch information
scottcorgan committed Feb 7, 2017
1 parent 371df59 commit 135b040
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 37 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Check if filepath exists and is a file. Returns false for directories.

_(No requires node >=6.0.0)_

## Install

```
Expand All @@ -11,19 +13,27 @@ npm install file-exists --save
## Usage

```js
var fileExists = require('file-exists');
const fileExists = require('file-exists');

fileExists('/index.html', (err, exists) => console.log(exists)) // OUTPUTS: true or false

console.log(fileExists('/index.html')) // OUTPUTS: true or false
console.log(fileExists.sync('/index.html')) // OUTPUTS: true or false
```

### Options

#### fileExists(filepath[, options])
#### fileExists(filepath[, options, callback])

* `filepath` - the path to the file to check if it exists
* `options` - an object of options
* `root` - the root directory to look in (or cwd)

* `callback(err, exists)` - gets called when checking is done

#### fileExists.sync(filepath[, options])
* `filepath` - the path to the file to check if it exists
* `options` - an object of options
* `root` - the root directory to look in (or cwd)

## Run Tests

```
Expand Down
39 changes: 24 additions & 15 deletions index.js
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
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "file-exists",
"version": "2.0.0",
"version": "3.0.0",
"description": "Check if filepath exists and is a file",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "node test/index.js | tap-spec"
"test": "node test/index.js | tap-format-spec"
},
"repository": {
"type": "git",
Expand All @@ -25,11 +25,15 @@
"bugs": {
"url": "https://github.com/scottcorgan/file-exists/issues"
},
"engines": {
"node": ">=6.0.0"
},
"homepage": "https://github.com/scottcorgan/file-exists",
"devDependencies": {
"mkdirp": "^0.5.0",
"rmdir": "^1.0.4",
"tap-spec": "^4.1.0",
"tape": "^4.2.2"
"@tap-format/spec": "^0.2.0",
"async": "^2.1.4",
"mkdirp": "0.5.1",
"rmdir": "1.2.0",
"tape": "4.6.3"
}
}
51 changes: 39 additions & 12 deletions test/index.js
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())
})

0 comments on commit 135b040

Please sign in to comment.