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

add cleanDestDir option

This commit is contained in:
Jake Jarvis 2021-10-05 11:07:02 -04:00
parent bd8b8047d7
commit 5008874502
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
3 changed files with 19 additions and 6 deletions

15
index.d.ts vendored
View File

@ -14,6 +14,14 @@ export interface Options {
*/
readonly extract?: boolean;
/**
* Path to temporary directory for unverified and/or unextracted downloads.
* Automatically generated if not set (recommended).
*
* @default `tempy.directory()`
*/
readonly tempDir?: string;
/**
* Full path or directory name relative to module to store the validated
* download.
@ -23,12 +31,11 @@ export interface Options {
readonly destDir?: string;
/**
* Path to temporary directory for unverified and/or unextracted downloads.
* Automatically generated if not set (recommended).
* Delete any existing files in the destination directory before downloading.
*
* @default `tempy.directory()`
* @default false
*/
readonly tempDir?: string;
readonly cleanDestDir?: boolean;
/**
* The algorithm used by the checksum file. Available options are dependent on

View File

@ -17,8 +17,9 @@ export default async function downloadAndCheck(downloadUrl, checksumUrl, options
options = {
filename: options.filename ?? urlParse(downloadUrl).pathname.split("/").pop(),
extract: !!options.extract,
destDir: options.destDir ? path.resolve(__dirname, options.destDir) : path.resolve(__dirname, "download"),
tempDir: options.tempDir ? path.resolve(__dirname, options.tempDir) : tempy.directory(),
destDir: options.destDir ? path.resolve(__dirname, options.destDir) : path.resolve(__dirname, "download"),
cleanDestDir: !!options.cleanDestDir,
algorithm: options.algorithm ?? "sha256",
encoding: options.encoding ?? "binary",
};
@ -33,6 +34,11 @@ export default async function downloadAndCheck(downloadUrl, checksumUrl, options
// validate the checksum of the download
await checkChecksum(options.tempDir, path.join(options.tempDir, "checksums.txt"), options.filename, options.algorithm, options.encoding);
// optionally clear the target directory of existing files
if (options.cleanDestDir) {
await fs.remove(options.destDir);
}
// ensure the target directory exists
await fs.mkdirp(options.destDir);

View File

@ -14,7 +14,7 @@
"url": "https://github.com/jakejarvis/careful-download.git"
},
"type": "module",
"module": "./index.js",
"exports": "./index.js",
"types": "./index.d.ts",
"files": [
"index.js",