#!/usr/bin/env node
import * as path$1 from "node:path";
import { fileURLToPath } from "node:url";
//#region src/codeScan/util/structuredOutputDetect.ts
const CODE_SCANS_RUN_VALUE_FLAGS = new Set([
	"--api-key",
	"--base",
	"--compare",
	"-c",
	"--config",
	"--api-host",
	"--github-pr",
	"--min-severity",
	"--minimum-severity",
	"--guidance",
	"--guidance-file",
	"--env-file",
	"--env-path"
]);
/**
* Detect whether `code-scans run` is being invoked with structured output (`--json`,
* `--format json`, `--format sarif`, `-f json`, `-f sarif`, or their `=value` forms).
*
* Mirrors Commander's "last wins" semantics for repeated --format, so we track all
* candidates across the argv and decide at the end rather than returning eagerly.
*/
function requestsStructuredCodeScanOutput(argv) {
	const codeScansIndex = argv.indexOf("code-scans");
	if (codeScansIndex === -1 || argv[codeScansIndex + 1] !== "run") return false;
	let jsonFlag = false;
	let lastFormat;
	for (let index = codeScansIndex + 2; index < argv.length; index++) {
		const arg = argv[index];
		if (arg === "--") break;
		if (arg === "--json") {
			jsonFlag = true;
			continue;
		}
		if (arg === "--format" || arg === "-f") {
			lastFormat = argv[index + 1];
			index++;
			continue;
		}
		if (arg.startsWith("--format=") || arg.startsWith("-f=")) {
			lastFormat = arg.slice(arg.indexOf("=") + 1);
			continue;
		}
		if (arg.startsWith("-f") && arg.length > 2 && arg[2] !== "=") {
			lastFormat = arg.slice(2);
			continue;
		}
		if (CODE_SCANS_RUN_VALUE_FLAGS.has(arg)) index++;
	}
	if (jsonFlag) return true;
	return lastFormat === "json" || lastFormat === "sarif";
}
//#endregion
//#region src/entrypoint.ts
/**
* Entry point for the promptfoo CLI.
*
* This file intentionally has NO dependencies to ensure the Node.js version
* check runs before any module loading that might fail on older versions.
*
* Some dependencies (like string-width via ora) use ES2024 features (e.g., RegExp /v flag)
* that cause cryptic syntax errors on Node.js < 20. By checking the version first,
* we can provide a helpful error message instead.
*/
const nodeEngineRange = "^20.20.0 || >=22.22.0";
const nodeEngineComparatorSets = [[{
	"operator": ">=",
	"version": "20.20.0"
}, {
	"operator": "<",
	"version": "21.0.0-0"
}], [{
	"operator": ">=",
	"version": "22.22.0"
}]];
const deprecatedNodeMajorVersions = new Set([20]);
function parseNodeEngineVersion(version, options = {}) {
	const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+.*)?$/.exec(version);
	if (!match) return null;
	if (!options.allowPrerelease && match[4]) return null;
	return [
		Number.parseInt(match[1], 10),
		Number.parseInt(match[2], 10),
		Number.parseInt(match[3], 10)
	];
}
function compareNodeEngineVersion(left, right) {
	for (let index = 0; index < left.length; index++) {
		if (left[index] > right[index]) return 1;
		if (left[index] < right[index]) return -1;
	}
	return 0;
}
function satisfiesNodeEngineComparator(currentVersion, comparator) {
	const comparatorVersion = parseNodeEngineVersion(comparator.version, { allowPrerelease: true });
	if (!comparatorVersion) return false;
	const comparison = compareNodeEngineVersion(currentVersion, comparatorVersion);
	switch (comparator.operator || "=") {
		case "=": return comparison === 0;
		case ">": return comparison > 0;
		case ">=": return comparison >= 0;
		case "<": return comparison < 0;
		case "<=": return comparison <= 0;
		default: return false;
	}
}
function isSupportedNodeEngineVersion(currentVersion) {
	const parsedCurrentVersion = parseNodeEngineVersion(currentVersion);
	if (!parsedCurrentVersion) return null;
	return nodeEngineComparatorSets.some((comparatorSet) => comparatorSet.length === 0 || comparatorSet.every((comparator) => satisfiesNodeEngineComparator(parsedCurrentVersion, comparator)));
}
function shouldWarnForDeprecatedNodeVersion(currentVersion) {
	const parsedCurrentVersion = parseNodeEngineVersion(currentVersion);
	return parsedCurrentVersion !== null && isSupportedNodeEngineVersion(currentVersion) === true && deprecatedNodeMajorVersions.has(parsedCurrentVersion[0]);
}
function formatUnsupportedNodeVersionMessage(currentVersion) {
	return [
		"\x1B[33mpromptfoo requires a supported Node.js runtime.",
		"",
		`Detected: ${currentVersion}`,
		`Required: ${nodeEngineRange}`,
		"",
		"Install a supported Node.js version and try again.\x1B[0m"
	].join("\n");
}
function formatMalformedNodeVersionMessage(currentVersion) {
	return [
		`\x1b[33mUnable to parse the current Node.js version: ${currentVersion}`,
		`Required: ${nodeEngineRange}`,
		"",
		"Install a supported Node.js version and try again.\x1B[0m"
	].join("\n");
}
function formatDeprecatedNodeVersionMessage(currentVersion) {
	return [
		"\x1B[33mWarning: Node.js 20 has reached end-of-life and is deprecated in promptfoo.",
		"",
		`Detected: ${currentVersion}`,
		"Recommended: Node.js >=22.22.0 (Node.js 24 LTS preferred)",
		"",
		"Upgrade your Node.js runtime to avoid disruption in a future promptfoo release.\x1B[0m"
	].join("\n");
}
const isBun = typeof globalThis.Bun !== "undefined";
const isDeno = typeof globalThis.Deno !== "undefined";
if (!isBun && !isDeno) {
	const isSupportedVersion = isSupportedNodeEngineVersion(process.version);
	if (isSupportedVersion === null) {
		console.error(formatMalformedNodeVersionMessage(process.version));
		process.exit(1);
	}
	if (!isSupportedVersion) {
		console.error(formatUnsupportedNodeVersionMessage(process.version));
		process.exit(1);
	}
	if (shouldWarnForDeprecatedNodeVersion(process.version)) console.warn(formatDeprecatedNodeVersionMessage(process.version));
}
const entryUrl = fileURLToPath(import.meta.url);
if (typeof process.argv[1] === "string" && path$1.resolve(process.argv[1]) === entryUrl && requestsStructuredCodeScanOutput(process.argv.slice(2))) Object.assign(process.env, {
	LOG_LEVEL: "error",
	PROMPTFOO_LOG_TO_STDERR: "true"
});
process.argv[1] = fileURLToPath(new URL("./main.js", import.meta.url));
await import("./main.js");
//#endregion
export {};

//# sourceMappingURL=entrypoint.js.map