forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradle-command-service.ts
65 lines (58 loc) · 1.5 KB
/
gradle-command-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
IChildProcess,
IErrors,
IHostInfo,
ISpawnResult,
ISpawnFromEventOptions,
} from "../../common/declarations";
import {
IGradleCommandService,
IGradleCommandOptions,
} from "../../definitions/gradle";
import { injector } from "../../common/yok";
export class GradleCommandService implements IGradleCommandService {
constructor(
private $childProcess: IChildProcess,
private $errors: IErrors,
private $hostInfo: IHostInfo,
private $logger: ILogger
) {}
public async executeCommand(
gradleArgs: string[],
options: IGradleCommandOptions
): Promise<ISpawnResult> {
const { message, cwd, stdio, spawnOptions } = options;
this.$logger.info(message);
const childProcessOptions = { cwd, stdio: stdio || "inherit" };
const gradleExecutable =
options.gradlePath ??
(this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew");
const result = await this.executeCommandSafe(
gradleExecutable,
gradleArgs,
childProcessOptions,
spawnOptions
);
return result;
}
private async executeCommandSafe(
gradleExecutable: string,
gradleArgs: string[],
childProcessOptions: { cwd: string; stdio: string },
spawnOptions: ISpawnFromEventOptions
): Promise<ISpawnResult> {
try {
const result = await this.$childProcess.spawnFromEvent(
gradleExecutable,
gradleArgs,
"close",
childProcessOptions,
spawnOptions
);
return result;
} catch (err) {
this.$errors.fail(err.message);
}
}
}
injector.register("gradleCommandService", GradleCommandService);