Skip to content

Commit

Permalink
Add pathExists() and pathExistsSync()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Apr 24, 2017
1 parent 33b7f84 commit 0e7d1b3
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Methods
- [move](docs/move.md)
- [outputFile](docs/outputFile.md)
- [outputJson](docs/outputJson.md)
- [pathExists](docs/pathExists.md)
- [readJson](docs/readJson.md)
- [remove](docs/remove.md)
- [writeJson](docs/writeJson.md)
Expand All @@ -123,6 +124,7 @@ Methods
- [moveSync](docs/move-sync.md)
- [outputFileSync](docs/outputFile-sync.md)
- [outputJsonSync](docs/outputJson-sync.md)
- [pathExistsSync](docs/pathExists-sync.md)
- [readJsonSync](docs/readJson-sync.md)
- [removeSync](docs/remove-sync.md)
- [writeJsonSync](docs/writeJson-sync.md)
Expand Down
3 changes: 3 additions & 0 deletions docs/pathExists-sync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pathExistsSync(file)

An alias for [`fs.existsSync()`](https://nodejs.org/api/fs.html#fs_fs_existssync_path), created for consistency with [`pathExists()`](pathExists.md).
22 changes: 22 additions & 0 deletions docs/pathExists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# pathExists(file[, callback])

Test whether or not the given path exists by checking with the file system. Like [`fs.exists`](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback), but with a normal callback signature (err, exists). Uses `fs.access` under the hood.

- `file` `<String>`
- `callback` `<Function>`

## Example:

```js
const fs = require('fs-extra')

const file = '/tmp/this/path/does/not/exist/file.txt'
// Promise usage:
fs.pathExists(file)
.then(exists => console.log(exists)) // => false
// Callback usage:
fs.pathExists(file, (err, exists) => {
console.log(err) // => null
console.log(exists) // => false
})
```
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ assign(fs, require('./move-sync'))
assign(fs, require('./empty'))
assign(fs, require('./ensure'))
assign(fs, require('./output'))
assign(fs, require('./path-exists'))

module.exports = fs
28 changes: 28 additions & 0 deletions lib/path-exists/__tests__/path-exists-sync.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'
/* eslint-env mocha */

const fs = require(process.cwd())
const path = require('path')
const os = require('os')
const assert = require('assert')

describe('pathExists()', () => {
let TEST_DIR

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'path-exists')
fs.emptyDir(TEST_DIR, done)
})

afterEach(done => fs.remove(TEST_DIR, done))

it('should return false if file does not exist', () => {
assert(!fs.pathExistsSync(path.join(TEST_DIR, 'somefile')))
})

it('should return true if file does exist', () => {
const file = path.join(TEST_DIR, 'exists')
fs.ensureFileSync(file)
assert(fs.pathExistsSync(file))
})
})
40 changes: 40 additions & 0 deletions lib/path-exists/__tests__/path-exists.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'
/* eslint-env mocha */

const fs = require(process.cwd())
const path = require('path')
const os = require('os')
const assert = require('assert')

describe('pathExists()', () => {
let TEST_DIR

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'path-exists')
fs.emptyDir(TEST_DIR, done)
})

afterEach(done => fs.remove(TEST_DIR, done))

it('should return false if file does not exist', () => {
return fs.pathExists(path.join(TEST_DIR, 'somefile'))
.then(exists => assert(!exists))
})

it('should return true if file does exist', () => {
const file = path.join(TEST_DIR, 'exists')
fs.ensureFileSync(file)
return fs.pathExists(file)
.then(exists => assert(exists))
})

it('should pass an empty error parameter to the callback', done => {
const file = path.join(TEST_DIR, 'exists')
fs.ensureFileSync(file)
fs.pathExists(file, (err, exists) => {
assert.ifError(err)
assert(exists)
done()
})
})
})
12 changes: 12 additions & 0 deletions lib/path-exists/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
const u = require('universalify').fromPromise
const fs = require('../fs')

function pathExists (path) {
return fs.access(path).then(() => true).catch(() => false)
}

module.exports = {
pathExists: u(pathExists),
pathExistsSync: fs.existsSync
}

0 comments on commit 0e7d1b3

Please sign in to comment.