()
| 10 | |
| 11 | // Function to load StreamZip from the correct location |
| 12 | function loadStreamZip() { |
| 13 | if (StreamZip) return StreamZip; |
| 14 | |
| 15 | try { |
| 16 | // First try normal require (works when worker is in app directory) |
| 17 | StreamZip = require('node-stream-zip'); |
| 18 | return StreamZip; |
| 19 | } catch (error) { |
| 20 | // If that fails, try to find it from the app's node_modules |
| 21 | const possiblePaths = []; |
| 22 | |
| 23 | // Add the passed node_modules path if available (make it absolute) |
| 24 | if (nodeModulesPath) { |
| 25 | const absoluteNodeModules = path.isAbsolute(nodeModulesPath) |
| 26 | ? nodeModulesPath |
| 27 | : path.resolve(nodeModulesPath); |
| 28 | possiblePaths.push(path.join(absoluteNodeModules, 'node-stream-zip')); |
| 29 | } |
| 30 | |
| 31 | // Add common locations - use process.resourcesPath for built Electron apps |
| 32 | const resourcesPath = process.resourcesPath || path.dirname(__dirname); |
| 33 | possiblePaths.push( |
| 34 | path.resolve(__dirname, '..', 'node_modules', 'node-stream-zip'), |
| 35 | path.resolve(__dirname, '..', '..', 'node_modules', 'node-stream-zip'), |
| 36 | path.resolve(resourcesPath, 'app.asar.unpacked', 'node_modules', 'node-stream-zip'), |
| 37 | path.resolve(resourcesPath, 'app.asar', 'node_modules', 'node-stream-zip'), |
| 38 | path.resolve(resourcesPath, 'app', 'node_modules', 'node-stream-zip'), |
| 39 | // Windows-specific paths |
| 40 | path.resolve(path.dirname(resourcesPath), 'app.asar.unpacked', 'node_modules', 'node-stream-zip'), |
| 41 | path.resolve(path.dirname(resourcesPath), 'Resources', 'app.asar.unpacked', 'node_modules', 'node-stream-zip') |
| 42 | ); |
| 43 | |
| 44 | // Try each path (normalize to absolute paths) |
| 45 | for (let modulePath of possiblePaths) { |
| 46 | // Normalize to absolute path |
| 47 | if (!path.isAbsolute(modulePath)) { |
| 48 | modulePath = path.resolve(modulePath); |
| 49 | } |
| 50 | |
| 51 | if (fs.existsSync(modulePath)) { |
| 52 | try { |
| 53 | // Try requiring the directory (Node will resolve to index.js or main from package.json) |
| 54 | // Use path.resolve to ensure we have an absolute path |
| 55 | const resolvedPath = path.resolve(modulePath); |
| 56 | StreamZip = require(resolvedPath); |
| 57 | console.log(`[Worker] Loaded node-stream-zip from: ${resolvedPath}`); |
| 58 | return StreamZip; |
| 59 | } catch (requireError) { |
| 60 | console.log(`[Worker] Failed to require ${modulePath}:`, requireError.message); |
| 61 | // If directory require fails, try requiring the main file directly |
| 62 | try { |
| 63 | const packageJsonPath = path.join(modulePath, 'package.json'); |
| 64 | if (fs.existsSync(packageJsonPath)) { |
| 65 | const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 66 | const mainFile = packageJson.main || 'index.js'; |
| 67 | const mainPath = path.resolve(modulePath, mainFile); |
| 68 | if (fs.existsSync(mainPath)) { |
| 69 | StreamZip = require(mainPath); |
no test coverage detected