Skip to content
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

refactor: use node:fs/promises #11206

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alumni
Copy link
Contributor

@alumni alumni commented Dec 21, 2024

Description of change

Replacing the sync functions from node:fs with async functions from node:fs/promises. This should simplify the code by not requiring Promise wrappers and it should not block the event loop anymore.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

@alumni alumni force-pushed the improvement/fs-promises branch 2 times, most recently from c8dfe76 to 88a3d93 Compare December 21, 2024 23:02
@alumni alumni changed the title refactor: use fs promises refactor: use node:fs/promises Dec 22, 2024
@@ -1,5 +1,6 @@
import "reflect-metadata"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove reflect metadata here? Is it really not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary. reflect-metadata only needs importing once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If test/utils/test-setup.ts is loaded correctly by mocha before the tests, this should definitely not be needed. I noticed some files also load test-setup.ts by themselves, this should also not be required.

It looks like a bigger cleanup is required for the test setup, so I reverted this for now.

await promisify(unlink)(logPath)
}
try {
fs.unlink(logPath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good catch, I somehow didn't see it.

There's an ESLint rule for this: @typescript-eslint/no-floating-promises, and it's in the recommended-type-checked config. We should consider enabling these rules.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely

err ? fail(err) : ok(data.toString()),
)
})
return (await fs.readFile(filePath)).toString()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split this into 2 lines

Copy link
Contributor Author

@alumni alumni Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split how into two lines?

This is the job of the formatter, this file was already formatted.

Later edit: no matter how I try to split, prettier reverts it to a single line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const file = await fs.readFile(filePath))

return file.toString()

@@ -67,7 +66,7 @@ export class CommandUtils {
* Creates directories recursively.
*/
static createDirectories(directory: string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updaye return type to Promise

})
try {
const stats = await fs.stat(potentialPackageJson)
if (stats.isFile()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an early return here for better readability.

if (!stats.isFile()) {
  continue
}

}
} catch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you catching here? A potential throw from fs.stats?

Copy link
Contributor Author

@alumni alumni Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. It's the same thing that was being caught before: https://nodejs.org/api/errors.html#common-system-errors

@@ -41,7 +38,7 @@ describe("ImportUtils.importOrRequireFile", () => {
expect(exports.default).to.be.a("function")
expect(exports.number).to.be.eq(6)

rmdirSync(testDir)
await fs.rmdir(testDir, { recursive: true })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is correct. The file was formatted before commit.

@alumni alumni force-pushed the improvement/fs-promises branch 2 times, most recently from b88acce to c827e75 Compare December 23, 2024 11:55
@alumni alumni force-pushed the improvement/fs-promises branch from c827e75 to b7cc21b Compare December 23, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants