From 9dda771c1f6c67f1f7bcca1f8edad7ec9c959459 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sat, 25 Mar 2017 15:33:03 +0200 Subject: [PATCH] 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 Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- doc/api/fs.md | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 5471c5f5e646f5..36a653386d230e 100644 --- a/doc/api/fs.md +++ b/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); @@ -729,13 +728,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); }); ``` @@ -759,15 +759,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); }); ``` @@ -945,7 +945,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: @@ -1154,8 +1154,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`. @@ -1564,7 +1564,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". @@ -1910,7 +1910,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!'); }); ```