-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
895fbde
commit 6452436
Showing
3 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |