prod_run.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(
  31. ([env, val]) =>
  32. `${env}=${val.startsWith('"') && val.endsWith('"') ? val : `"${val}"`}`
  33. )
  34. .join('\n');
  35. fs.writeFileSync('build.env', envFileContent);
  36. execSync(`npx import-meta-env -x build.env -e build.env -p "/site/**/*"`);
  37. fs.rmSync('build.env');
  38. const caddyFileName =
  39. process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true'
  40. ? 'sh-admin-subpath-access.Caddyfile'
  41. : 'sh-admin-multiport-setup.Caddyfile';
  42. const caddyProcess = runChildProcessWithPrefix(
  43. 'caddy',
  44. ['run', '--config', `/etc/caddy/${caddyFileName}`, '--adapter', 'caddyfile'],
  45. 'App/Admin Dashboard Caddy'
  46. );
  47. caddyProcess.on('exit', (code) => {
  48. console.log(`Exiting process because Caddy 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. process.exit(0);
  55. });