Skip to content

Modernize: Replace fs-extra with native APIs#32296

Merged
ndelangen merged 14 commits into
storybookjs:nextfrom
y-hsgw:chore/replace-node-native
Sep 4, 2025
Merged

Modernize: Replace fs-extra with native APIs#32296
ndelangen merged 14 commits into
storybookjs:nextfrom
y-hsgw:chore/replace-node-native

Conversation

@y-hsgw
Copy link
Copy Markdown
Member

@y-hsgw y-hsgw commented Aug 19, 2025

Closes #20228

What I did

This PR replaces fs-extra with the native APIs.

Checklist for Contributors

Testing

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

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

Manual testing

Run yarn test and yarn build-package under the scripts/ directory.

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-storybook/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>

Greptile Summary

This PR systematically replaces the fs-extra dependency with native Node.js filesystem APIs across the entire scripts directory. The migration touches 35+ files and removes fs-extra from the package.json dependencies. Key changes include:

  • Import replacements: All fs-extra imports are replaced with node:fs/promises imports (readFile, writeFile, rm, cp, access, etc.)
  • Custom utility functions: Several files implement custom helper functions to replicate fs-extra functionality:
    • pathExists() using access() with try-catch error handling
    • readJson() using readFile() + JSON.parse()
    • emptyDir() using mkdir() + readdir() + rm()
    • outputFile() using mkdir(dirname()) + writeFile()
  • JSON operations: Replaces readJson/writeJson with manual readFile/writeFile + JSON.parse/JSON.stringify
  • File operations: Replaces copy with cp, remove with rm, move with rename, ensureDir with mkdir
  • Test updates: Updates test mocks and expectations to work with native APIs instead of fs-extra
  • ESLint config: Removes fs-extra from the banned dependencies list

This change aligns with modern Node.js best practices of leveraging native APIs when equivalent functionality exists, reducing the dependency footprint from 1 external package to 0. The migration affects build scripts, release automation, sandbox generation, and various utility functions throughout the development toolchain. All changes maintain functional equivalence while using only built-in Node.js capabilities available since Node.js 14+.

Confidence score: 2/5

  • This PR has a critical runtime bug that will cause immediate failures in production
  • Score reflects a missing function reference in scripts/reset.js that will throw a ReferenceError
  • Pay close attention to scripts/reset.js which calls remove(uri) on line 45 but only imports rm() from native APIs

Copy link
Copy Markdown
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.

36 files reviewed, 10 comments

Edit Code Review Bot Settings | Greptile

Comment thread scripts/release/write-changelog.ts
Comment thread scripts/sandbox/utils/yarn.ts Outdated
Comment thread scripts/bench/utils.ts Outdated
Comment thread scripts/task.ts Outdated
Comment thread scripts/tasks/publish.ts
Comment thread scripts/sandbox/generate.ts Outdated
Comment thread scripts/tasks/sandbox-parts.ts
Comment thread scripts/utils/yarn.ts
Comment thread scripts/.eslintrc.cjs Outdated
Comment thread scripts/release/__tests__/version.test.ts Outdated
@ndelangen ndelangen added maintenance User-facing maintenance tasks ci:normal labels Sep 4, 2025
@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Sep 4, 2025

View your CI Pipeline Execution ↗ for commit 9bb3db0

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 46s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-04 10:03:01 UTC

@ndelangen ndelangen changed the title refactor: consider removing fs-extra Modernize: Replace fs-extra with native Sep 4, 2025
@ndelangen ndelangen changed the title Modernize: Replace fs-extra with native Modernize: Replace fs-extra with native APIs Sep 4, 2025
@ndelangen ndelangen requested a review from Copilot September 4, 2025 08:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modernizes the scripts directory by replacing the fs-extra dependency with native Node.js filesystem APIs. This reduces the dependency footprint and aligns with modern Node.js best practices by leveraging built-in functionality available since Node.js 14+.

Key changes include:

  • Replacing all fs-extra imports with node:fs/promises imports across 35+ files
  • Implementing custom utility functions to replicate specific fs-extra functionality where needed
  • Removing fs-extra from package.json dependencies and ESLint banned dependencies

Reviewed Changes

Copilot reviewed 35 out of 36 changed files in this pull request and generated no comments.

Show a summary per file
File Description
scripts/utils/yarn.ts Replace readJSON/writeJSON with manual JSON parsing/stringifying
scripts/utils/tools.ts Replace pathExists and readJson with native access and readFile
scripts/utils/paths.ts Replace pathExists with custom implementation using access
scripts/utils/package-json.ts Replace readJSON/writeJSON with native file operations
scripts/utils/filterExistsInCodeDir.ts Replace pathExists with custom implementation
scripts/tasks/sandbox.ts Replace pathExists and remove with access and rm
scripts/tasks/sandbox-parts.ts Replace multiple fs-extra functions with native equivalents and custom utilities
scripts/tasks/publish.ts Replace pathExists with custom implementation
scripts/tasks/install.ts Replace pathExists and remove with access and rm
scripts/tasks/generate.ts Replace pathExists and remove with access and rm
scripts/tasks/compile.ts Replace readFile import with native equivalent
scripts/tasks/build.ts Replace pathExists with custom implementation
scripts/task.ts Replace outputFile and pathExists with custom implementations
scripts/sandbox/utils/yarn.ts Replace move, remove with rename, rm
scripts/sandbox/utils/template.ts Replace readFile import with native equivalent
scripts/sandbox/publish.ts Replace copy, emptyDir, remove with native equivalents and custom utilities
scripts/sandbox/generate.ts Replace multiple fs-extra functions with native equivalents and custom utilities
scripts/run-registry.ts Replace pathExists, readJSON, remove with native equivalents
scripts/reset.js Replace remove with rm
scripts/release/ files Replace readFile, readJson, writeJson with native equivalents across multiple release scripts
scripts/package.json Remove fs-extra and @types/fs-extra dependencies
scripts/get-template.ts Replace pathExists and readFile with native equivalents
scripts/get-report-message.ts Replace readJson with native file operations
scripts/combine-compodoc.ts Replace fs-extra imports with native equivalents
scripts/check-package.ts Replace readJSON with native file operations
scripts/build-package.ts Replace readJSON with native file operations
scripts/bench/utils.ts Replace multiple fs-extra functions with native equivalents
Comments suppressed due to low confidence (1)

scripts/reset.js:1

  • The function remove is not defined. The import statement imports rm from node:fs/promises, but the code still calls remove(uri). This should be changed to rm(uri, { force: true, recursive: true }).
import { spawn } from 'node:child_process';

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ndelangen ndelangen merged commit ecfbcbc into storybookjs:next Sep 4, 2025
51 checks passed
@github-actions github-actions Bot mentioned this pull request Sep 4, 2025
24 tasks
@y-hsgw y-hsgw deleted the chore/replace-node-native branch September 4, 2025 11:13
@github-actions github-actions Bot mentioned this pull request Sep 16, 2025
23 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:normal maintenance User-facing maintenance tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: consider removing fs-extra

3 participants