Browse Source

doc: update and modernize examples in fs.ms

* 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: https://github.com/nodejs/node/pull/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>
v6
Vse Mozhet Byt 8 years ago
committed by James M Snell
parent
commit
31da3757fa
  1. 50
      doc/api/fs.md

50
doc/api/fs.md

@ -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,
@ -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
@ -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);
@ -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) => {
@ -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);
@ -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);
});
```
@ -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);
});
```
@ -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>
@ -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`.
@ -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".
@ -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!');
});
```

Loading…
Cancel
Save