forked from dangmai/prettier-plugin-apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.ts
47 lines (41 loc) · 1.23 KB
/
http-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
import { ChildProcess, spawn } from "child_process";
import path from "path";
import util from "util";
import waitOn from "wait-on";
import { getSerializerBinDirectory } from "./util.js";
const waitOnPromise = util.promisify(waitOn);
export async function start(
host: string,
port: number,
password: string,
allowedOrigins?: string,
): Promise<ChildProcess> {
let serializerBin = getSerializerBinDirectory();
if (process.platform === "win32") {
serializerBin = path.join(serializerBin, "apex-ast-serializer-http.bat");
} else {
serializerBin = path.join(serializerBin, "apex-ast-serializer-http");
}
const args = ["-s", "-a", password, "-h", host, "-p", port.toString()];
if (allowedOrigins !== undefined) {
args.push("-c", allowedOrigins);
}
const command = spawn(serializerBin, args, {
stdio: "inherit",
});
await waitOnPromise({
resources: [`http://${host}:${port}/api/ast`],
});
// eslint-disable-next-line no-console
console.log(`Server listening on http://${host}:${port}`);
return command;
}
export async function stop(
host: string,
port: number,
password: string,
): Promise<Response> {
return fetch(`http://${host}:${port}/shutdown?token=${password}`, {
method: "POST",
});
}