Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions recipes/rimraf-to-fs-rm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Rimraf to fs.rm

This recipe migrates straightforward `rimraf` delete calls to Node.js built-in
`fs.rm`, `fs.rmSync`, and `fs/promises.rm` APIs.

It covers the literal delete cases for `rimraf` v3, v4, and v5 style imports:

- default async imports from `rimraf`, `rimraf-v3`, or `rimraf-v4`
- named `rimraf` and `rimrafSync` imports from `rimraf-v5`
- callback-based literal deletes
- promise-based literal deletes
- synchronous literal deletes
- synchronous glob deletes by expanding with `fs.globSync()` before `fs.rmSync()`
- package.json dependency removal when `rimraf` is not still used as a CLI in scripts

## Examples

```diff
- import rimraf from "rimraf-v4";
+ import { rm as rmPromise } from "node:fs/promises";

- await rimraf("dist", { glob: false });
+ await rmPromise("dist", { recursive: true, force: true });
```

```diff
- import { rimraf, rimrafSync } from "rimraf-v5";
+ import { rmSync } from "node:fs";
+ import { rm as rmPromise } from "node:fs/promises";

- await rimraf("dist");
- rimrafSync("build");
+ await rmPromise("dist", { recursive: true, force: true });
+ rmSync("build", { recursive: true, force: true });
```

```diff
- import { rimrafSync } from "rimraf-v5";
+ import { globSync, rmSync } from "node:fs";

- rimrafSync("dist/**/*.js");
+ for (const filePath of globSync("dist/**/*.js")) {
+ rmSync(filePath, { recursive: true, force: true });
+ }
```

## Manual review

Some `rimraf` behavior should stay manual because it depends on runtime
semantics rather than only source syntax:

- custom retry behavior
- custom glob options
- async glob deletes with callbacks
- Windows package-specific fallback behavior
- CLI usage, which is left untouched and may need a different migration path

When code depends on those behaviors, keep the behavior explicit instead of
assuming full parity with native deletion APIs.
24 changes: 24 additions & 0 deletions recipes/rimraf-to-fs-rm/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema_version: "1.0"
name: "@nodejs/rimraf-to-fs-rm"
version: "1.0.0"
description: Migrate literal rimraf deletes to Node.js fs.rm APIs
author: Herrtian
license: MIT
workflow: workflow.yaml
category: migration
repository: https://github.com/nodejs/userland-migrations

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration
- nodejs
- rimraf

registry:
access: public
visibility: public
26 changes: 26 additions & 0 deletions recipes/rimraf-to-fs-rm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@nodejs/rimraf-to-fs-rm",
"version": "1.0.0",
"description": "Migrate literal rimraf deletes to Node.js fs.rm APIs.",
"type": "module",
"scripts": {
"test": "node --run test:workflow && node --run test:remove-dependencies",
"test:workflow": "npx codemod jssg test -l typescript ./src/workflow.ts ./tests",
"test:remove-dependencies": "npx codemod jssg test -l json ./src/remove-dependencies.ts ./tests/remove-dependencies --allow-child-process --allow-fs --strictness cst"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/rimraf-to-fs-rm",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Herrtian",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/rimraf-to-fs-rm/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.6.1"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
68 changes: 68 additions & 0 deletions recipes/rimraf-to-fs-rm/src/remove-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { SgNode, Transform } from '@codemod.com/jssg-types/main';
import type Json from '@codemod.com/jssg-types/langs/json';
import removeDependencies from '@nodejs/codemod-utils/remove-dependencies';

const rimrafPackages = [
'rimraf',
'@types/rimraf',
];

/**
* Returns whether package.json scripts still call the rimraf CLI.
*/
function hasRimrafCliScript(rootNode: SgNode<Json>): boolean {
const scriptsPair = rootNode.find({
rule: {
kind: 'pair',
all: [
{
has: {
field: 'key',
kind: 'string',
regex: '^"scripts"$',
},
},
{
has: {
field: 'value',
kind: 'object',
},
},
],
},
});

const scriptsObject = scriptsPair?.field('value');
if (!scriptsObject) return false;

return Boolean(
scriptsObject.find({
rule: {
kind: 'pair',
has: {
field: 'value',
kind: 'string',
has: {
kind: 'string_content',
regex: '\\brimraf(?:\\s|$)',
},
},
},
}),
);
}

/**
* Removes rimraf packages when package scripts no longer need the rimraf CLI.
*/
const transform: Transform<Json> = async (root) => {
if (hasRimrafCliScript(root.root())) return null;

return removeDependencies(rimrafPackages, {
packageJsonPath: root.filename(),
runInstall: false,
persistFileWrite: false,
});
};

export default transform;
Loading