Skip to content

Commit cdc2964

Browse files
Fix PR comments
1 parent 839b92d commit cdc2964

File tree

11 files changed

+65
-1074
lines changed

11 files changed

+65
-1074
lines changed

.npmignore

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ bin/nativescript
1818
bin/*.cmd
1919

2020
lib/**/*.ts
21-
!lib/**/*.d.ts
2221
lib/**/*.js.map
2322

2423
test/

lib/declarations.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ interface IProcessInfo {
1818
exitCode?: number;
1919
}
2020

21+
interface ISpawnFromEventOptions {
22+
spawnOptions?: any;
23+
ignoreError?: boolean;
24+
}
25+
2126
interface IDictionary<T> {
2227
[key: string]: T
2328
}

lib/definitions/bluebird.d.ts

-782
This file was deleted.

lib/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import * as Promise from "bluebird";
21
import { ChildProcess } from "./wrappers/child-process";
32
import { SysInfo } from "./sys-info";
43

5-
let childProcess = new ChildProcess();
6-
let sysInfo = new SysInfo(childProcess);
4+
const childProcess = new ChildProcess();
5+
const sysInfo = new SysInfo(childProcess);
76

8-
function getJavaVersion(): Promise<string> {
7+
const getJavaVersion = (): Promise<string> => {
98
return sysInfo.getJavaVersion();
109
};
1110

12-
function getJavaCompilerVersion(): Promise<string> {
11+
const getJavaCompilerVersion = (): Promise<string> => {
1312
return sysInfo.getJavaCompilerVersion();
1413
};
1514

lib/sys-info.ts

+17-33
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
11
import { ChildProcess } from "./wrappers/child-process";
2-
import * as Promise from "bluebird";
32
import * as path from "path";
43

54
export class SysInfo {
65
// Different java has different format for `java -version` command.
76
private static JAVA_VERSION_REGEXP = /(?:openjdk|java) version \"((?:\d+\.)+(?:\d+))/i;
87

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;
129

1310
private javaVerCache: string;
1411
private javaCompilerVerCache: string;
1512

1613
constructor(private childProcess: ChildProcess) { }
1714

18-
public getJavaVersion(): Promise<string> {
15+
public async getJavaVersion(): Promise<string> {
1916
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];
3120
}
3221

33-
return Promise.resolve(this.javaVerCache);
22+
return this.javaVerCache;
3423
}
3524

36-
public getJavaCompilerVersion(): Promise<string> {
25+
public async getJavaCompilerVersion(): Promise<string> {
3726
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+
}
5236
}
5337

54-
return Promise.resolve(this.javaCompilerVerCache);
38+
return this.javaCompilerVerCache;
5539
}
5640
}

lib/wrappers/child-process.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as childProcess from "child_process";
2-
import * as Promise from "bluebird";
32

43
export class ChildProcess {
5-
public spawnFromEvent(command: string, args: string[], event: string, options?: childProcess.SpawnOptions, ignoreError: boolean = false): Promise<IProcessInfo> {
4+
public spawnFromEvent(command: string, args: string[], event: string, options?: ISpawnFromEventOptions): Promise<IProcessInfo> {
65
return new Promise<IProcessInfo>((resolve, reject) => {
7-
let commandChildProcess = childProcess.spawn(command, args, options);
6+
options = options || {};
7+
const commandChildProcess = childProcess.spawn(command, args, options.spawnOptions);
88
let capturedOut = "";
99
let capturedErr = "";
1010

@@ -21,14 +21,14 @@ export class ChildProcess {
2121
}
2222

2323
commandChildProcess.on(event, (arg: any) => {
24-
let exitCode = typeof arg === "number" ? arg : arg && arg.code;
25-
let result = {
24+
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
25+
const result = {
2626
stdout: capturedOut,
2727
stderr: capturedErr,
2828
exitCode: exitCode
2929
};
3030

31-
if (ignoreError) {
31+
if (options.ignoreError) {
3232
resolve(result);
3333
} else {
3434
if (exitCode === 0) {
@@ -39,21 +39,21 @@ export class ChildProcess {
3939
errorMessage += ` Error output: \n ${capturedErr}`;
4040
}
4141

42-
throw new Error(errorMessage);
42+
reject(errorMessage);
4343
}
4444
}
4545
});
4646

4747
commandChildProcess.once("error", (err: Error) => {
48-
if (ignoreError) {
49-
let result = {
48+
if (options.ignoreError) {
49+
const result = {
5050
stdout: capturedOut,
5151
stderr: err.message,
5252
exitCode: (<any>err).code
5353
};
5454
resolve(result);
5555
} else {
56-
throw err;
56+
reject(err);
5757
}
5858
});
5959
});
@@ -63,10 +63,10 @@ export class ChildProcess {
6363
return new Promise<IProcessInfo>((resolve, reject) => {
6464
childProcess.exec(command, options, (err, stdout, stderr) => {
6565
if (err) {
66-
throw err;
66+
reject(err);
6767
}
6868

69-
let result: IProcessInfo = {
69+
const result: IProcessInfo = {
7070
stdout,
7171
stderr
7272
};

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"homepage": "https://github.com/NativeScript/nativescript-doctor#readme",
2525
"devDependencies": {
26+
"@types/mocha": "2.2.32",
2627
"grunt": "1.0.1",
2728
"grunt-contrib-clean": "1.0.0",
2829
"grunt-contrib-watch": "1.0.0",
@@ -35,7 +36,5 @@
3536
"tslint": "3.15.1",
3637
"typescript": "2.0.3"
3738
},
38-
"dependencies": {
39-
"bluebird": "3.4.6"
40-
}
39+
"dependencies": {}
4140
}

0 commit comments

Comments
 (0)