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

Core: Replace fs-extra with the native APIs #29126

Merged
merged 31 commits into from
Sep 17, 2024

Conversation

ziebam
Copy link
Contributor

@ziebam ziebam commented Sep 14, 2024

Closes #29104. Works on #29038.

What I did

This PR replaces fs-extra with the native APIs in all code/**/* packages. Now it's only included in scripts/ and as a transitive dependency.

Checklist for Contributors

Testing

I've confirmed that the unit tests pass locally in the packages I've made the changes to. Please advise on which integration/E2E tests I should run (if any).

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Here's what I did for most changes (in packages that have had the unit tests):

  1. Run npx vitest in the package directory before making any changes.
  2. Build in watch mode.
  3. Make changes and either rerun npx vitest after every change (for smaller packages) or utilize Vitest's watching capabilities (for bigger packages).

As above, please advise if I should do some more testing here.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.4 MB 77.4 MB 0 B 2.22 0%
initSize 163 MB 162 MB -507 kB -0.23 -0.3%
diffSize 85.4 MB 84.9 MB -507 kB -0.27 -0.6%
buildSize 7.57 MB 7.57 MB -2 B 0.8 0%
buildSbAddonsSize 1.66 MB 1.66 MB 0 B 0.89 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 2.34 MB 2.34 MB 0 B 0.82 0%
buildSbPreviewSize 352 kB 352 kB 0 B 0.65 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.55 MB 4.55 MB 0 B 0.89 0%
buildPreviewSize 3.02 MB 3.02 MB -2 B 0.11 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 25.4s 13s -12s -412ms 0.03 -95.3%
generateTime 19.7s 20.6s 900ms -0.13 4.4%
initTime 16.1s 18.8s 2.7s 1.33 🔺14.7%
buildTime 10.4s 10.5s 146ms -0.88 1.4%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 7.9s 7.7s -277ms 0.82 -3.6%
devManagerResponsive 5.1s 5.2s 18ms 1.23 0.3%
devManagerHeaderVisible 874ms 771ms -103ms -0.37 -13.4%
devManagerIndexVisible 915ms 802ms -113ms -0.41 -14.1%
devStoryVisibleUncached 1.3s 1.3s 26ms -0.09 1.9%
devStoryVisible 914ms 801ms -113ms -0.42 -14.1%
devAutodocsVisible 776ms 671ms -105ms -0.55 -15.6%
devMDXVisible 778ms 679ms -99ms -0.43 -14.6%
buildManagerHeaderVisible 904ms 720ms -184ms -0.57 -25.6%
buildManagerIndexVisible 944ms 721ms -223ms -0.74 -30.9%
buildStoryVisible 945ms 834ms -111ms 0.03 -13.3%
buildAutodocsVisible 898ms 676ms -222ms -0.42 -32.8%
buildMDXVisible 726ms 679ms -47ms -0.06 -6.9%

Greptile Summary

This PR replaces the fs-extra package with native Node.js fs and fs/promises APIs across multiple files in the Storybook codebase, aiming to reduce dependencies and use built-in functionality.

  • Replaced fs-extra with node:fs and node:fs/promises in core packages and builders
  • Updated file operations to use native methods like readFile, writeFile, cp, and rm
  • Removed fs-extra from package.json files in affected packages
  • Added TODO comments regarding experimental fsPromises.cp usage in Node 16-21
  • Implemented custom file existence checks to replace fs-extra's ensureFile functionality

@ziebam ziebam marked this pull request as ready for review September 14, 2024 18:13
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

30 file(s) reviewed, 10 comment(s)
Edit PR Review Bot Settings

code/__mocks__/fs.ts Show resolved Hide resolved
mockFiles = newMockFiles;
}

export const readFileSync = (filePath = '') => mockFiles[filePath];
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Default parameter value '' might cause unexpected behavior if undefined is passed

code/__mocks__/fs/promises.ts Show resolved Hide resolved
code/__mocks__/fs/promises.ts Show resolved Hide resolved
Comment on lines 20 to 22
const { dependencies = {}, peerDependencies = {} } = JSON.parse(
await readFile(path, { encoding: 'utf-8' })
);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider wrapping JSON.parse in a try-catch block to handle potential JSON parsing errors

code/core/scripts/helpers/dependencies.ts Outdated Show resolved Hide resolved
code/core/scripts/helpers/generateTypesMapperFiles.ts Outdated Show resolved Hide resolved
code/core/src/core-server/build-static.ts Outdated Show resolved Hide resolved
@JReinhold JReinhold added build Internal-facing build tooling & test updates ci:normal labels Sep 16, 2024
@ziebam
Copy link
Contributor Author

ziebam commented Sep 16, 2024

The failures seem unrelated to my PR, although I'm not sure since there's not much output. Are they expected, @JReinhold?

EDIT: They were related to my PR. 😄

Copy link

nx-cloud bot commented Sep 16, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 8115974. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

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

Fantastic job!

Just to make sure, have you ensured all of these APIs are available in Node 18, which we currently support?

code/core/src/cli/helpers.ts Show resolved Hide resolved
code/core/src/core-server/build-static.ts Outdated Show resolved Hide resolved
@ziebam
Copy link
Contributor Author

ziebam commented Sep 16, 2024

Just to make sure, have you ensured all of these APIs are available in Node 18, which we currently support?

Yup, everything I've used was introduced before Node 18. The only concern could be fsPromises.cp which is marked as experimental, but that's been resolved in another PR comment.

As for the failing ci/circleci: test-portable-stories-nextjs: I think it might be flaky? Sometimes it fails with the same error as now, but it passed before merging next.

@JReinhold
Copy link
Contributor

Yes it looks flaky, don't worry about it.

@JReinhold JReinhold changed the title Replace fs-extra with the native APIs Core: Replace fs-extra with the native APIs Sep 17, 2024
@JReinhold JReinhold added feature request dependencies and removed build Internal-facing build tooling & test updates feature request labels Sep 17, 2024
@JReinhold JReinhold merged commit ea27938 into storybookjs:next Sep 17, 2024
56 of 61 checks passed
@ziebam ziebam deleted the remove-fs-extra branch September 17, 2024 10:35
@github-actions github-actions bot mentioned this pull request Sep 17, 2024
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate from fs-extra to node:fs
3 participants