mirror of
https://github.com/jakejarvis/careful-downloader.git
synced 2025-04-26 03:05:23 -04:00
add cleanDestDir
option
This commit is contained in:
parent
bd8b8047d7
commit
5008874502
15
index.d.ts
vendored
15
index.d.ts
vendored
@ -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
|
||||
|
8
index.js
8
index.js
@ -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);
|
||||
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user