Add pathExists() and pathExistsSync()

This commit is contained in:
RyanZim 2017-04-13 19:03:14 -04:00
parent 1613f205de
commit 3f7988de22
7 changed files with 108 additions and 0 deletions

View File

@ -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)
@ -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)

3
docs/pathExists-sync.md Normal file
View File

@ -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
docs/pathExists.md Normal file
View File

@ -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
})
```

View File

@ -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

View File

@ -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))
})
})

View File

@ -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
lib/path-exists/index.js Normal file
View File

@ -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
}