-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-background.mjs
More file actions
96 lines (83 loc) · 2.44 KB
/
Copy pathstart-background.mjs
File metadata and controls
96 lines (83 loc) · 2.44 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
// Customer launcher: start the board with no terminal left open.
// Windows keeps the tray note as the main surface (use Open The Board.bat
// when you want this page). Mac and Linux have no tray — this opens the board.
import { exec, spawn } from "node:child_process";
import fs from "node:fs";
import net from "node:net";
import path from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = path.dirname(fileURLToPath(import.meta.url));
const PORT = Number(process.env.RIAH_TODO_PORT) || 8779;
const HOST = "127.0.0.1";
const URL = `http://${HOST}:${PORT}/`;
const SCRATCH = path.join(ROOT, "scratch");
const PID_FILE = path.join(SCRATCH, "board.pid");
const LOG_FILE = path.join(SCRATCH, "board.log");
function isUp() {
return new Promise((resolve) => {
const s = net.connect({ host: HOST, port: PORT }, () => {
s.end();
resolve(true);
});
s.on("error", () => resolve(false));
});
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
function shouldOpenBrowser() {
if (process.env.RIAH_TODO_NO_BROWSER === "1") return false;
if (process.env.RIAH_TODO_OPEN_BROWSER === "1") return true;
return process.platform !== "win32";
}
function openBrowser(url) {
if (!shouldOpenBrowser()) return;
const plat = process.platform;
if (plat === "win32") exec(`start "" "${url}"`);
else if (plat === "darwin") exec(`open "${url}"`);
else exec(`xdg-open "${url}"`);
}
async function waitUntilUp(ms) {
const end = Date.now() + ms;
while (Date.now() < end) {
if (await isUp()) return true;
await sleep(200);
}
return false;
}
async function main() {
if (await isUp()) {
openBrowser(URL);
return;
}
fs.mkdirSync(SCRATCH, { recursive: true });
const out = fs.openSync(LOG_FILE, "a");
fs.writeSync(out, `\n--- start ${new Date().toISOString()} ---\n`);
const child = spawn(process.execPath, [path.join(ROOT, "server.mjs")], {
cwd: ROOT,
detached: true,
stdio: ["ignore", out, out],
windowsHide: true,
env: {
...process.env,
RIAH_TODO_PORT: String(PORT),
},
});
fs.writeFileSync(PID_FILE, String(child.pid), "utf8");
child.unref();
const ready = await waitUntilUp(15 * 1000);
if (!ready) {
process.exitCode = 1;
return;
}
openBrowser(URL);
}
main().catch((e) => {
try {
fs.appendFileSync(LOG_FILE, String(e && e.stack ? e.stack : e) + "\n");
} catch {
/* ignore */
}
process.exit(1);
});