Skip to content

Commit

Permalink
Cleanup and commenting for JS run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
00Fjongl committed Aug 7, 2024
1 parent 3fa63b8 commit 6412190
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
14 changes: 0 additions & 14 deletions read-config.mjs

This file was deleted.

26 changes: 16 additions & 10 deletions run-command.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { exec, fork } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { build } from 'esbuild';

// Necessary constants are copied over from /src/server.mjs.

const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url))));

const serverUrl = (base => {
Expand All @@ -18,28 +20,37 @@ const serverUrl = (base => {

const shutdown = fileURLToPath(new URL("./src/.shutdown", import.meta.url));

// Run each command line argument passed after node run-command.mjs.
// Commands are defined in the switch case statement below.
for(let i = 2; i < process.argv.length; i++)
switch (process.argv[i]) {
// Commmand to boot up the server. Use PM2 to run if production is true in the
// config file.
case "start":
if (config.production)
exec("npm run pm2-start", (error, stdout) => {
if (error) throw error;
console.log(stdout);
});
// Handle setup on Windows differently from platforms with POSIX-compliant shells.
// This should run the server as a background process.
else if (process.platform === "win32")
exec('START /MIN "" "node backend.js"', (error, stdout) => {
if (error) throw error;
console.log(stdout);
});
else {
const server = fork(fileURLToPath(new URL("./backend.js", import.meta.url)),
const server = fork(
fileURLToPath(new URL("./backend.js", import.meta.url)),
{detached: true}
);
server.unref();
server.disconnect();
}
break;

// Stop the server. Make a temporary file that the server will check for if told
// to shut down. This is done by sending a GET request to the server.
case "stop":
await writeFile(shutdown, "");
try {
Expand Down Expand Up @@ -80,17 +91,12 @@ for(let i = 2; i < process.argv.length; i++)
break;
}

// Forcibly kill all node processes and fully reset PM2. To be used for debugging.
case "kill":
exec("npm run pm2-nuke", (error, stdout) => {console.log(stdout)});
if (process.platform === "win32")
exec("taskkill /F /IM node*", (error, stdout) => {
console.log(stdout);
});
else exec("pkill node", (error, stdout) => {
console.log(stdout);
});
exec("npm run pm2-nuke", (error, stdout) => {
console.log(stdout);
});
exec("taskkill /F /IM node*", (error, stdout) => {console.log(stdout)});
else exec("pkill node", (error, stdout) => {console.log(stdout)});
break;

// No default case.
Expand Down

0 comments on commit 6412190

Please sign in to comment.