-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pathExists() and pathExistsSync()
- Loading branch information
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |