Skip to content

Commit 3f7988d

Browse files
committed
Add pathExists() and pathExistsSync()
1 parent 1613f20 commit 3f7988d

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Methods
107107
- [move](docs/move.md)
108108
- [outputFile](docs/outputFile.md)
109109
- [outputJson](docs/outputJson.md)
110+
- [pathExists](docs/pathExists.md)
110111
- [readJson](docs/readJson.md)
111112
- [remove](docs/remove.md)
112113
- [writeJson](docs/writeJson.md)
@@ -123,6 +124,7 @@ Methods
123124
- [moveSync](docs/move-sync.md)
124125
- [outputFileSync](docs/outputFile-sync.md)
125126
- [outputJsonSync](docs/outputJson-sync.md)
127+
- [pathExistsSync](docs/pathExists-sync.md)
126128
- [readJsonSync](docs/readJson-sync.md)
127129
- [removeSync](docs/remove-sync.md)
128130
- [writeJsonSync](docs/writeJson-sync.md)

docs/pathExists-sync.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# pathExistsSync(file)
2+
3+
An alias for [`fs.existsSync()`](https://nodejs.org/api/fs.html#fs_fs_existssync_path), created for consistency with [`pathExists()`](pathExists.md).

docs/pathExists.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# pathExists(file[, callback])
2+
3+
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.
4+
5+
- `file` `<String>`
6+
- `callback` `<Function>`
7+
8+
## Example:
9+
10+
```js
11+
const fs = require('fs-extra')
12+
13+
const file = '/tmp/this/path/does/not/exist/file.txt'
14+
// Promise usage:
15+
fs.pathExists(file)
16+
.then(exists => console.log(exists)) // => false
17+
// Callback usage:
18+
fs.pathExists(file, (err, exists) => {
19+
console.log(err) // => null
20+
console.log(exists) // => false
21+
})
22+
```

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ assign(fs, require('./move-sync'))
1717
assign(fs, require('./empty'))
1818
assign(fs, require('./ensure'))
1919
assign(fs, require('./output'))
20+
assign(fs, require('./path-exists'))
2021

2122
module.exports = fs
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const fs = require(process.cwd())
5+
const path = require('path')
6+
const os = require('os')
7+
const assert = require('assert')
8+
9+
describe('pathExists()', () => {
10+
let TEST_DIR
11+
12+
beforeEach(done => {
13+
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'path-exists')
14+
fs.emptyDir(TEST_DIR, done)
15+
})
16+
17+
afterEach(done => fs.remove(TEST_DIR, done))
18+
19+
it('should return false if file does not exist', () => {
20+
assert(!fs.pathExistsSync(path.join(TEST_DIR, 'somefile')))
21+
})
22+
23+
it('should return true if file does exist', () => {
24+
const file = path.join(TEST_DIR, 'exists')
25+
fs.ensureFileSync(file)
26+
assert(fs.pathExistsSync(file))
27+
})
28+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const fs = require(process.cwd())
5+
const path = require('path')
6+
const os = require('os')
7+
const assert = require('assert')
8+
9+
describe('pathExists()', () => {
10+
let TEST_DIR
11+
12+
beforeEach(done => {
13+
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'path-exists')
14+
fs.emptyDir(TEST_DIR, done)
15+
})
16+
17+
afterEach(done => fs.remove(TEST_DIR, done))
18+
19+
it('should return false if file does not exist', () => {
20+
return fs.pathExists(path.join(TEST_DIR, 'somefile'))
21+
.then(exists => assert(!exists))
22+
})
23+
24+
it('should return true if file does exist', () => {
25+
const file = path.join(TEST_DIR, 'exists')
26+
fs.ensureFileSync(file)
27+
return fs.pathExists(file)
28+
.then(exists => assert(exists))
29+
})
30+
31+
it('should pass an empty error parameter to the callback', done => {
32+
const file = path.join(TEST_DIR, 'exists')
33+
fs.ensureFileSync(file)
34+
fs.pathExists(file, (err, exists) => {
35+
assert.ifError(err)
36+
assert(exists)
37+
done()
38+
})
39+
})
40+
})

lib/path-exists/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
const u = require('universalify').fromPromise
3+
const fs = require('../fs')
4+
5+
function pathExists (path) {
6+
return fs.access(path).then(() => true).catch(() => false)
7+
}
8+
9+
module.exports = {
10+
pathExists: u(pathExists),
11+
pathExistsSync: fs.existsSync
12+
}

0 commit comments

Comments
 (0)