1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2025-04-26 07:45:23 -04:00

don't delete the temp dir if set manually and dir exists (fixes #3)

This commit is contained in:
Jake Jarvis 2021-10-06 11:30:01 -04:00
parent 933899d6de
commit 8567cfc76a
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39

View File

@ -9,6 +9,14 @@ import decompress from "decompress";
import urlParse from "url-parse";
export default async function downloader(downloadUrl, checksumUrl, options) {
options = options || {};
// don't delete the temp dir if set manually and dir exists
let deleteTempDir = true;
if (options.tempDir && fs.pathExistsSync(options.tempDir)) {
deleteTempDir = false;
}
// normalize options and set defaults
options = {
filename: options.filename || urlParse(downloadUrl).pathname.split("/").pop(),
@ -48,9 +56,11 @@ export default async function downloader(downloadUrl, checksumUrl, options) {
return path.join(options.destDir, options.filename);
}
} finally {
// delete temporary directory
// delete temporary directory (except for edge cases above)
if (deleteTempDir) {
await fs.remove(options.tempDir);
}
}
}
// Download any file to any destination. Returns a promise.