"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_primordials_array = require('../../primordials/array.js');
const require_objects_inspect = require('../../objects/inspect.js');
const require_process_spawn_child = require('../../process/spawn/child.js');
const require_constants_agents = require('../../constants/agents.js');
const require_constants_node = require('../../constants/node.js');
const require_fs_find_up = require('../../fs/find-up.js');
const require_eco_npm_npm_exec = require('./npm/exec.js');
const require_eco_npm_pnpm_exec = require('./pnpm/exec.js');
const require_eco_npm_yarnpkg_yarn_exec = require('./yarnpkg/yarn/exec.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/eco/npm/script.ts
/**
* @file Cross-tool script runner — picks the right package manager by detecting
*   the nearest lockfile and dispatches to its exec function. Falls back to
*   running `node --run` directly when no lockfile is found. Lockfile
*   precedence (first match wins, walking up from cwd):
*
*   1. pnpm-lock.yaml → execPnpm(['run', scriptName, ...args])
*   2. package-lock.json → execNpm(['run', scriptName, ...args])
*   3. yarn.lock → execYarn(['run', scriptName, ...args])
*   4. (no lockfile) → node --run scriptName (or `node <npm-cli> run` on older
*      Node where `node --run` isn't available) Honors `shell: true` by passing
*      through to spawn() unchanged.
*/
/**
* Execute a package.json script using the detected package manager.
*
* @param scriptName - The package.json script to run.
* @param args - Either the script arguments or an options object.
* @param options - Spawn options plus `prepost` to force npm-style pre/post
*   scripts.
*
* @returns The spawned `ChildProcess`-like promise from the underlying runner.
*/
function execScript(scriptName, args, options) {
	let resolvedOptions;
	let resolvedArgs;
	if (!require_primordials_array.ArrayIsArray(args) && args !== null && typeof args === "object") {
		resolvedOptions = args;
		resolvedArgs = [];
	} else {
		resolvedOptions = options;
		resolvedArgs = args || [];
	}
	const { prepost, ...spawnOptions } = {
		__proto__: null,
		...resolvedOptions
	};
	if (spawnOptions.shell === true) return require_process_spawn_child.spawn(scriptName, resolvedArgs, spawnOptions);
	const useNodeRun = !prepost && require_constants_node.supportsNodeRun();
	const cwd = /* @__PURE__ */ require_objects_inspect.getOwn(spawnOptions, "cwd") ?? node_process.default.cwd();
	if (/* @__PURE__ */ require_fs_find_up.findUpSync("pnpm-lock.yaml", { cwd })) return require_eco_npm_pnpm_exec.execPnpm([
		"run",
		scriptName,
		...resolvedArgs
	], spawnOptions);
	if (/* @__PURE__ */ require_fs_find_up.findUpSync("package-lock.json", { cwd })) return require_eco_npm_npm_exec.execNpm([
		"run",
		scriptName,
		...resolvedArgs
	], spawnOptions);
	if (/* @__PURE__ */ require_fs_find_up.findUpSync("yarn.lock", { cwd })) return require_eco_npm_yarnpkg_yarn_exec.execYarn([
		"run",
		scriptName,
		...resolvedArgs
	], spawnOptions);
	/* c8 ignore stop */
	/* c8 ignore start */
	return require_process_spawn_child.spawn(require_constants_node.getExecPath(), [
		...require_constants_node.getNodeNoWarningsFlags(),
		...useNodeRun ? ["--run"] : [require_constants_agents.NPM_REAL_EXEC_PATH, "run"],
		scriptName,
		...resolvedArgs
	], { ...spawnOptions });
	/* c8 ignore stop */
}

//#endregion
exports.execScript = execScript;