From 46b89e08c02958063e6dcdcc1a2947720fb98838 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Fri, 20 Sep 2024 02:23:38 +0700 Subject: [PATCH] fix: process spawning in cli/utils (#1237) * fix process spawning in cli/utils * fix test-ledger path --- cli/src/utils/process.ts | 41 ++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/cli/src/utils/process.ts b/cli/src/utils/process.ts index d0d73faf02..0a78fa4a43 100644 --- a/cli/src/utils/process.ts +++ b/cli/src/utils/process.ts @@ -128,22 +128,35 @@ export function spawnBinary(command: string, args: string[] = []) { const logDir = "test-ledger"; const binaryName = path.basename(command); - const dir = path.join(__dirname, logDir); - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); - } + const dir = path.join(__dirname, "../..", logDir); + try { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } - const out = fs.openSync(`${logDir}/${binaryName}.log`, "a"); - const err = fs.openSync(`${logDir}/${binaryName}.log`, "a"); - const spawnedProcess = spawn(command, args, { - stdio: ["ignore", out, err], - shell: false, - detached: true, - }); + const logPath = path.join(dir, `${binaryName}.log`); + const out = fs.openSync(logPath, "a"); + const err = fs.openSync(logPath, "a"); - spawnedProcess.on("close", (code) => { - console.log(`${binaryName} process exited with code ${code}`); - }); + const spawnedProcess = spawn(command, args, { + stdio: ["ignore", out, err], + shell: false, + detached: true, + }); + + spawnedProcess.on("close", (code) => { + console.log(`${binaryName} process exited with code ${code}`); + }); + + return spawnedProcess; + } catch (error: unknown) { + if (error instanceof Error) { + console.error(`Error spawning binary: ${error.message}`); + } else { + console.error(`An unknown error occurred while spawning binary`); + } + throw error; + } } export async function waitForServers(