diff --git a/electron/main/init.ts b/electron/main/init.ts index b761e0289..11ed6ef38 100644 --- a/electron/main/init.ts +++ b/electron/main/init.ts @@ -153,11 +153,17 @@ export async function startBackend(setPort?: (port: number) => void): Promise => { try { // Find the hybrid_browser_toolkit ts directory in the virtual environment - const sitePackagesPath = path.join(venvPath, 'lib', 'site-packages'); + // Need to determine the Python version to construct the correct path + let sitePackagesPath: string | null = null; + const libPath = path.join(venvPath, 'lib'); + + // Try to find the site-packages directory (it varies by Python version) + if (fs.existsSync(libPath)) { + const libContents = fs.readdirSync(libPath); + const pythonDir = libContents.find(name => name.startsWith('python')); + if (pythonDir) { + sitePackagesPath = path.join(libPath, pythonDir, 'site-packages'); + } + } + + if (!sitePackagesPath || !fs.existsSync(sitePackagesPath)) { + log.warn('[DEPS INSTALL] site-packages directory not found in venv, skipping npm install'); + return true; // Not an error if the venv structure is different + } + const toolkitPath = path.join(sitePackagesPath, 'camel', 'toolkits', 'hybrid_browser_toolkit', 'ts'); if (!fs.existsSync(toolkitPath)) { - log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found, skipping npm install'); + log.warn('[DEPS INSTALL] hybrid_browser_toolkit ts directory not found at ' + toolkitPath + ', skipping npm install'); return true; // Not an error if the toolkit isn't installed } @@ -350,8 +367,9 @@ export async function installDependencies(version: string): Promise { + const npmExists = await new Promise(resolve => { testNpm.on('close', (code) => resolve(code === 0)); + testNpm.on('error', () => resolve(false)); }); if (npmExists) { @@ -365,25 +383,35 @@ export async function installDependencies(version: string): Promise((resolve, reject) => { - npmInstall.stdout.on('data', (data) => { - log.info(`[DEPS INSTALL] npm install: ${data}`); - safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); - }); + if (npmInstall.stdout) { + npmInstall.stdout.on('data', (data) => { + log.info(`[DEPS INSTALL] npm install: ${data}`); + safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); + }); + } - npmInstall.stderr.on('data', (data) => { - log.warn(`[DEPS INSTALL] npm install stderr: ${data}`); - safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() }); - }); + if (npmInstall.stderr) { + npmInstall.stderr.on('data', (data) => { + log.warn(`[DEPS INSTALL] npm install stderr: ${data}`); + safeMainWindowSend('install-dependencies-log', { type: 'stderr', data: data.toString() }); + }); + } npmInstall.on('close', (code) => { if (code === 0) { @@ -394,6 +422,11 @@ export async function installDependencies(version: string): Promise { + log.error(`[DEPS INSTALL] npm install process error: ${err}`); + reject(err); + }); }); // Run npm build (use the same npm command as install) @@ -409,21 +442,26 @@ export async function installDependencies(version: string): Promise((resolve, reject) => { - npmBuild.stdout.on('data', (data) => { - log.info(`[DEPS INSTALL] npm build: ${data}`); - safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); - }); + if (npmBuild.stdout) { + npmBuild.stdout.on('data', (data) => { + log.info(`[DEPS INSTALL] npm build: ${data}`); + safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); + }); + } - npmBuild.stderr.on('data', (data) => { - // TypeScript build warnings are common, don't treat as errors - log.info(`[DEPS INSTALL] npm build output: ${data}`); - safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); - }); + if (npmBuild.stderr) { + npmBuild.stderr.on('data', (data) => { + // TypeScript build warnings are common, don't treat as errors + log.info(`[DEPS INSTALL] npm build output: ${data}`); + safeMainWindowSend('install-dependencies-log', { type: 'stdout', data: data.toString() }); + }); + } npmBuild.on('close', (code) => { if (code === 0) { @@ -434,6 +472,11 @@ export async function installDependencies(version: string): Promise { + log.error(`[DEPS INSTALL] npm build process error: ${err}`); + reject(err); + }); }); // Optionally install Playwright browsers @@ -461,6 +504,11 @@ export async function installDependencies(version: string): Promise { + log.warn('[DEPS INSTALL] Playwright installation process error:', err); + resolve(); // Non-critical, continue + }); }); } catch (error) { log.warn('[DEPS INSTALL] Failed to install Playwright browsers:', error);