import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';

const e2eDirectory = fileURLToPath(new URL('.', import.meta.url));
const env = { ...process.env };
const browsersPath = env.PLAYWRIGHT_BROWSERS_PATH;

if (browsersPath?.includes('/cursor-sandbox-cache/')) {
  delete env.PLAYWRIGHT_BROWSERS_PATH;
}

const [command = 'test', ...args] = process.argv.slice(2);
const executable = process.platform === 'win32' ? 'npx.cmd' : 'npx';
const child = spawn(executable, ['playwright', command, ...args], {
  cwd: e2eDirectory,
  env,
  stdio: 'inherit'
});

child.on('exit', (code, signal) => {
  if (signal) {
    process.kill(process.pid, signal);
    return;
  }

  process.exit(code ?? 1);
});
