diff --git a/src/cli.ts b/src/cli.ts index d86bb7e..5c15fc9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -56,35 +56,9 @@ async function main() { } const inputPath = path.resolve(process.cwd(), input) - const fileExtension = path.extname(input) - const baseName = path.basename(input, fileExtension) - const outputName = `${baseName}-patched${fileExtension}` - let outputPath = path.resolve(path.dirname(inputPath), outputName) - let skipDecode = false - - let isAppBundle = false - let taskFunction: (options: TaskOptions) => Listr - switch (fileExtension) { - case '.apk': - taskFunction = patchApk - break - case '.xapk': - isAppBundle = true - taskFunction = patchXapkBundle - break - case '.apks': - case '.zip': - isAppBundle = true - taskFunction = patchApksBundle - break - case '': - taskFunction = patchApk - skipDecode = true - outputPath += '.apk' - break - default: - showSupportedExtensions() - } + const { taskFunction, skipDecode, isAppBundle, outputName } = + await determineTask(inputPath) + const outputPath = path.resolve(path.dirname(inputPath), outputName) // Initialize and validate certificate path let certificatePath: string | undefined @@ -177,6 +151,51 @@ async function main() { }) } +/** + * Determines the correct "task" (e.g. "patch APK" or "patch XAPK") depending on + * the input path's type (file or directory) and extension (e.g. ".apk"). + */ +async function determineTask(inputPath: string) { + const fileStats = await fs.stat(inputPath) + + let outputFileExtension = '.apk' + + let skipDecode = false + let isAppBundle = false + let taskFunction: (options: TaskOptions) => Listr + + if (fileStats.isDirectory()) { + taskFunction = patchApk + skipDecode = true + } else { + const inputFileExtension = path.extname(inputPath) + + switch (inputFileExtension) { + case '.apk': + taskFunction = patchApk + break + case '.xapk': + isAppBundle = true + taskFunction = patchXapkBundle + break + case '.apks': + case '.zip': + isAppBundle = true + taskFunction = patchApksBundle + break + default: + showSupportedExtensions() + } + + outputFileExtension = inputFileExtension + } + + const baseName = path.basename(inputPath, outputFileExtension) + const outputName = `${baseName}-patched${outputFileExtension}` + + return { skipDecode, taskFunction, isAppBundle, outputName } +} + function getErrorMessage(error: PatchingError, { tmpDir }: { tmpDir: string }) { // User errors can be shown without a stack trace if (error instanceof UserError) return error.message