run-web-snapshots.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /** Run serial browser owners before one bounded snapshot pool. */
  2. import { spawn } from 'node:child_process'
  3. import { pnpmInvocation } from './pnpm-invocation.ts'
  4. const serialFiles = [
  5. 'apps/web/tests/hmr-live.e2e.ts',
  6. ]
  7. const workerRaw = process.env.DSH_WEB_SNAPSHOT_WORKERS
  8. const workers = Number.parseInt(workerRaw ?? '', 10)
  9. if (!Number.isSafeInteger(workers) || workers < 2 || String(workers) !== workerRaw) {
  10. throw new Error(`DSH_WEB_SNAPSHOT_WORKERS must be an integer greater than 1, got ${JSON.stringify(workerRaw)}.`)
  11. }
  12. const invocation = pnpmInvocation(['exec', 'vitest', 'run', '--config', 'vitest.web.config.ts'])
  13. let serialStatus = 0
  14. for (const file of serialFiles) {
  15. serialStatus = await run(invocation.command, [...invocation.args, file])
  16. if (serialStatus !== 0) break
  17. }
  18. if (serialStatus === 0) {
  19. process.exitCode = await run(invocation.command, [
  20. ...invocation.args,
  21. ...serialFiles.map(file => `--exclude=${file}`),
  22. '--fileParallelism',
  23. `--maxWorkers=${String(workers)}`,
  24. ])
  25. } else {
  26. process.exitCode = serialStatus
  27. }
  28. function run(command: string, args: string[]): Promise<number> {
  29. return new Promise((resolveRun, reject) => {
  30. const child = spawn(command, args, { stdio: 'inherit' })
  31. child.once('error', reject)
  32. child.once('exit', (exitCode, signalCode) => {
  33. if (signalCode !== null) {
  34. console.error(`web snapshots terminated by ${signalCode}`)
  35. resolveRun(1)
  36. return
  37. }
  38. resolveRun(exitCode ?? 1)
  39. })
  40. })
  41. }