forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.js
77 lines (65 loc) · 2.07 KB
/
simulator.js
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
74
75
76
77
import os from "os";
import path from "path";
import fs from "fs";
import waitOn from "wait-on";
import axios from "axios";
import { spawn } from "child_process";
import { Transform } from "stream";
function workerPort() {
const index = process.env.TEST_WORKER_INDEX * 1;
return 12000 + index;
}
function logPrefix() {
return `[worker:${process.env.TEST_WORKER_INDEX}]`;
}
function createSteamLog() {
return new Transform({
transform(chunk, encoding, callback) {
const lines = chunk.toString().split("\n");
lines.forEach((line) => {
if (line.trim()) log(line);
});
callback();
},
});
}
function log(...args) {
console.log(logPrefix(), ...args);
}
export function simulatorHost() {
return `localhost:${workerPort()}`;
}
export function simulatorUrl() {
return `http://${simulatorHost()}`;
}
export function simulatorConfig() {
const input = "./tests/simulator.evcc.yaml";
const content = fs.readFileSync(input, "utf8");
const result = content.replace(/localhost:7072/g, simulatorHost());
const resultName = "simulator.evcc.generated.yaml";
const resultPath = path.join(os.tmpdir(), resultName);
fs.writeFileSync(resultPath, result);
return resultPath;
}
export async function startSimulator() {
const port = workerPort();
log("starting simulator", { port });
log(`wait until port ${port} is available`);
await waitOn({ resources: [`tcp:${port}`], reverse: true, log: true });
const instance = spawn("npm", ["run", "simulator", "--", "--port", port]);
const steamLog = createSteamLog();
instance.stdout.pipe(steamLog);
instance.stderr.pipe(steamLog);
instance.on("exit", (code) => {
log("simulator terminated", { code, port });
steamLog.end();
});
await waitOn({ resources: [`${simulatorUrl()}/api/state`], log: true });
}
export async function stopSimulator() {
const port = workerPort();
log("shutting down simulator", { port });
await axios.post(`${simulatorUrl()}/api/shutdown`);
log(`wait until port ${port} is closed`);
await waitOn({ resources: [`tcp:localhost:${port}`], reverse: true });
}