From 51ba66dceb244e0d7248c6add09e7b92a20fdcc3 Mon Sep 17 00:00:00 2001 From: jakecastelli <959672929@qq.com> Date: Sat, 4 Mar 2023 22:17:01 +1030 Subject: [PATCH 1/3] doc: improve fs code example quality --- doc/api/fs.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 5fd4cfeefb504d..c794da1cac8576 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1117,7 +1117,7 @@ try { ```cjs const { mkdir } = require('node:fs/promises'); -const { resolve, join } = require('node:path'); +const { join } = require('node:path'); async function makeDirectory() { const projectFolder = join(__dirname, 'test', 'project'); @@ -1159,9 +1159,11 @@ object with an `encoding` property specifying the character encoding to use. ```mjs import { mkdtemp } from 'node:fs/promises'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; try { - await mkdtemp(path.join(os.tmpdir(), 'foo-')); + await mkdtemp(join(tmpdir(), 'foo-')); } catch (err) { console.error(err); } @@ -3237,8 +3239,10 @@ object with an `encoding` property specifying the character encoding to use. ```mjs import { mkdtemp } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; -mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => { +mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => { if (err) throw err; console.log(directory); // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 @@ -7542,6 +7546,8 @@ For example, the following is prone to error because the `fs.stat()` operation might complete before the `fs.rename()` operation: ```js +const fs = require('node:fs'); + fs.rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; console.log('renamed complete'); @@ -7558,12 +7564,12 @@ of one before invoking the other: ```mjs import { rename, stat } from 'node:fs/promises'; -const from = '/tmp/hello'; -const to = '/tmp/world'; +const fromDir = '/tmp/hello'; +const toDir = '/tmp/world'; try { - await rename(from, to); - const stats = await stat(to); + await rename(fromDir, toDir); + const stats = await stat(toDir); console.log(`stats: ${JSON.stringify(stats)}`); } catch (error) { console.error('there was an error:', error.message); @@ -7573,10 +7579,10 @@ try { ```cjs const { rename, stat } = require('node:fs/promises'); -(async function(from, to) { +(async function(fromDir, toDir) { try { - await rename(from, to); - const stats = await stat(to); + await rename(fromDir, toDir); + const stats = await stat(toDir); console.log(`stats: ${JSON.stringify(stats)}`); } catch (error) { console.error('there was an error:', error.message); From 7ba550021878e9d65355b01f5c36bbd799f38c54 Mon Sep 17 00:00:00 2001 From: jakecastelli <959672929@qq.com> Date: Sat, 4 Mar 2023 22:37:58 +1030 Subject: [PATCH 2/3] fix: add missing optional chaining operator --- doc/api/fs.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index c794da1cac8576..bc3e29c6aadb03 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -7638,7 +7638,7 @@ try { fd = await open('/open/some/file.txt', 'r'); // Do something with the file } finally { - await fd.close(); + await fd?.close(); } ``` @@ -7652,7 +7652,7 @@ try { fd = await open('file.txt', 'r'); // Do something with the file } finally { - await fd.close(); + await fd?.close(); } ``` @@ -7767,7 +7767,7 @@ try { fd = await open(Buffer.from('/open/some/file.txt'), 'r'); // Do something with the file } finally { - await fd.close(); + await fd?.close(); } ``` From 67da4a314133340110d0c7fc0dc67fb6bf7d20b6 Mon Sep 17 00:00:00 2001 From: jakecastelli <959672929@qq.com> Date: Sun, 5 Mar 2023 21:09:26 +1030 Subject: [PATCH 3/3] fixup fixup --- doc/api/fs.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index bc3e29c6aadb03..a83a50b5d184ae 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -7564,12 +7564,12 @@ of one before invoking the other: ```mjs import { rename, stat } from 'node:fs/promises'; -const fromDir = '/tmp/hello'; -const toDir = '/tmp/world'; +const oldPath = '/tmp/hello'; +const newPath = '/tmp/world'; try { - await rename(fromDir, toDir); - const stats = await stat(toDir); + await rename(oldPath, newPath); + const stats = await stat(newPath); console.log(`stats: ${JSON.stringify(stats)}`); } catch (error) { console.error('there was an error:', error.message); @@ -7579,10 +7579,10 @@ try { ```cjs const { rename, stat } = require('node:fs/promises'); -(async function(fromDir, toDir) { +(async function(oldPath, newPath) { try { - await rename(fromDir, toDir); - const stats = await stat(toDir); + await rename(oldPath, newPath); + const stats = await stat(newPath); console.log(`stats: ${JSON.stringify(stats)}`); } catch (error) { console.error('there was an error:', error.message);