From 0e7d1b33f78cce09dedff4f747e2a3aa3e3d6935 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Thu, 13 Apr 2017 19:03:14 -0400 Subject: [PATCH] Add pathExists() and pathExistsSync() --- README.md | 2 + docs/pathExists-sync.md | 3 ++ docs/pathExists.md | 22 ++++++++++ lib/index.js | 1 + .../__tests__/path-exists-sync.test.js | 28 +++++++++++++ lib/path-exists/__tests__/path-exists.test.js | 40 +++++++++++++++++++ lib/path-exists/index.js | 12 ++++++ 7 files changed, 108 insertions(+) create mode 100644 docs/pathExists-sync.md create mode 100644 docs/pathExists.md create mode 100644 lib/path-exists/__tests__/path-exists-sync.test.js create mode 100644 lib/path-exists/__tests__/path-exists.test.js create mode 100644 lib/path-exists/index.js diff --git a/README.md b/README.md index 6a114e48..4e6e127e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/pathExists-sync.md b/docs/pathExists-sync.md new file mode 100644 index 00000000..3ef973c2 --- /dev/null +++ b/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). diff --git a/docs/pathExists.md b/docs/pathExists.md new file mode 100644 index 00000000..0302b09d --- /dev/null +++ b/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` `` +- `callback` `` + +## 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 +}) +``` diff --git a/lib/index.js b/lib/index.js index dd9fe2b2..cb7dd9e6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 diff --git a/lib/path-exists/__tests__/path-exists-sync.test.js b/lib/path-exists/__tests__/path-exists-sync.test.js new file mode 100644 index 00000000..dbde2b44 --- /dev/null +++ b/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)) + }) +}) diff --git a/lib/path-exists/__tests__/path-exists.test.js b/lib/path-exists/__tests__/path-exists.test.js new file mode 100644 index 00000000..abc1e634 --- /dev/null +++ b/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() + }) + }) +}) diff --git a/lib/path-exists/index.js b/lib/path-exists/index.js new file mode 100644 index 00000000..ddd9bc71 --- /dev/null +++ b/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 +}