-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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: improve fs code example quality #46948
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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'; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this imply that
Suggested change
or
Suggested change
would be less confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree 👍 thanks! |
||||||||||||||||||
|
||||||||||||||||||
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); | ||||||||||||||||||
|
@@ -7632,7 +7638,7 @@ try { | |||||||||||||||||
fd = await open('/open/some/file.txt', 'r'); | ||||||||||||||||||
// Do something with the file | ||||||||||||||||||
} finally { | ||||||||||||||||||
await fd.close(); | ||||||||||||||||||
await fd?.close(); | ||||||||||||||||||
} | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -7646,7 +7652,7 @@ try { | |||||||||||||||||
fd = await open('file.txt', 'r'); | ||||||||||||||||||
// Do something with the file | ||||||||||||||||||
} finally { | ||||||||||||||||||
await fd.close(); | ||||||||||||||||||
await fd?.close(); | ||||||||||||||||||
} | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -7761,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(); | ||||||||||||||||||
} | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would typically need to have two examples here, one ESM and one CJS. (doesn't have to block this PR from landing, but it'd need to happen at some point)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problems, once this PR lands I can work on another PR to make sure all of the fs code examples have js flavor 👌 (at the moment there are many of the examples in fs missing them)