Skip to content

Commit 4e89567

Browse files
committedFeb 28, 2024
Use fs.rm() instead of rimraf
A regression occurred in 0.2.2 which can be reproduced using: ```js const assert = require('assert'); const tmp = require('tmp'); tmp.dir({ unsafeCleanup: true }, (err, path, cleanup) => { assert.ifError(err); cleanup(assert.ifError); }); ``` This works with 0.2.1. With 0.2.2 it fails with: /path/to/tmp/node-tmp/lib/tmp.js:358 return removeFunction(fileOrDirName, next || function() {}); ^ TypeError: removeFunction is not a function at _cleanupCallback (/path/to/tmp/node-tmp/lib/tmp.js:358:16) at /path/to/tmp/node-tmp/repro.js:5:3 at _dirCreated (/path/to/tmp/node-tmp/lib/tmp.js:207:7) at FSReqCallback.oncomplete (node:fs:192:23) This occurs because 00bb5b2 upgraded the rimraf dependency from ^3.0.0 to ^5.0.5 without handling the change to a `Promise`-based API in 4.0.0 (isaacs/rimraf@a71e7f9) or the removal of the default export in 5.0.0 (isaacs/rimraf@c7a3fd4). This commit fixes the issue by dropping the `rimraf` dependency in favor of `fs.rm({recursive: true})`. Fixes: 00bb5b2 ("Update rimraf and drop old Node compatibility") Fixes: #295 Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1 parent 9fcf3ce commit 4e89567

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
 

‎lib/tmp.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const os = require('os');
1414
const path = require('path');
1515
const crypto = require('crypto');
1616
const _c = { fs: fs.constants, os: os.constants };
17-
const rimraf = require('rimraf');
1817

1918
/*
2019
* The working inner variables.
@@ -43,12 +42,42 @@ const
4342
_removeObjects = [],
4443

4544
// API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
46-
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
47-
FN_RIMRAF_SYNC = rimraf.sync;
45+
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs);
4846

4947
let
5048
_gracefulCleanup = false;
5149

50+
/**
51+
* Recursively remove a directory and its contents.
52+
*
53+
* @param {string} dirPath path of directory to remove
54+
* @param {Function} callback
55+
*/
56+
function rimraf(dirPath, callback) {
57+
// fs.rm() was added in Node.js 14.14.
58+
// Use deprecated rmdir({recursive: true}) on older versions.
59+
if (!fs.rm) {
60+
return fs.rmdir(dirPath, { recursive: true }, callback);
61+
}
62+
63+
return fs.rm(dirPath, { recursive: true }, callback);
64+
}
65+
66+
/**
67+
* Recursively remove a directory and its contents, synchronously.
68+
*
69+
* @param {string} dirPath path of directory to remove
70+
*/
71+
function FN_RIMRAF_SYNC(dirPath) {
72+
// fs.rm() was added in Node.js 14.14.
73+
// Use deprecated rmdir({recursive: true}) on older versions.
74+
if (!fs.rm) {
75+
return fs.rmdirSync(dirPath, { recursive: true });
76+
}
77+
78+
return fs.rmSync(dirPath, { recursive: true });
79+
}
80+
5281
/**
5382
* Gets a temporary file name.
5483
*

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"node": ">=14"
2626
},
2727
"dependencies": {
28-
"rimraf": "^5.0.5"
2928
},
3029
"devDependencies": {
3130
"eslint": "^6.3.0",

0 commit comments

Comments
 (0)