run-ip-operator-e2e.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { spawn, spawnSync } from 'node:child_process';
  2. const baseUrl = 'http://127.0.0.1:4300';
  3. const isWindows = process.platform === 'win32';
  4. const specFile = process.argv[2] || 'e2e/ip-operator-workbench.spec.ts';
  5. let serverProcess = null;
  6. async function main() {
  7. const alreadyRunning = await isServerReady();
  8. if (!alreadyRunning) {
  9. const { command, args } = commandFor('npm run start -- --host 127.0.0.1 --port 4300', 'npm', [
  10. 'run',
  11. 'start',
  12. '--',
  13. '--host',
  14. '127.0.0.1',
  15. '--port',
  16. '4300',
  17. ]);
  18. serverProcess = spawn(command, args, {
  19. stdio: 'inherit',
  20. shell: false,
  21. });
  22. await waitForServer();
  23. }
  24. const code = await runPlaywright();
  25. cleanupServer();
  26. process.exit(code);
  27. }
  28. async function isServerReady() {
  29. try {
  30. const response = await fetch(baseUrl);
  31. return response.ok;
  32. } catch {
  33. return false;
  34. }
  35. }
  36. async function waitForServer() {
  37. const startedAt = Date.now();
  38. while (Date.now() - startedAt < 120_000) {
  39. if (await isServerReady()) return;
  40. await new Promise((resolve) => setTimeout(resolve, 1000));
  41. }
  42. cleanupServer();
  43. throw new Error(`Angular dev server did not become ready at ${baseUrl}`);
  44. }
  45. function runPlaywright() {
  46. return new Promise((resolve) => {
  47. const { command, args } = commandFor(`npx playwright test ${specFile}`, 'npx', [
  48. 'playwright',
  49. 'test',
  50. specFile,
  51. ]);
  52. const child = spawn(command, args, {
  53. stdio: 'inherit',
  54. shell: false,
  55. });
  56. child.on('close', (code) => resolve(code ?? 1));
  57. });
  58. }
  59. function commandFor(windowsCommand, command, args) {
  60. if (!isWindows) return { command, args };
  61. return { command: 'cmd.exe', args: ['/d', '/s', '/c', windowsCommand] };
  62. }
  63. function cleanupServer() {
  64. if (!serverProcess?.pid) return;
  65. if (isWindows) {
  66. spawnSync('taskkill', ['/pid', String(serverProcess.pid), '/t', '/f'], { stdio: 'ignore' });
  67. } else {
  68. serverProcess.kill('SIGTERM');
  69. }
  70. serverProcess = null;
  71. }
  72. process.on('SIGINT', () => {
  73. cleanupServer();
  74. process.exit(130);
  75. });
  76. process.on('SIGTERM', () => {
  77. cleanupServer();
  78. process.exit(143);
  79. });
  80. main().catch((error) => {
  81. cleanupServer();
  82. console.error(error);
  83. process.exit(1);
  84. });