Skip to content

Commit

Permalink
Move logic to determine task type into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
niklashigi committed Apr 17, 2022
1 parent 24854f9 commit c911d1e
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c911d1e

Please sign in to comment.