From b14f64c69caadb8e68666917076d88d52d511619 Mon Sep 17 00:00:00 2001 From: tanishqmanuja Date: Fri, 25 Oct 2024 17:34:55 +0530 Subject: [PATCH] feat: option to disable overwrite --- .github/README.md | 2 ++ src/cli.ts | 24 ++++++++++++++++++++---- src/lib/index.ts | 9 ++++++++- src/lib/types.ts | 1 + 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.github/README.md b/.github/README.md index 2623563..aef5b52 100644 --- a/.github/README.md +++ b/.github/README.md @@ -51,6 +51,7 @@ APKMirrorDownloader.download({ org: "google-inc", repo: "youtube" }); - dpi: Optional. The screen density of the application. For example, 240dpi, 320dpi, 480dpi, etc. - minAndroidVersion: Optional. The minimum Android version that the application is compatible with. - outDir: Optional. The output directory where the application files will be stored. +- overwrite: Optional. Whether to overwrite the output file if it already exists. 🟣 **AppOptions Interface** @@ -61,6 +62,7 @@ APKMirrorDownloader.download({ org: "google-inc", repo: "youtube" }); - minAndroidVersion: Optional. The minimum Android version that the application is compatible with. - outFile: Optional. The name of the output file where the application will be saved. - outDir: Optional. The output directory where the application files will be stored. +- overwrite: Optional. Whether to overwrite the output file if it already exists. `AppOptions` will be merged automatically with `APKMDOptions` when download function is called. diff --git a/src/cli.ts b/src/cli.ts index 986e64f..6edb564 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -45,8 +45,13 @@ yargs(process.argv.slice(2)) await apkmd .download({ org, repo }, options) .then( - ({ dest }) => { - console.log(`Downloaded to ${dest}`); + ({ dest, skipped }) => { + if (skipped) { + console.log(`Skipped ${org}/${repo}`); + console.log(`Already exists at ${dest}`); + } else { + console.log(`Downloaded to ${dest}`); + } }, e => { console.log(`Unable to download ${org}/${repo}`); @@ -106,6 +111,11 @@ yargs(process.argv.slice(2)) .option("outdir", { type: "string", describe: "Output directory", + }) + .option("overwrite", { + type: "boolean", + describe: "Overwrite existing files", + default: true, }); }, async argv => { @@ -121,11 +131,17 @@ yargs(process.argv.slice(2)) minAndroidVersion: argv.minandroidversion, outFile: argv.outfile, outDir: argv.outdir, + overwrite: argv.overwrite, }; await APKMirrorDownloader.download(app, options).then( - ({ dest }) => { - console.log(`Downloaded to ${dest}`); + ({ dest, skipped }) => { + if (skipped) { + console.log(`Skipped ${app.org}/${app.repo}`); + console.log(`Already exists at ${dest}`); + } else { + console.log(`Downloaded to ${dest}`); + } }, err => { console.error(err); diff --git a/src/lib/index.ts b/src/lib/index.ts index 332b450..1f6a93b 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,5 @@ +import { existsSync } from "fs"; + import { match } from "ts-pattern"; import { cleanObject } from "../utils/object"; @@ -30,6 +32,7 @@ const DEFAULT_APP_OPTIONS = { version: "stable", arch: "universal", dpi: "nodpi", + overwrite: true, } satisfies AppOptions; export type APKMDOptionsWithSuggestions = APKMDOptions & { @@ -154,8 +157,12 @@ export class APKMirrorDownloader { const outFile = ensureExtension(o.outFile ?? filename, extension); const dest = `${outDir}/${outFile}`; + if (!o.overwrite && existsSync(dest)) { + return { dest, skipped: true as const }; + } + await Bun.write(dest, res); - return { dest }; + return { dest, skipped: false as const }; }); } } diff --git a/src/lib/types.ts b/src/lib/types.ts index fb42d48..184c137 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -37,4 +37,5 @@ export type AppOptions = { minAndroidVersion?: string; outFile?: string; outDir?: string; + overwrite?: boolean; };