#!/usr/bin/env node

import { spawnSync } from "node:child_process";
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);

export function resolvePlaywrightCli() {
	return require.resolve("@playwright/cli/playwright-cli.js");
}

export function runPlaywrightCli(args = process.argv.slice(2), options = {}) {
	const bin = resolvePlaywrightCli();
	return spawnSync(process.execPath, [bin, ...args], {
		stdio: "inherit",
		shell: false,
		...options,
	});
}

if (import.meta.url === `file://${process.argv[1]}`) {
	const result = runPlaywrightCli();
	process.exitCode = result.status ?? 1;
}
