Skip to content

Commit

Permalink
Add prerequisite checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shroudedcode committed Apr 17, 2021
1 parent 895fbde commit 6452436
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/patch-apk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import Listr = require('listr')
import chalk = require('chalk')

import { TaskOptions } from './cli'
import downloadTools from './tasks/download-tools'
import observeAsync from './utils/observe-async'
import applyPatches from './tasks/apply-patches'
import checkPrerequisites from './tasks/check-prerequisites'

export default function patchApk(options: TaskOptions) {
const { apktool, uberApkSigner } = options
Expand All @@ -19,8 +19,8 @@ export default function patchApk(options: TaskOptions) {

return new Listr([
{
title: 'Downloading tools',
task: () => downloadTools(options),
title: 'Checking prerequisities',
task: () => checkPrerequisites(options),
},
{
title: 'Decoding APK file',
Expand Down
43 changes: 43 additions & 0 deletions src/tasks/check-prerequisites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Listr = require('listr')

import { TaskOptions } from '../cli'
import getJavaVersion from '../utils/get-java-version'
import downloadTools from './download-tools'

const MIN_NODE_VERSION = 10
const MIN_JAVA_VERSION = 8

export default function checkPrerequisites(options: TaskOptions) {
return new Listr([
{
title: 'Checking Node.js version',
task: () => {
const majorVersion = parseInt(process.versions.node.split('.')[0])
if (majorVersion < MIN_NODE_VERSION)
throw new VersionError('Node.js', MIN_NODE_VERSION, majorVersion)
},
},
{
title: 'Checking Java version',
task: async () => {
const majorVersion = await getJavaVersion()
if (majorVersion < MIN_JAVA_VERSION)
throw new VersionError('Java', MIN_JAVA_VERSION, majorVersion)
},
},
{
title: 'Downloading tools',
task: () => downloadTools(options),
},
])
}

class VersionError extends Error {
constructor(tool: string, minVersion: number, currentVersion: number) {
super(
`apk-mitm requires at least ${tool} ${minVersion} to work and you seem to be using ${tool} ${currentVersion}.` +
` Please upgrade your ${tool} installation and try again!`,
)
this.name = VersionError.name
}
}
20 changes: 20 additions & 0 deletions src/utils/get-java-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import execa = require('execa')

/** Extracts the Java major version from the output of `java -version`. */
export default async function getJavaVersion() {
try {
const { stderr } = await execa('java', ['-version'])
const version = stderr.match(/"(\d+\.\d+\.\d+)/)![1]

// `.replace` needed to remove `1.` prefix from versions older than Java 9
return parseInt(version.replace(/^1\./, '').match(/^\d+/)![0])
} catch (error) {
if (error.code === 'ENOENT')
throw new Error(
'No "java" executable could be found!' +
' Make sure that Java is installed and available in your PATH.',
)

throw error
}
}

0 comments on commit 6452436

Please sign in to comment.