aio_run.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 caddyFileName = process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true' ? 'aio-subpath-access.Caddyfile' : 'aio-multiport-setup.Caddyfile'
  41. const caddyProcess = runChildProcessWithPrefix("caddy", ["run", "--config", `/etc/caddy/${caddyFileName}`, "--adapter", "caddyfile"], "App/Admin Dashboard Caddy")
  42. const backendProcess = runChildProcessWithPrefix("node", ["/dist/backend/dist/main.js"], "Backend Server")
  43. const webappProcess = runChildProcessWithPrefix("webapp-server", [], "Webapp Server")
  44. caddyProcess.on("exit", (code) => {
  45. console.log(`Exiting process because Caddy Server exited with code ${code}`)
  46. process.exit(code)
  47. })
  48. backendProcess.on("exit", (code) => {
  49. console.log(`Exiting process because Backend Server exited with code ${code}`)
  50. process.exit(code)
  51. })
  52. webappProcess.on("exit", (code) => {
  53. console.log(`Exiting process because Webapp Server exited with code ${code}`)
  54. process.exit(code)
  55. })
  56. process.on('SIGINT', () => {
  57. console.log("SIGINT received, exiting...")
  58. caddyProcess.kill("SIGINT")
  59. backendProcess.kill("SIGINT")
  60. webappProcess.kill("SIGINT")
  61. process.exit(0)
  62. })