diff --git a/package.json b/package.json index 67a593d..bf335eb 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,7 @@ "test": "echo \"Error: no test specified\" && exit 1", "server": "npm run dev --workspace=server", "client": "npm start --prefix client", - "dev": "concurrently \"npm run server\" \"npm run client\"", - "teardown": "npm run teardown --workspace=server" + "dev": "concurrently \"npm run server\" \"npm run client\"" }, "workspaces": [ "client", diff --git a/server/package.json b/server/package.json index e2284f9..977435b 100644 --- a/server/package.json +++ b/server/package.json @@ -18,7 +18,8 @@ "hash": "ts-node src/scripts/hash.ts", "verify-hash": "ts-node src/scripts/verify-hash.ts", "create-user": "ts-node src/scripts/create-user.ts", - "dev": "npm run setup && npm run node; npm run teardown" + "dev-bg": "npm run setup && npm run node", + "dev": "ts-node src/scripts/dev.ts" }, "dependencies": { "bcrypt": "^6.0.0", diff --git a/server/src/scripts/dev.ts b/server/src/scripts/dev.ts new file mode 100644 index 0000000..913e30e --- /dev/null +++ b/server/src/scripts/dev.ts @@ -0,0 +1,32 @@ +//Dev-Script +//Runs `npm run dev-bg` and listens for interrupts to execute `npm run teardown` +import { spawn } from "node:child_process"; + +const child = spawn("npm", ["run", "dev-bg"], { + stdio: "inherit", + shell: true, +}); + +async function teardown() { + console.log("Running teardown..."); + const teardownProcess = spawn("npm", ["run", "teardown"], { + stdio: "inherit", + shell: true, + }); + + await new Promise((resolve) => teardownProcess.on("exit", resolve)); +} + +async function shutdown(signal: any) { + child.kill(signal); + await teardown(); + process.exit(); +} + +process.on("SIGINT", () => shutdown("SIGINT")); +process.on("SIGTERM", () => shutdown("SIGTERM")); + +child.on("exit", async (code) => { + await teardown(); + process.exit(code ?? 0); +});