Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pathExists() #406

Merged
merged 1 commit into from Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
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
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -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
}