diff --git a/index.d.ts b/index.d.ts index 05167ae..ed77aea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 diff --git a/index.js b/index.js index 26f0427..5d9852b 100644 --- a/index.js +++ b/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); diff --git a/package.json b/package.json index bb72cd5..d572f30 100644 --- a/package.json +++ b/package.json @@ -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",