Skip to content

Commit

Permalink
doc: update and modernize examples in fs.ms
Browse files Browse the repository at this point in the history
* unify quotes in fs.md
* avoid quote escaping in fs.md
* simplify logics in fs.md
* concatenation -> template literal in fs.md
* add missing callback in fs.md
* fix typo in fs.md
* update output example in fs.md

PR-URL: #12035
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
vsemozhetbyt authored and jasnell committed Mar 27, 2017
1 parent d367a0a commit 31da375
Showing 1 changed file with 25 additions and 25 deletions.
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

0 comments on commit 31da375

Please sign in to comment.