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

doc: modernize and fix code examples in fs.md #12035

Closed
wants to merge 7 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
50 changes: 25 additions & 25 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ For a regular file [`util.inspect(stats)`][] would return a string very
similar to this:

```js
{
Stats {
dev: 2114,
ino: 48064969,
mode: 33188,
Expand All @@ -232,8 +232,7 @@ similar to this:
atime: Mon, 10 Oct 2011 23:24:11 GMT,
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
birthtime: Mon, 10 Oct 2011 23:24:11 GMT
}
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
```

Please note that `atime`, `mtime`, `birthtime`, and `ctime` are
Expand Down Expand Up @@ -377,12 +376,12 @@ fs.access('myfile', (err) => {
```js
fs.open('myfile', 'wx', (err, fd) => {
if (err) {
if (err.code === "EEXIST") {
if (err.code === 'EEXIST') {
console.error('myfile already exists');
return;
} else {
throw err;
}

throw err;
}

writeMyData(fd);
Expand All @@ -394,12 +393,12 @@ fs.open('myfile', 'wx', (err, fd) => {
```js
fs.access('myfile', (err) => {
if (err) {
if (err.code === "ENOENT") {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
} else {
throw err;
}

throw err;
}

fs.open('myfile', 'r', (err, fd) => {
Expand All @@ -414,12 +413,12 @@ fs.access('myfile', (err) => {
```js
fs.open('myfile', 'r', (err, fd) => {
if (err) {
if (err.code === "ENOENT") {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
} else {
throw err;
}

throw err;
}

readMyData(fd);
Expand Down Expand Up @@ -779,13 +778,14 @@ fs.exists('myfile', (exists) => {
```js
fs.open('myfile', 'wx', (err, fd) => {
if (err) {
if (err.code === "EEXIST") {
if (err.code === 'EEXIST') {
console.error('myfile already exists');
return;
} else {
throw err;
}

throw err;
}

writeMyData(fd);
});
```
Expand All @@ -809,15 +809,15 @@ fs.exists('myfile', (exists) => {
```js
fs.open('myfile', 'r', (err, fd) => {
if (err) {
if (err.code === "ENOENT") {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
} else {
throw err;
}
} else {
readMyData(fd);

throw err;
}

readMyData(fd);
});
```

Expand Down Expand Up @@ -1025,7 +1025,7 @@ const fd = fs.openSync('temp.txt', 'r+');

// truncate the file to 10 bytes, whereas the actual size is 7 bytes
fs.ftruncate(fd, 10, (err) => {
assert.ifError(!err);
assert.ifError(err);
console.log(fs.readFileSync('temp.txt'));
});
// Prints: <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
Expand Down Expand Up @@ -1281,8 +1281,8 @@ fs.mkdtemp(tmpDir, (err, folder) => {
});

// This method is *CORRECT*:
const path = require('path');
fs.mkdtemp(tmpDir + path.sep, (err, folder) => {
const { sep } = require('path');
fs.mkdtemp(`${tmpDir}${sep}`, (err, folder) => {
if (err) throw err;
console.log(folder);
// Will print something similar to `/tmp/abc123`.
Expand Down Expand Up @@ -1763,7 +1763,7 @@ argument will automatically be normalized to absolute path.
Here is an example below:

```js
fs.symlink('./foo', './new-port');
fs.symlink('./foo', './new-port', callback);
```

It creates a symbolic link named "new-port" that points to "foo".
Expand Down Expand Up @@ -2173,7 +2173,7 @@ Example:
```js
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
if (err) throw err;
console.log('It\'s saved!');
console.log('The file has been saved!');
});
```

Expand Down