forked from webxdc/webxdc-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.ts
71 lines (59 loc) · 1.65 KB
/
run.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
import process from "process";
import open from "open";
import { createFrontend, InjectExpress } from "./app";
import { Instances, Options } from "./instance";
import { getLocation, Location, LocationError } from "./location";
import { getAppInfo, AppInfo, AppInfoError } from "./appInfo";
export type Inject = {
injectFrontend: InjectExpress;
injectSim: InjectExpress;
getIndexHtml: () => string;
};
function actualRun(appInfo: AppInfo, options: Options, inject: Inject): void {
const { injectFrontend, injectSim, getIndexHtml } = inject;
const instances = new Instances(appInfo, injectSim, options);
const numberOfInstances = 2;
for (let i = 0; i < numberOfInstances; i++) {
instances.add();
}
const frontend = createFrontend(
appInfo,
instances,
injectFrontend,
getIndexHtml
);
frontend.listen(options.basePort, () => {
console.log("Starting webxdc-dev frontend");
});
instances.start();
open("http://localhost:" + options.basePort);
}
export function run(locationStr: string, options: Options, inject: Inject) {
let location: Location;
try {
location = getLocation(locationStr);
} catch (e) {
if (e instanceof LocationError) {
console.error(e.message);
return;
}
throw e;
}
for (const signal in ["SIGINT", "SIGTERM"]) {
process.on(signal, () => {
location.dispose();
});
}
console.log("Starting webxdc project in:", locationStr);
getAppInfo(location)
.then((appInfo) => {
actualRun(appInfo, options, inject);
})
.catch((e) => {
if (e instanceof AppInfoError) {
console.error(e.message);
return;
}
throw e;
});
}