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

fs: implement fsp.exists #39968

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,17 @@ including subdirectories and files.
When copying a directory to another directory, globs are not supported and
behavior is similar to `cp dir1/ dir2/`.

### `fsPromises.exists(path)`
<!-- YAML
added: REPLACEME
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
-->

* `path` {string|Buffer|URL}
* Returns: {Promise} Resolved as `true` if the given path exists, otherwise,
resolved as `false`.

Test whether or not the given path exists by checking with the file system.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really check if the file exists, which is why the name is misleading, see #39960 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It makes sense. Do you have a better name suggestion? Or like os.path.exists on Python, is a sepecial note sufficient?


### `fsPromises.lchmod(path, mode)`
<!-- YAML
deprecated: v10.0.0
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,22 @@ async function readFile(path, options) {
return handleFdClose(readFileHandle(fd, options), fd.close);
}

async function exists(path) {
path = getValidatedPath(path, 'path');
try {
await access(path, F_OK);
return true;
} catch {
return false;
}
}

module.exports = {
exports: {
access,
copyFile,
cp,
exists,
open,
opendir: promisify(opendir),
rename,
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
chmod,
chown,
copyFile,
exists,
lchown,
link,
lchmod,
Expand Down Expand Up @@ -471,6 +472,27 @@ async function executeOnHandle(dest, func) {
});
});
}

{
const newFile = path.resolve(tmpDir, 'foo');
await writeFile(newFile, '');
const fileExists = await exists(newFile);
assert.strictEqual(fileExists, true);
await unlink(newFile);
}

{
const nonExistFile = path.resolve(tmpDir, 'non-exist-file');
const fileExists = await exists(nonExistFile);
assert.strictEqual(fileExists, false);
}

{
await assert.rejects(() => exists({}), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
}
}

doTest().then(common.mustCall());
Expand Down