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

added few more examples to fs.access. Fixes #17508 #17578

Closed
wants to merge 3 commits into from
Closed
Changes from all 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
51 changes: 47 additions & 4 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,55 @@ no effect on Windows (will behave like `fs.constants.F_OK`).

The final argument, `callback`, is a callback function that is invoked with
a possible error argument. If any of the accessibility checks fail, the error
argument will be populated. The following example checks if the file
`/etc/passwd` can be read and written by the current process.
argument will be populated.

```js
fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
console.log(err ? 'no access!' : 'can read/write');
// Check if `package.json` exists in the current directory.
fs.access('./package.json', fs.constants.F_OK, (err) => {
console.log(err ? 'No package.json file!' : 'package.json exists!');
});
```

```js
Copy link
Member

Choose a reason for hiding this comment

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

Since this is all about examples, we could actually use a single JS block that includes all the different examples. But that might just be my personal preference.

// Check if we can read `package.json` file.
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid using informal pronouns like "we" in the docs

Copy link
Member

Choose a reason for hiding this comment

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

...so maybe Check if package.json file can be read or something similar to that...

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @jasnell and @Trott, I'm learning a lot of things. I'll keep these things in my mind.

fs.access('./package.json', fs.constants.R_OK, (err) => {
console.log(err ? 'Can\'t read package.json!' : 'Ready to read!');
});
```

```js
// Check if we can write to `package.json` file.
fs.access('./package.json', fs.constants.W_OK, (err) => {
console.log(err ? 'You cannot write to package.json.' : 'You can write to package.json.');
Copy link
Member

Choose a reason for hiding this comment

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

Line length > 80

});
```

```js
// Check if `package.json` exists in the current directory and is writeable.
fs.access('./package.json', fs.constants.F_OK | fs.constants.W_OK, (err) => {

// If there is an error, print the error in console and exit.
if(err) {
if(err.code === 'ENOENT')
console.error("File does not exist.");
if(err.code === 'EPERM')
Copy link
Member

Choose a reason for hiding this comment

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

Whitespace after the if in if(.

console.error("File is read-only.");
return;
}

console.log("The file exists. You can write to the file.");
});
```

OR simply

```js
// Check if `package.json` exists in the current directory and is writeable.
fs.access('./package.json', fs.constants.F_OK | fs.constants.W_OK, (err) => {
if(err)
console.error("Either the file does not exist or it is read-only.");
else
console.log("The file exists. You can write to the file.");
});
Copy link
Member

Choose a reason for hiding this comment

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

This example is pretty redundant as the only difference is checking for the error code. It should be obvious from the example above what to do.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, @BridgeAR. I appreciate your feedback.

```

Expand Down