"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_constants_platform = require('../constants/platform.js');
const require_primordials_array = require('../primordials/array.js');
const require_node_fs = require('../node/fs.js');
const require_node_path = require('../node/path.js');
const require_bin_which = require('./which.js');
const require_env_home = require('../env/home.js');
const require_env_windows = require('../env/windows.js');
const require_env_xdg = require('../env/xdg.js');
const require_bin_shadow = require('./shadow.js');
let src_external_which = require("../external/which");
src_external_which = require_runtime.__toESM(src_external_which);
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/bin/find.ts
/**
* @file Find specific package-manager binaries with platform- aware fallbacks.
*   `findRealBin` tries common platform paths in order, then falls back to a
*   `which` PATH search filtered to skip shadow-bin directories (any path
*   containing `node_modules/.bin`). The shadow-bin filter matters because
*   tools like ts-node and tsx install fake `npm` / `pnpm` / `yarn` shims into
*   the project's `.bin` to intercept the call — those aren't the user's real
*   package manager. Per-tool helpers (`findRealNpm`, `findRealPnpm`,
*   `findRealYarn`) encode the canonical install layouts:
*
*   - npm: alongside `process.execPath` first (Node bundles it), then
*     `%APPDATA%\npm` on Windows or `/usr/local/bin` / `/usr/bin` on POSIX.
*   - pnpm: `~/.local/share/pnpm` (XDG) or `~/.pnpm` on POSIX,
*     `%APPDATA%\npm\pnpm` or `%LOCALAPPDATA%\pnpm\pnpm` on Windows.
*   - yarn: similar to npm but also `~/.yarn/bin/yarn` on POSIX. Each helper
*     returns an empty string when no candidate exists so the caller can fall
*     through to `which` on PATH.
*/
/**
* Find the real executable for a binary, bypassing shadow bins.
*
* @example
*   ;```typescript
*   const npmPath = findRealBin('npm', ['/usr/local/bin/npm'])
*   const gitPath = findRealBin('git')
*   ```
*/
function findRealBin(binName, commonPaths = []) {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	for (const binPath of commonPaths) if (fs.existsSync(binPath)) return binPath;
	/* c8 ignore start */
	const allPaths = src_external_which.default.sync(binName, {
		all: true,
		nothrow: true
	}) || [];
	const pathsArray = require_primordials_array.ArrayIsArray(allPaths) ? allPaths : typeof allPaths === "string" ? [allPaths] : [];
	if (pathsArray.length === 0) return;
	/* c8 ignore stop */
	for (const binPath of pathsArray) if (!require_bin_shadow.isShadowBinPath(path.dirname(binPath))) return binPath;
	return pathsArray[0];
}
/**
* Find the real npm executable, bypassing any aliases and shadow bins.
*
* @example
*   ;```typescript
*   const npmPath = findRealNpm()
*   // e.g. '/usr/local/bin/npm'
*   ```
*/
function findRealNpm() {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const nodeDir = path.dirname(node_process.default.execPath);
	/* c8 ignore start */
	const nodeDirCandidates = require_constants_platform.WIN32 ? [path.join(nodeDir, "npm.cmd"), path.join(nodeDir, "npm")] : [path.join(nodeDir, "npm")];
	/* c8 ignore stop */
	/* c8 ignore start */
	for (const candidate of nodeDirCandidates) if (fs.existsSync(candidate)) return candidate;
	const appdata = /* @__PURE__ */ require_env_windows.getAppdata();
	const result = findRealBin("npm", require_constants_platform.WIN32 ? [
		appdata ? path.join(appdata, "npm", "npm.cmd") : "",
		appdata ? path.join(appdata, "npm", "npm") : "",
		"C:\\Program Files\\nodejs\\npm.cmd",
		"C:\\Program Files\\nodejs\\npm"
	].filter(Boolean) : ["/usr/local/bin/npm", "/usr/bin/npm"]);
	if (result && fs.existsSync(result)) return result;
	/* c8 ignore stop */
	/* c8 ignore start - Fallback paths only fire when npm isn't found
	at any of the common platform locations above. Test runners
	always have npm somewhere standard. */
	const npmPath = require_bin_which.whichRealSync("npm", { nothrow: true });
	if (npmPath && typeof npmPath === "string" && fs.existsSync(npmPath)) return npmPath;
	return "npm";
	/* c8 ignore stop */
}
/**
* Find the real pnpm executable, bypassing any aliases and shadow bins.
*
* @example
*   ;```typescript
*   const pnpmPath = findRealPnpm()
*   // e.g. '/usr/local/bin/pnpm'
*   ```
*/
function findRealPnpm() {
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const home = /* @__PURE__ */ require_env_home.getHome();
	const appdata = /* @__PURE__ */ require_env_windows.getAppdata();
	const localappdata = /* @__PURE__ */ require_env_windows.getLocalappdata();
	const xdgDataHome = /* @__PURE__ */ require_env_xdg.getXdgDataHome();
	/* c8 ignore stop */
	return findRealBin("pnpm", require_constants_platform.WIN32 ? [
		appdata ? path.join(appdata, "npm", "pnpm.cmd") : "",
		appdata ? path.join(appdata, "npm", "pnpm") : "",
		localappdata ? path.join(localappdata, "pnpm", "pnpm.cmd") : "",
		localappdata ? path.join(localappdata, "pnpm", "pnpm") : "",
		"C:\\Program Files\\nodejs\\pnpm.cmd",
		"C:\\Program Files\\nodejs\\pnpm"
	].filter(Boolean) : [
		"/usr/local/bin/pnpm",
		"/usr/bin/pnpm",
		xdgDataHome ? path.join(xdgDataHome, "pnpm/pnpm") : home ? path.join(home, ".local/share/pnpm/pnpm") : "",
		home ? path.join(home, ".pnpm/pnpm") : ""
	].filter(Boolean)) ?? "";
}
/**
* Find the real yarn executable, bypassing any aliases and shadow bins.
*
* @example
*   ;```typescript
*   const yarnPath = findRealYarn()
*   // e.g. '/usr/local/bin/yarn'
*   ```
*/
function findRealYarn() {
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const home = /* @__PURE__ */ require_env_home.getHome();
	const appdata = /* @__PURE__ */ require_env_windows.getAppdata();
	/* c8 ignore stop */
	return findRealBin("yarn", require_constants_platform.WIN32 ? [
		appdata ? path.join(appdata, "npm", "yarn.cmd") : "",
		appdata ? path.join(appdata, "npm", "yarn") : "",
		home ? path.join(home, ".yarn/bin/yarn.cmd") : "",
		home ? path.join(home, ".yarn/bin/yarn") : "",
		"C:\\Program Files\\nodejs\\yarn.cmd",
		"C:\\Program Files\\nodejs\\yarn"
	].filter(Boolean) : [
		"/usr/local/bin/yarn",
		"/usr/bin/yarn",
		home ? path.join(home, ".yarn/bin/yarn") : "",
		home ? path.join(home, ".config/yarn/global/node_modules/.bin/yarn") : ""
	].filter(Boolean)) ?? "";
}

//#endregion
exports.findRealBin = findRealBin;
exports.findRealNpm = findRealNpm;
exports.findRealPnpm = findRealPnpm;
exports.findRealYarn = findRealYarn;