From 81bd57cf6a08fa073a8f6712affb9c4b9bac557e Mon Sep 17 00:00:00 2001 From: Kacper Date: Thu, 1 Jan 2026 22:39:12 +0100 Subject: [PATCH] feat: add runNpmAndWait function for improved npm command handling - Introduced a new function, runNpmAndWait, to execute npm commands and wait for their completion, enhancing error handling. - Updated the main function to build shared packages before starting the backend server, ensuring necessary dependencies are ready. - Adjusted server and web process commands to use a consistent naming convention. --- init.mjs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/init.mjs b/init.mjs index 9947f574..49d47fa6 100644 --- a/init.mjs +++ b/init.mjs @@ -268,6 +268,20 @@ function runNpm(args, options = {}) { return crossSpawn('npm', args, spawnOptions); } +/** + * Run an npm command and wait for completion + */ +function runNpmAndWait(args, options = {}) { + const child = runNpm(args, options); + return new Promise((resolve, reject) => { + child.on('close', (code) => { + if (code === 0) resolve(); + else reject(new Error(`npm ${args.join(' ')} failed with code ${code}`)); + }); + child.on('error', (err) => reject(err)); + }); +} + /** * Run npx command using cross-spawn for Windows compatibility */ @@ -525,6 +539,10 @@ async function main() { console.log(''); log('Launching Web Application...', 'blue'); + // Build shared packages once (dev:server and dev:web both do this at the root level) + log('Building shared packages...', 'blue'); + await runNpmAndWait(['run', 'build:packages'], { stdio: 'inherit' }); + // Start the backend server log(`Starting backend server on port ${serverPort}...`, 'blue'); @@ -535,7 +553,7 @@ async function main() { // Start server in background, showing output in console AND logging to file const logStream = fs.createWriteStream(path.join(__dirname, 'logs', 'server.log')); - serverProcess = runNpm(['run', 'dev:server'], { + serverProcess = runNpm(['run', '_dev:server'], { stdio: ['ignore', 'pipe', 'pipe'], env: { PORT: String(serverPort), @@ -582,7 +600,7 @@ async function main() { console.log(''); // Start web app - webProcess = runNpm(['run', 'dev:web'], { + webProcess = runNpm(['run', '_dev:web'], { stdio: 'inherit', env: { TEST_PORT: String(webPort),