Skip to content

Commit 80d4e83

Browse files
author
Jboucly
committed
feat: add a monitoring script to restart the server on changes
1 parent 4a97cfd commit 80d4e83

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"lint:prettier": "prettier . --write",
4444
"prepare": "[ -f node_modules/.bin/husky ] && husky || echo no husky installed.",
4545
"server": "node ./serveronly",
46-
"server:dev": "nodemon",
46+
"server:watch": "node serveronly/watcher.js",
4747
"start": "node --run start:x11",
4848
"start:dev": "node --run start:x11 -- dev",
4949
"start:wayland": "WAYLAND_DISPLAY=\"${WAYLAND_DISPLAY:=wayland-1}\" ./node_modules/.bin/electron js/electron.js --enable-features=UseOzonePlatform --ozone-platform=wayland",

serveronly/watcher.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const { spawn } = require("child_process");
2+
const fs = require("fs");
3+
const path = require("path");
4+
const Log = require("../js/logger");
5+
6+
let child = null;
7+
let restartTimer = null;
8+
9+
/**
10+
* Start the server process
11+
*/
12+
function startServer () {
13+
child = spawn("npm", ["run", "server"], { stdio: "inherit" });
14+
15+
child.on("exit", (code) => {
16+
Log.info(`Changes detected : ${code}`);
17+
Log.info("Restarting server...");
18+
startServer();
19+
});
20+
}
21+
22+
/**
23+
* Watch a directory for changes and restart the server on change
24+
* @param dir
25+
*/
26+
function watchDir (dir) {
27+
fs.watch(dir, { recursive: true }, (_eventType, filename) => {
28+
if (!filename) return;
29+
30+
if (dir.includes("modules") && !filename.endsWith("node_helper.js")) return;
31+
32+
if (restartTimer) clearTimeout(restartTimer);
33+
34+
restartTimer = setTimeout(() => {
35+
Log.info(`Changes detected in ${dir}: ${filename} — restarting...`);
36+
if (child) child.kill("SIGTERM");
37+
}, 500);
38+
});
39+
}
40+
41+
startServer();
42+
watchDir(path.join(__dirname, "..", "config"));
43+
watchDir(path.join(__dirname, "..", "modules"));
44+
45+
process.on("SIGINT", () => {
46+
if (restartTimer) clearTimeout(restartTimer);
47+
if (child) child.kill("SIGTERM");
48+
process.exit(0);
49+
});

0 commit comments

Comments
 (0)