aio_run.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 envFileContent = Object.entries(process.env)
  30. .filter(([env]) => env.startsWith("VITE_"))
  31. .map(([env, val]) => `${env}=${
  32. (val.startsWith("\"") && val.endsWith("\""))
  33. ? val
  34. : `"${val}"`
  35. }`)
  36. .join("\n")
  37. fs.writeFileSync("build.env", envFileContent)
  38. execSync(`npx import-meta-env -x build.env -e build.env -p "/site/**/*"`)
  39. fs.rmSync("build.env")
  40. const caddyProcess = runChildProcessWithPrefix("caddy", ["run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"], "App/Admin Dashboard Caddy")
  41. const backendProcess = runChildProcessWithPrefix("pnpm", ["run", "start:prod"], "Backend Server")
  42. caddyProcess.on("exit", (code) => {
  43. console.log(`Exiting process because Caddy Server exited with code ${code}`)
  44. process.exit(code)
  45. })
  46. backendProcess.on("exit", (code) => {
  47. console.log(`Exiting process because Backend Server exited with code ${code}`)
  48. process.exit(code)
  49. })
  50. process.on('SIGINT', () => {
  51. console.log("SIGINT received, exiting...")
  52. caddyProcess.kill("SIGINT")
  53. backendProcess.kill("SIGINT")
  54. process.exit(0)
  55. })