From ff7fc7787b4e3df0e1863ec1b8c32bd2af2af335 Mon Sep 17 00:00:00 2001 From: aarov Date: Sat, 28 Sep 2024 22:51:52 +0000 Subject: [PATCH] Update index.js --- index.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 965dcc8..a4f4080 100644 --- a/index.js +++ b/index.js @@ -111,9 +111,9 @@ async function gitMain() { } else { await git.checkout(GIT_BRANCH) } - //console.log(statusResult) const behind = statusResult.behind if (behind > 0 ) { + killProcesses() console.log(`${behind} change behind, pulling...`) await git.pull() } @@ -123,20 +123,40 @@ async function gitMain() { } } -const { execSync, spawn } = require('child_process') +const { execSync, spawn, ChildProcess} = require('child_process') +/** + * @type {Array} + */ + +const processes = [] async function ProjectCmd(cmd) { //console.log(`executing "${cmd}"`) const processPromise = new Promise(function(resolve, reject) { const process = spawn(cmd, { stdio: 'inherit', cwd: PROJECT_DIR, shell:true }); + processes.push(process) process.on('close', (code) => { //console.log(`command "${cmd}" exited with code ${code}`); + processes.splice(processes.indexOf(process),1) resolve(); }); }) return await processPromise; - +} + +async function killProcesses() { + for (let index = 0; index < processes.length; index++) { + const process = processes[index]; + console.log("killing process",process.pid) + await new Promise(function(resolve, reject) { + process.on('close', (code) => { + console.log(`process exited with code ${code}`); + resolve(); + }); + process.kill("SIGINT") + }) + } } async function RunProject(commands = ["npm i","npm start"]) { @@ -166,7 +186,11 @@ async function main() { } console.log("custom start commands",customCommands) - RunProject(customCommands) + + if (processes.length == 0) { + RunProject(customCommands) + } } -main() \ No newline at end of file +main() +setInterval(main, 1000 * 60) // 5 minutes \ No newline at end of file