import { spawn } from "node:child_process";

//#region ../chokidar/dist/esm/utils/runParallel/bin.mjs
const stripStderr = (stderr) => {
	if (!stderr) return;
	let cleaned = stderr.trim();
	cleaned = cleaned.replace(/your \d+x\d+ screen size is bogus\. expect trouble/gi, "");
	return cleaned.trim() || void 0;
};
/**
* Spawn a binary and read its stdout.
* @param cmd The name of the binary to spawn.
* @param args The arguments for the binary.
* @param options Optional option for the spawn function.
* @param done Callback function.
*/
const run = (cmd, args, options, done) => {
	let normalizedOptions;
	let normalizedDone;
	if (typeof options === "function") {
		normalizedDone = options;
		normalizedOptions = void 0;
	} else {
		normalizedDone = done;
		normalizedOptions = options;
	}
	let executed = false;
	const ch = spawn(cmd, args, normalizedOptions ?? {});
	let stdout = "";
	let stderr = "";
	if (ch.stdout) ch.stdout.on("data", (d) => {
		stdout += d.toString();
	});
	if (ch.stderr) ch.stderr.on("data", (d) => {
		stderr += d.toString();
	});
	ch.on("error", (err) => {
		if (executed) return;
		executed = true;
		normalizedDone(new Error(String(err)));
	});
	ch.on("close", (code) => {
		if (executed) return;
		executed = true;
		const cleanedStderr = stripStderr(stderr);
		if (cleanedStderr) return normalizedDone(new Error(cleanedStderr));
		normalizedDone(null, stdout, code ?? void 0);
	});
};

//#endregion
export { run };
//# sourceMappingURL=bin.mjs.map