prod_run.mjs 1.5 KB

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