import { spawn } from "child_process"; import dotenv from 'dotenv'; dotenv.config() /** * Object to store references to spawned processes. * @type {Object.} */ const runningProcesses = {}; /** * Launches a new process if it's not already running. * @param {string} processName - The name of the process to launch. * @param {string[]} args - The arguments to pass to the process. * @param {boolean} waitForClose - Set this to wait to return until the process exits * @param {boolean} returnOutput - Set this in addition to 'waitForClose' to return the script output when the process closes * @param {boolean} waitForClose - Set the current working directory of the process being launched */ export const launchProcess = (processName, args, waitForClose = false, returnOutput = false, pcwd = undefined) => { if (!runningProcesses[processName]) { let childProcess; if (pcwd) { childProcess = spawn(processName, args, { cwd: pcwd }); } else { childProcess = spawn(processName, args); } // Store reference to the spawned process 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 output = new Promise(res => { childProcess.on('exit', (code, signal) => { // Remove reference to the process when it exits delete runningProcesses[processName]; console.log(`${processName} process exited with code ${code} and signal ${signal}`); console.log("Child process console output: ", scriptOutput); // Return the full script output if requested if (returnOutput === true) { return res(scriptOutput) } return res(code); }) }); if (waitForClose === true) { return output } console.log(`${processName} process started.`); } else { console.log(`${processName} process is already running.`); } } /** * Checks the status of a process. * @param {string} processName - The name of the process to check. * @returns {string} A message indicating whether the process is running or not. */ export const checkProcessStatus = (processName) => { const childProcess = runningProcesses[processName]; if (childProcess) { // Check if the process is running if (!childProcess.killed) { return `${processName} process is running.`; } else { return `${processName} process is not running.`; } } else { return `${processName} process is not running.`; } } /** * Kills a running process. * @param {string} processName - The name of the process to kill. */ export const killProcess = (processName) => { const childProcess = runningProcesses[processName]; if (childProcess) { childProcess.kill(); console.log(`${processName} process killed.`); } else { console.log(`${processName} process is not running.`); } } export const getRunningProcesses = () => runningProcesses;