fs-jetpack
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -0,1 +1,4 @@ | ||
# 2.2.2 (2019-02-19) | ||
- Use rimraf as internal implementation of `remove()` to fix https://github.com/szwacz/fs-jetpack/issues/80 | ||
# 2.2.1 (2019-01-20) | ||
@@ -2,0 +5,0 @@ - `find()` no longer crashes on ENOENT (e.g. when one of the files has been deleted by other process). |
"use strict"; | ||
const pathUtil = require("path"); | ||
const fs = require("./utils/fs"); | ||
const rimraf = require('rimraf'); | ||
const promisify = require('./utils/promisify'); | ||
const promisifiedRimraf = promisify(rimraf); | ||
const validate = require("./utils/validate"); | ||
const list = require("./list"); | ||
@@ -18,25 +18,3 @@ const validateInput = (methodName, path) => { | ||
const removeSync = path => { | ||
try { | ||
// Assume the path is a file and just try to remove it. | ||
fs.unlinkSync(path); | ||
} catch (err) { | ||
if ( | ||
err.code === "EPERM" || | ||
err.code === "EISDIR" || | ||
err.code === "ENOTEMPTY" | ||
) { | ||
// Must delete everything inside the directory first. | ||
list.sync(path).forEach(filename => { | ||
removeSync(pathUtil.join(path, filename)); | ||
}); | ||
// Everything inside directory has been removed, | ||
// it's safe now do go for the directory itself. | ||
fs.rmdirSync(path); | ||
} else if (err.code === "ENOENT") { | ||
// File already doesn't exist. We're done. | ||
} else { | ||
// Something unexpected happened. Rethrow original error. | ||
throw err; | ||
} | ||
} | ||
rimraf.sync(path, { disableGlob: true }); | ||
}; | ||
@@ -48,70 +26,4 @@ | ||
const removeAsyncInternal = (path, retryCount) => { | ||
return new Promise((resolve, reject) => { | ||
const retryInAWhileOrFail = err => { | ||
if (retryCount === 3) { | ||
// Too many retries already. Fail. | ||
reject(err); | ||
} else { | ||
// Try the same action after some pause. | ||
setTimeout(() => { | ||
removeAsyncInternal(path, retryCount + 1).then(resolve, reject); | ||
}, 100); | ||
} | ||
}; | ||
const removeEverythingInsideDirectory = () => { | ||
return list.async(path).then(filenamesInsideDir => { | ||
const promises = filenamesInsideDir.map(filename => { | ||
return removeAsyncInternal(pathUtil.join(path, filename), 0); | ||
}); | ||
return Promise.all(promises); | ||
}); | ||
}; | ||
// Assume the path is a file and just try to remove it. | ||
fs.unlink(path) | ||
.then(resolve) | ||
.catch(err => { | ||
if (err.code === "EBUSY") { | ||
retryInAWhileOrFail(err); | ||
} else if ( | ||
err.code === "EPERM" || | ||
err.code === "EISDIR" || | ||
err.code === "ENOTEMPTY" | ||
) { | ||
// File deletion attempt failed. Probably it's not a file, it's a directory. | ||
// So try to proceed with that assumption. | ||
removeEverythingInsideDirectory() | ||
.then(() => { | ||
// Now go for the directory. | ||
return fs.rmdir(path); | ||
}) | ||
.then(resolve) | ||
.catch(err2 => { | ||
if ( | ||
err2.code === "EBUSY" || | ||
err2.code === "EPERM" || | ||
err2.code === "ENOTEMPTY" | ||
) { | ||
// Failed again. This might be due to other processes reading | ||
// something inside the directory. Let's take a nap and retry. | ||
retryInAWhileOrFail(err2); | ||
} else { | ||
reject(err2); | ||
} | ||
}); | ||
} else if (err.code === "ENOENT") { | ||
// File already doesn't exist. We're done. | ||
resolve(); | ||
} else { | ||
// Something unexpected happened. Rethrow original error. | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
}; | ||
const removeAsync = path => { | ||
return removeAsyncInternal(path, 0); | ||
return promisifiedRimraf(path, { disableGlob: true }); | ||
}; | ||
@@ -118,0 +30,0 @@ |
{ | ||
"name": "fs-jetpack", | ||
"description": "Better file system API", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"author": "Jakub Szwacz <jakub@szwacz.com>", | ||
"dependencies": { | ||
"minimatch": "^3.0.2" | ||
"minimatch": "^3.0.2", | ||
"rimraf": "^2.6.3" | ||
}, | ||
@@ -9,0 +10,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
273616
2
7998
+ Addedrimraf@^2.6.3
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedwrappy@1.0.2(transitive)