prod_run.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/local/bin/node
  2. // @ts-check
  3. import { execSync, spawn } from 'child_process';
  4. import fs from 'fs';
  5. import process from 'process';
  6. function runChildProcessWithPrefix(command, args, prefix) {
  7. const childProcess = spawn(command, args);
  8. childProcess.stdout.on('data', (data) => {
  9. const output = data.toString().trim().split('\n');
  10. output.forEach((line) => {
  11. console.log(`${prefix} | ${line}`);
  12. });
  13. });
  14. childProcess.stderr.on('data', (data) => {
  15. const error = data.toString().trim().split('\n');
  16. error.forEach((line) => {
  17. console.error(`${prefix} | ${line}`);
  18. });
  19. });
  20. childProcess.on('close', (code) => {
  21. console.log(`${prefix} Child process exited with code ${code}`);
  22. });
  23. childProcess.on('error', (stuff) => {
  24. console.log('error');
  25. console.log(stuff);
  26. });
  27. return childProcess;
  28. }
  29. const caddyFileName =
  30. process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true'
  31. ? 'backend-subpath.Caddyfile'
  32. : 'backend-multiport.Caddyfile';
  33. const caddyProcess = runChildProcessWithPrefix(
  34. 'caddy',
  35. ['run', '--config', `/etc/caddy/${caddyFileName}`, '--adapter', 'caddyfile'],
  36. 'App/Admin Dashboard Caddy',
  37. );
  38. const backendProcess = runChildProcessWithPrefix(
  39. 'pnpm',
  40. ['run', 'start:prod'],
  41. 'Backend Server',
  42. );
  43. caddyProcess.on('exit', (code) => {
  44. console.log(`Exiting process because Caddy Server exited with code ${code}`);
  45. process.exit(code);
  46. });
  47. backendProcess.on('exit', (code) => {
  48. console.log(
  49. `Exiting process because Backend Server exited with code ${code}`,
  50. );
  51. process.exit(code);
  52. });
  53. process.on('SIGINT', () => {
  54. console.log('SIGINT received, exiting...');
  55. caddyProcess.kill('SIGINT');
  56. backendProcess.kill('SIGINT');
  57. process.exit(0);
  58. });