Skip to content

Commit

Permalink
feat: option to disable overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqmanuja committed Oct 25, 2024
1 parent 538947b commit b14f64c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -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.

Expand Down
24 changes: 20 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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 => {
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { existsSync } from "fs";

import { match } from "ts-pattern";

import { cleanObject } from "../utils/object";
Expand Down Expand Up @@ -30,6 +32,7 @@ const DEFAULT_APP_OPTIONS = {
version: "stable",
arch: "universal",
dpi: "nodpi",
overwrite: true,
} satisfies AppOptions;

export type APKMDOptionsWithSuggestions = APKMDOptions & {
Expand Down Expand Up @@ -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 };
});
}
}
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export type AppOptions = {
minAndroidVersion?: string;
outFile?: string;
outDir?: string;
overwrite?: boolean;
};

0 comments on commit b14f64c

Please sign in to comment.