diff --git a/lib/tmp.js b/lib/tmp.js index 1af6031..4270d03 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -41,8 +41,6 @@ const SIGINT = 'SIGINT', - SIGINT_LISTENER_NAME = '_tmp$sigint_listener' - // this will hold the objects need to be removed on exit _removeObjects = []; @@ -447,6 +445,8 @@ function _rimrafRemoveDirSyncWrapper(dirPath, next) { } } +const FN_RMDIR_SYNC = fs.rmdirSync.bind(fs); + /** * Prepares the callback for removal of the temporary directory. * @@ -457,7 +457,7 @@ function _rimrafRemoveDirSyncWrapper(dirPath, next) { */ function _prepareTmpDirRemoveCallback(name, opts) { const removeFunction = opts.unsafeCleanup ? _rimrafRemoveDirWrapper : fs.rmdir.bind(fs); - const removeFunctionSync = opts.unsafeCleanup ? _rimrafRemoveDirSyncWrapper : fs.rmdirSync.bind(fs); + const removeFunctionSync = opts.unsafeCleanup ? _rimrafRemoveDirSyncWrapper : FN_RMDIR_SYNC; const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name); const removeCallback = _prepareRemoveCallback(removeFunction, name, removeCallbackSync); if (!opts.keep) _removeObjects.unshift(removeCallbackSync); @@ -497,7 +497,14 @@ function _prepareRemoveCallback(removeFunction, arg, cleanupCallbackSync) { // we will ignore the error return next(err); } - } else return removeFunction(arg, next); + } else { + // must no call rmdirSync/rmSync this way + if (removeFunction == FN_RMDIR_SYNC) { + return removeFunction(arg); + } else { + return removeFunction(arg, next); + } + } } else return next(new Error('cleanup callback has already been called')); }; } @@ -610,43 +617,33 @@ function _is_legacy_listener(listener) { */ function _safely_install_sigint_listener() { - let listeners = process.listeners(SIGINT); + const listeners = process.listeners(SIGINT); const existingListeners = []; for (let i = 0, length = listeners.length; i < length; i++) { const lstnr = listeners[i]; /* istanbul ignore else */ - if (lstnr.name === SIGINT_LISTENER_NAME) { + if (lstnr.name === '_tmp$sigint_listener') { existingListeners.push(lstnr); process.removeListener(SIGINT, lstnr); } } - process.on(SIGINT, function _tmp$sigint_listener() { + process.on(SIGINT, function _tmp$sigint_listener(doExit) { for (let i = 0, length = existingListeners.length; i < length; i++) { + // let the existing listener do the garbage collection (e.g. jest sandbox) try { - // let the existing listener do the garbage collection (e.g. jest sandbox) - // pass in false in case that we are dealing with an old version of this - existingListeners[i](false); + existingListeners[i](false); } catch (err) { // ignore } } - // check if there are any user installed sigint listeners - // if there are none, exit the process - let doExit = true; - listeners = process.listeners(SIGINT); - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - if (lstnr.name !== SIGINT_LISTENER_NAME) { - doExit = false; - break; - } - } - // force the garbage collector even it is called again in the exit listener - _garbageCollector(); - // exit the process if no user listeners have been installed - if (doExit) { - process.exit(); - } + try { + // force the garbage collector even it is called again in the exit listener + _garbageCollector(); + } finally { + if (!!doExit) { + process.exit(0); + } + } }); } @@ -672,11 +669,12 @@ function _safely_install_exit_listener() { process.removeListener(EXIT, lstnr); } } - process.addListener(EXIT, function _tmp$safe_listener(code) { + // TODO: what was the data parameter good for? + process.addListener(EXIT, function _tmp$safe_listener(data) { for (let i = 0, length = existingListeners.length; i < length; i++) { + // let the existing listener do the garbage collection (e.g. jest sandbox) try { - // let the existing listener do the garbage collection (e.g. jest sandbox) - existingListeners[i](code); + existingListeners[i](data); } catch (err) { // ignore } diff --git a/package.json b/package.json index 6d28fc7..3973e2e 100644 --- a/package.json +++ b/package.json @@ -22,13 +22,13 @@ "node": ">=6" }, "dependencies": { - "rimraf": "^2.6.3" + "rimraf": "^3.0.0" }, "devDependencies": { - "eslint": "^4.19.1", - "eslint-plugin-mocha": "^5.0.0", + "eslint": "^6.3.0", + "eslint-plugin-mocha": "^6.1.1", "istanbul": "^0.4.5", - "mocha": "^5.1.1" + "mocha": "^6.2.0" }, "main": "lib/tmp.js", "files": [