aio_run.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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("pnpm", ["run", "start:prod"], "Backend Server")
  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(`Exiting process because Backend Server exited with code ${code}`)
  49. process.exit(code)
  50. })
  51. process.on('SIGINT', () => {
  52. console.log("SIGINT received, exiting...")
  53. caddyProcess.kill("SIGINT")
  54. backendProcess.kill("SIGINT")
  55. process.exit(0)
  56. })