-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[code-infra] Replace fs-extra
with node:fs
where possible
#19127
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
[code-infra] Replace fs-extra
with node:fs
where possible
#19127
Conversation
docs/scripts/api/utils.ts
Outdated
import { Symbol, isPropertySignature, isExportSpecifier, TypeFormatFlags } from 'typescript'; | ||
import fs from 'node:fs'; |
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.
I imported from node:fs
, but we seem to import from fs
directly. I can change that if needed.
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.
I'm adding mui/mui-public#476
Deploy preview: https://deploy-preview-19127--material-ui-x.netlify.app/ Bundle size report
|
@@ -50,7 +50,8 @@ async function main(argv) { | |||
const exec = dryRun ? execDry : execActual; | |||
|
|||
const rootWorkspace = getWorkspaceRoot(); | |||
const rootWorkspaceManifest = await fse.readJSON(path.join(rootWorkspace, 'package.json')); |
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.
readJSON
imports ./jsonfile
, which imports the jsonfile
package.
jsonfile
's readFile
is implemented here. Since we don't provide any options, it boils down to fs.readFile
+ JSON.parse(data, null)
.
stripBom
just converts the buffer into an utf8 string, so this code is equivalent.
docs/scripts/api/utils.ts
Outdated
@@ -97,7 +97,7 @@ export async function writePrettifiedFile(filename: string, data: string) { | |||
); | |||
} | |||
|
|||
fse.writeFileSync( | |||
fs.writeFileSync( |
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.
Why not
fs.writeFileSync( | |
await fs.writeFile( |
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.
Wanted to keep the changes to a minimum, but I guess this is fine.
docs/scripts/api/utils.ts
Outdated
import { Symbol, isPropertySignature, isExportSpecifier, TypeFormatFlags } from 'typescript'; | ||
import fs from 'node:fs'; |
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.
Perhaps we can standardize on
import fs from 'node:fs'; | |
import fs from 'node:fs/promises'; |
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.
Yeah, I'm ok with that.
@@ -12,7 +12,6 @@ | |||
const ignoreList = ['/pages.ts', 'styling.ts', 'styling.tsx', 'types.ts']; | |||
|
|||
const fs = require('fs'); |
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.
Might as well just import fs/promises
instead of doing fs.promises
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 have a mixed usage of fs
and fs/promises
in this file, so that's why I opted for this approach.
We can also do const fsPromises = require('fs/promises');
and migrate the relevant code snippets. What would you prefer?
scripts/releaseTag.mjs
Outdated
@@ -50,7 +50,8 @@ async function main(argv) { | |||
const exec = dryRun ? execDry : execActual; | |||
|
|||
const rootWorkspace = getWorkspaceRoot(); | |||
const rootWorkspaceManifest = await fse.readJSON(path.join(rootWorkspace, 'package.json')); | |||
const rootWorkspacePackageJson = await fs.readFile(path.join(rootWorkspace, 'package.json')); |
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.
I believe with a proper encoding it will always return a string
const rootWorkspacePackageJson = await fs.readFile(path.join(rootWorkspace, 'package.json')); | |
const rootWorkspacePackageJson = await fs.readFile(path.join(rootWorkspace, 'package.json'), 'utf-8'); |
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.
You're right. If we do that, to maintain a 100% compatible change, we'd need to handle byte order mark as jsonfile
does.
I'm not very familiar with this, so I'm not sure how big of a problem it might be.
From my tests, our root package.json
does not use BOM, but it might be dependent on the last person who made changes to that specific file.
From the UTF-8 spec:
A protocol SHOULD forbid use of U+FEFF as a signature for those
textual protocol elements that the protocol mandates to be always
UTF-8, the signature function being totally useless in those
cases.
So I think we can ignore it and assume we're always using UTF-8
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've been consuming json files all over the place with this method since forever, so I'm not really concerned. I'm adding the unicode-bom
rule in the ticket
@@ -90,15 +89,18 @@ const previewOverride = { | |||
async function transpileFile(tsxPath, program, ignoreCache = false) { | |||
const jsPath = tsxPath.replace(/\.tsx?$/, '.js'); | |||
try { | |||
if (!ignoreCache && (await fse.exists(jsPath))) { | |||
const [jsStat, tsxStat] = await Promise.all([fse.stat(jsPath), fse.stat(tsxPath)]); | |||
if (!ignoreCache && (await fs.promises.exists(jsPath))) { |
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.
This line introduced an issue. Fix here
fs-extra
with node:fs
where possiblefs-extra
with node:fs
where possible
Part of mui/mui-public#481
Noticed that we depend on
fs-extra
, so tried to replace it withnode:fs
where possible. Most of the calls tofs-extra
werenode:fs
calls anyway.After these changes, there are only 4 imports of
fs-extra
, which we might be able to remove eventually.