Improve subprocess handler

- Will now output the subprocess console when in dev mode
This commit is contained in:
Logan Cusano
2024-04-28 03:18:13 -04:00
parent 6cae18e70c
commit bf69e93e29

View File

@@ -1,4 +1,6 @@
import { spawn } from "child_process"; import { spawn } from "child_process";
import dotenv from 'dotenv';
dotenv.config()
/** /**
* Object to store references to spawned processes. * Object to store references to spawned processes.
@@ -25,11 +27,29 @@ export const launchProcess = (processName, args, waitForClose=false, pcwd=undefi
// Store reference to the spawned process // Store reference to the spawned process
runningProcesses[processName] = childProcess; runningProcesses[processName] = childProcess;
// Output the process output in development
var scriptOutput = "";
// Get the stdout from the child process
childProcess.stdout.setEncoding('utf8');
childProcess.stdout.on('data', (data) => {
if (process.env.NODE_ENV === "development") console.log(`Data from ${processName}:`, data);
scriptOutput += data.toString();
});
// Get the stderr from the child process
childProcess.stderr.setEncoding('utf8');
childProcess.stderr.on('data', (data) => {
if (process.env.NODE_ENV === "development") console.log(`Data from ${processName}:`, data);
scriptOutput += data.toString();
})
let code = new Promise(res => { let code = new Promise(res => {
childProcess.on('exit', (code, signal) => { childProcess.on('exit', (code, signal) => {
// Remove reference to the process when it exits // Remove reference to the process when it exits
delete runningProcesses[processName]; delete runningProcesses[processName];
console.log(`${processName} process exited with code ${code} and signal ${signal}`); console.log(`${processName} process exited with code ${code} and signal ${signal}`);
console.log("Child process console output: ", scriptOutput);
res(code); res(code);
}) })
}); });