This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
angular-server.ts
73 lines (60 loc) · 2.65 KB
/
angular-server.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
66
67
68
69
70
71
72
73
import { Logger } from "./../test-explorer/logger";
import { SpawnOptions } from "child_process";
import spawn = require("cross-spawn");
import { KarmaEventListener } from "../karma/karma-event-listener";
import { window } from "vscode";
export class AngularServer {
private readonly logger: Logger;
private angularProcess: any;
public constructor(private angularProjectRootPath: string, private baseKarmaConfigFilePath: string) {
this.logger = new Logger();
}
public stopPreviousRun(): Promise<void> {
if (this.angularProcess != null) {
const karmaEventListener = KarmaEventListener.getInstance();
karmaEventListener.stopListeningToKarma();
this.angularProcess.kill();
}
return new Promise<void>(resolve => {
this.angularProcess.on("exit", (code: any, signal: any) => {
this.logger.log(`Angular exited with code ${code} and signal ${signal}`);
resolve();
});
});
}
public start(): void {
const baseKarmaConfigFilePath = require.resolve(this.baseKarmaConfigFilePath);
const options = {
cwd: this.angularProjectRootPath,
shell: true,
} as SpawnOptions;
const { cliCommand, cliArgs } = this.createAngularCommandAndArguments(baseKarmaConfigFilePath);
this.angularProcess = spawn(cliCommand, cliArgs, options);
this.logger.log(`Starting Angular tests with arguments: ${cliArgs.join(" ")}`);
// this.angularProcess.stdout.on("data", (data: any) => this.logger.log(`stdout: ${data}`));
this.angularProcess.stderr.on("data", (data: any) => this.logger.log(`stderr: ${data}`));
this.angularProcess.on("error", (err: any) => this.logger.log(`error from ng child process: ${err}`));
}
private createAngularCommandAndArguments(baseKarmaConfigFilePath: string) {
const fs = require("fs");
const path = require("path");
const resolveGlobal = require("resolve-global");
const isAngularInstalledGlobally = resolveGlobal.silent("@angular/cli") != null;
const isAngularInstalledLocally = fs.existsSync(path.join(this.angularProjectRootPath, "node_modules", "@angular", "cli", "bin", "ng"));
const commonArgs = ["test", `--karma-config="${baseKarmaConfigFilePath}"`, "--progress=false"];
let cliCommand: string = "";
let cliArgs: string[] = [];
if (isAngularInstalledGlobally) {
cliArgs = commonArgs;
cliCommand = "ng";
} else if (isAngularInstalledLocally) {
cliArgs = ["ng", ...commonArgs];
cliCommand = "npx";
} else {
const error = "@angular/cli is not installed, install it and restart vscode";
window.showErrorMessage(error);
throw Error(error);
}
return { cliCommand, cliArgs };
}
}