|
1 | 1 | import { ChildProcess } from "./wrappers/child-process"; |
2 | | -import * as Promise from "bluebird"; |
3 | 2 | import * as path from "path"; |
4 | 3 |
|
5 | 4 | export class SysInfo { |
6 | 5 | // Different java has different format for `java -version` command. |
7 | 6 | private static JAVA_VERSION_REGEXP = /(?:openjdk|java) version \"((?:\d+\.)+(?:\d+))/i; |
8 | 7 |
|
9 | | - // For other versions of java javac version output is not on first line. |
10 | | - // Thus can't use ^ for starts with in regex. |
11 | | - private static JAVA_COMPILER_VERSION_REGEXP = /javac (.*)/i; |
| 8 | + private static JAVA_COMPILER_VERSION_REGEXP = /^javac (.*)/im; |
12 | 9 |
|
13 | 10 | private javaVerCache: string; |
14 | 11 | private javaCompilerVerCache: string; |
15 | 12 |
|
16 | 13 | constructor(private childProcess: ChildProcess) { } |
17 | 14 |
|
18 | | - public getJavaVersion(): Promise<string> { |
| 15 | + public async getJavaVersion(): Promise<string> { |
19 | 16 | if (!this.javaVerCache) { |
20 | | - return new Promise<string>((resolve, reject) => { |
21 | | - this.childProcess.spawnFromEvent("java", ["-version"], "exit") |
22 | | - .then((spawnResult) => { |
23 | | - this.javaVerCache = SysInfo.JAVA_VERSION_REGEXP.exec(spawnResult.stderr)[1]; |
24 | | - resolve(this.javaVerCache); |
25 | | - }) |
26 | | - .catch((err) => { |
27 | | - this.javaVerCache = null; |
28 | | - resolve(this.javaVerCache); |
29 | | - }); |
30 | | - }); |
| 17 | + const spawnResult = await this.childProcess.spawnFromEvent("java", ["-version"], "exit", { ignoreError: true }); |
| 18 | + const matches = spawnResult && SysInfo.JAVA_VERSION_REGEXP.exec(spawnResult.stderr); |
| 19 | + this.javaVerCache = matches && matches[1]; |
31 | 20 | } |
32 | 21 |
|
33 | | - return Promise.resolve(this.javaVerCache); |
| 22 | + return this.javaVerCache; |
34 | 23 | } |
35 | 24 |
|
36 | | - public getJavaCompilerVersion(): Promise<string> { |
| 25 | + public async getJavaCompilerVersion(): Promise<string> { |
37 | 26 | if (!this.javaCompilerVerCache) { |
38 | | - return new Promise<string>((resolve, reject) => { |
39 | | - let javaCompileExecutableName = "javac"; |
40 | | - let javaHome = process.env.JAVA_HOME; |
41 | | - let pathToJavaCompilerExecutable = javaHome ? path.join(javaHome, "bin", javaCompileExecutableName) : javaCompileExecutableName; |
42 | | - this.childProcess.exec(`"${pathToJavaCompilerExecutable}" -version`) |
43 | | - .then((output) => { |
44 | | - this.javaCompilerVerCache = output ? SysInfo.JAVA_COMPILER_VERSION_REGEXP.exec(output.stderr)[1] : null; |
45 | | - resolve(this.javaCompilerVerCache); |
46 | | - }) |
47 | | - .catch((err) => { |
48 | | - this.javaCompilerVerCache = null; |
49 | | - resolve(this.javaCompilerVerCache); |
50 | | - }); |
51 | | - }); |
| 27 | + const javaCompileExecutableName = "javac"; |
| 28 | + const javaHome = process.env.JAVA_HOME; |
| 29 | + const pathToJavaCompilerExecutable = javaHome ? path.join(javaHome, "bin", javaCompileExecutableName) : javaCompileExecutableName; |
| 30 | + try { |
| 31 | + const output = await this.childProcess.exec(`"${pathToJavaCompilerExecutable}" -version`); |
| 32 | + this.javaCompilerVerCache = SysInfo.JAVA_COMPILER_VERSION_REGEXP.exec(output.stderr)[1]; |
| 33 | + } catch (err) { |
| 34 | + this.javaCompilerVerCache = null; |
| 35 | + } |
52 | 36 | } |
53 | 37 |
|
54 | | - return Promise.resolve(this.javaCompilerVerCache); |
| 38 | + return this.javaCompilerVerCache; |
55 | 39 | } |
56 | 40 | } |
0 commit comments