prod_run.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/local/bin/node
  2. import { execSync, spawn } from "child_process"
  3. import fs from "fs"
  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.log("error")
  24. console.log(stuff)
  25. })
  26. return childProcess
  27. }
  28. const envFileContent = Object.entries(process.env)
  29. .filter(([env]) => env.startsWith("VITE_"))
  30. .map(([env, val]) => `${env}=${
  31. (val.startsWith("\"") && val.endsWith("\""))
  32. ? val
  33. : `"${val}"`
  34. }`)
  35. .join("\n")
  36. fs.writeFileSync("build.env", envFileContent)
  37. execSync(`npx import-meta-env -x build.env -e build.env -p "/site/**/*"`)
  38. fs.rmSync("build.env")
  39. const caddyFileName = process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true' ? 'sh-admin-subpath-access.Caddyfile' : 'sh-admin-multiport-setup.Caddyfile'
  40. const caddyProcess = runChildProcessWithPrefix("caddy", ["run", "--config", `/etc/caddy/${caddyFileName}`, "--adapter", "caddyfile"], "App/Admin Dashboard Caddy")
  41. caddyProcess.on("exit", (code) => {
  42. console.log(`Exiting process because Caddy Server exited with code ${code}`)
  43. process.exit(code)
  44. })
  45. process.on('SIGINT', () => {
  46. console.log("SIGINT received, exiting...")
  47. caddyProcess.kill("SIGINT")
  48. process.exit(0)
  49. })