You've already forked careful-downloader
mirror of
https://github.com/jakejarvis/careful-downloader.git
synced 2025-07-04 09:56:40 -04:00
add cleanDestDir
option
This commit is contained in:
15
index.d.ts
vendored
15
index.d.ts
vendored
@ -14,6 +14,14 @@ export interface Options {
|
|||||||
*/
|
*/
|
||||||
readonly extract?: boolean;
|
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
|
* Full path or directory name relative to module to store the validated
|
||||||
* download.
|
* download.
|
||||||
@ -23,12 +31,11 @@ export interface Options {
|
|||||||
readonly destDir?: string;
|
readonly destDir?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to temporary directory for unverified and/or unextracted downloads.
|
* Delete any existing files in the destination directory before downloading.
|
||||||
* Automatically generated if not set (recommended).
|
|
||||||
*
|
*
|
||||||
* @default `tempy.directory()`
|
* @default false
|
||||||
*/
|
*/
|
||||||
readonly tempDir?: string;
|
readonly cleanDestDir?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The algorithm used by the checksum file. Available options are dependent on
|
* 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 = {
|
options = {
|
||||||
filename: options.filename ?? urlParse(downloadUrl).pathname.split("/").pop(),
|
filename: options.filename ?? urlParse(downloadUrl).pathname.split("/").pop(),
|
||||||
extract: !!options.extract,
|
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(),
|
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",
|
algorithm: options.algorithm ?? "sha256",
|
||||||
encoding: options.encoding ?? "binary",
|
encoding: options.encoding ?? "binary",
|
||||||
};
|
};
|
||||||
@ -33,6 +34,11 @@ export default async function downloadAndCheck(downloadUrl, checksumUrl, options
|
|||||||
// validate the checksum of the download
|
// validate the checksum of the download
|
||||||
await checkChecksum(options.tempDir, path.join(options.tempDir, "checksums.txt"), options.filename, options.algorithm, options.encoding);
|
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
|
// ensure the target directory exists
|
||||||
await fs.mkdirp(options.destDir);
|
await fs.mkdirp(options.destDir);
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
"url": "https://github.com/jakejarvis/careful-download.git"
|
"url": "https://github.com/jakejarvis/careful-download.git"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"module": "./index.js",
|
"exports": "./index.js",
|
||||||
"types": "./index.d.ts",
|
"types": "./index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"index.js",
|
"index.js",
|
||||||
|
Reference in New Issue
Block a user