diff --git a/lib/tmp.js b/lib/tmp.js index 6d25f53..05a65fb 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -45,7 +45,8 @@ const _removeObjects = [], // API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback - FN_RMDIR_SYNC = fs.rmdirSync.bind(fs); + FN_RMDIR_SYNC = fs.rmdirSync.bind(fs), + FN_RIMRAF_SYNC = rimraf.sync; var _gracefulCleanup = false; @@ -432,31 +433,6 @@ function _prepareTmpFileRemoveCallback(name, fd, opts, sync) { return sync ? removeCallbackSync : removeCallback; } -/** - * Simple wrapper for rimraf. - * - * @param {string} dirPath - * @param {Function} next - * @private - */ -function _rimrafRemoveDirWrapper(dirPath, next) { - rimraf(dirPath, next); -} - -/** - * Simple wrapper for rimraf.sync. - * - * @param {string} dirPath - * @private - */ -function _rimrafRemoveDirSyncWrapper(dirPath, next) { - try { - return next(null, rimraf.sync(dirPath)); - } catch (err) { - return next(err); - } -} - /** * Prepares the callback for removal of the temporary directory. * @@ -470,8 +446,8 @@ function _rimrafRemoveDirSyncWrapper(dirPath, next) { * @private */ function _prepareTmpDirRemoveCallback(name, opts, sync) { - const removeFunction = opts.unsafeCleanup ? _rimrafRemoveDirWrapper : fs.rmdir.bind(fs); - const removeFunctionSync = opts.unsafeCleanup ? _rimrafRemoveDirSyncWrapper : FN_RMDIR_SYNC; + const removeFunction = opts.unsafeCleanup ? rimraf : fs.rmdir.bind(fs); + const removeFunctionSync = opts.unsafeCleanup ? FN_RIMRAF_SYNC : FN_RMDIR_SYNC; const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync); const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync); if (!opts.keep) _removeObjects.unshift(removeCallbackSync); @@ -507,7 +483,7 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall if (index >= 0) _removeObjects.splice(index, 1); called = true; - if (sync || removeFunction === FN_RMDIR_SYNC) { + if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction == FN_RIMRAF_SYNC) { return removeFunction(fileOrDirName); } else { return removeFunction(fileOrDirName, next || function() {});