Migrate fs-extra to Node.js fs

Proprietary

Replaces fs-extra with the Node.js standard library (node:fs, node:fs/promises, node:path). fs-extra's own methods (remove, ensureDir, pathExists, readJson, writeJson, outputFile, outputJson, ensureFile, copy, emptyDir) are rewritten to their native equivalents and the fs methods fs-extra re-exports are bound from node:fs or node:fs/promises directly. move becomes a call to a helper written into the same file, since rename alone doesn't create the destination's parent, refuse an existing destination, or work across devices. Calls without a faithful native equivalent (ensureSymlink, ensureLink, ...), and calls whose last argument's type doesn't say whether it is a callback (any, untyped JavaScript, a union of a function and data, a named type alias), are left in place and listed in the FsExtraManualMigrationSteps data table. fs-extra and @types/fs-extra are removed from each package.json that declares them, with the lock file updated to match. They are removed even where a call listed in the data table still uses fs-extra, so migrate those before building. Tests that mock or spy on fs-extra itself (vi.mock('fs-extra'), vi.spyOn(fs, 'outputFile'), ...) keep watching a module the migrated code no longer calls, so they fail until they mock node:fs / node:fs/promises instead. Every such site is listed in the data table; migrate them together with the code they cover.

org.openrewrite.node.migrate.fs-extra.migrate-fs-extra-to-node-fs
mod run . --recipe=org.openrewrite.node.migrate.fs-extra.migrate-fs-extra-to-node-fs

Install it once, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.

Recipe list

It runs these recipes, in this order.

  1. Replace fs-extra remove with fs.rm
  2. Replace fs-extra ensureDir/mkdirp/mkdirs with fs.mkdir
  3. Replace fs-extra pathExists with fs.access/fs.existsSync
  4. Replace fs-extra readJson with JSON.parse of readFile
  5. Replace fs-extra writeJson with fs.writeFile
  6. Replace fs-extra outputFile and outputJson with mkdir + writeFile
  7. Replace fs-extra ensureFile with fs.mkdir and fs.writeFile
  8. Replace fs-extra emptyDir with mkdir and rm of each entry
  9. Replace fs-extra move with a generated helper
  10. Replace fs-extra copy with fs.cp
  11. Replace fs-extra's re-exported fs methods with node:fs
  12. Find fs-extra usages that need manual migration
  13. Remove the fs-extra dependency