|
1 | 1 | const fs = require('fs'); |
2 | 2 | const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Resolve the real bun binary path. On Windows, PM2 resolves 'bun' to |
| 7 | + * bun.cmd (a batch-file shim) which it cannot execute. This helper |
| 8 | + * locates the actual bun.exe so PM2 can run it directly. |
| 9 | + */ |
| 10 | +function resolveBun() { |
| 11 | + if (process.platform !== 'win32') return 'bun'; |
| 12 | + try { |
| 13 | + const result = execSync('where.exe bun.exe', { encoding: 'utf8', timeout: 5000 }); |
| 14 | + const paths = result.trim().split(/\r?\n/); |
| 15 | + const realBun = paths.find(p => !p.toLowerCase().endsWith('.cmd')); |
| 16 | + if (realBun) return realBun.trim(); |
| 17 | + } catch (_) { |
| 18 | + // where.exe failed — try known fallback locations. |
| 19 | + } |
| 20 | + const fallback = path.join(process.env.APPDATA || '', 'npm', 'node_modules', 'bun', 'bin', 'bun.exe'); |
| 21 | + try { |
| 22 | + fs.accessSync(fallback); |
| 23 | + return fallback; |
| 24 | + } catch (_) { |
| 25 | + // Fallback path does not exist. |
| 26 | + } |
| 27 | + return 'bun'; |
| 28 | +} |
| 29 | + |
| 30 | +const BUN = resolveBun(); |
3 | 31 |
|
4 | 32 | function isLinuxMusl() { |
5 | 33 | if (process.platform !== 'linux') { |
@@ -309,7 +337,7 @@ module.exports = { |
309 | 337 | { |
310 | 338 | name: `shipsec-backend-${instanceNum}`, |
311 | 339 | cwd: __dirname + '/backend', |
312 | | - script: 'bun', |
| 340 | + script: BUN, |
313 | 341 | args: isProduction ? 'src/main.ts' : 'run dev', |
314 | 342 | interpreter: 'none', |
315 | 343 | env_file: resolveEnvFile('backend', instanceNum), |
@@ -349,8 +377,9 @@ module.exports = { |
349 | 377 | { |
350 | 378 | name: `shipsec-frontend-${instanceNum}`, |
351 | 379 | cwd: __dirname + '/frontend', |
352 | | - script: 'bun', |
| 380 | + script: BUN, |
353 | 381 | args: 'run dev', |
| 382 | + interpreter: 'none', |
354 | 383 | env_file: resolveEnvFile('frontend', instanceNum), |
355 | 384 | env: { |
356 | 385 | ...loadFrontendEnv(resolveEnvFile('frontend', instanceNum)), |
|
0 commit comments