const require_rolldown_runtime = require('../../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_pidTree = require('./pidTree.cjs');
let node_child_process = require("node:child_process");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/utils/runParallel/spawnPosix.mjs
/**
* Kills the new process and its sub processes.
*/
const createKillHandler = (child) => {
	return (signal) => {
		if (!child.pid) return false;
		try {
			require_pidTree.list(child.pid, { root: true }, (err, pids) => {
				if (err) {
					try {
						process.kill(-child.pid, signal ?? "SIGTERM");
					} catch {}
					return;
				}
				if (!pids) {
					try {
						process.kill(-child.pid, signal ?? "SIGTERM");
					} catch {}
					return;
				}
				for (const pid of pids) try {
					process.kill(pid, signal ?? "SIGTERM");
				} catch (_err) {}
			});
		} catch {
			try {
				process.kill(-child.pid, signal ?? "SIGTERM");
			} catch {}
		}
		return true;
	};
};
/**
* Launches a new process with the given command.
* This is almost same as `child_process.spawn`.
*
* This returns a `ChildProcess` instance.
* `kill` method of the instance kills the new process and its sub processes.
*
* @param command - The command to run.
* @param args - List of string arguments.
* @param options - Options.
* @returns A ChildProcess instance of new process.
* @private
*/
const spawnPosix = (command, args, options) => {
	const child = (0, node_child_process.spawn)(command, args, options);
	child.kill = createKillHandler(child);
	return child;
};

//#endregion
exports.spawnPosix = spawnPosix;
//# sourceMappingURL=spawnPosix.cjs.map