"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var script_exports = {};
__export(script_exports, {
  execScript: () => execScript
});
module.exports = __toCommonJS(script_exports);
var import_node_process = __toESM(require("node:process"));
var import_agents = require("../../constants/agents");
var import_node = require("../../constants/node");
var import_find_up = require("../../fs/find-up");
var import_inspect = require("../../objects/inspect");
var import_array = require("../../primordials/array");
var import_spawn = require("../../spawn/spawn");
var import_exec = require("./npm/exec");
var import_exec2 = require("./pnpm/exec");
var import_exec3 = require("./yarnpkg/yarn/exec");
function execScript(scriptName, args, options) {
  let resolvedOptions;
  let resolvedArgs;
  if (!(0, import_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 (0, import_spawn.spawn)(scriptName, resolvedArgs, spawnOptions);
  }
  const useNodeRun = !prepost && (0, import_node.supportsNodeRun)();
  const cwd = (0, import_inspect.getOwn)(spawnOptions, "cwd") ?? import_node_process.default.cwd();
  const pnpmLockPath = (0, import_find_up.findUpSync)(import_agents.PNPM_LOCK_YAML, { cwd });
  if (pnpmLockPath) {
    return (0, import_exec2.execPnpm)(["run", scriptName, ...resolvedArgs], spawnOptions);
  }
  const packageLockPath = (0, import_find_up.findUpSync)(import_agents.PACKAGE_LOCK_JSON, { cwd });
  if (packageLockPath) {
    return (0, import_exec.execNpm)(["run", scriptName, ...resolvedArgs], spawnOptions);
  }
  const yarnLockPath = (0, import_find_up.findUpSync)(import_agents.YARN_LOCK, { cwd });
  if (yarnLockPath) {
    return (0, import_exec3.execYarn)(["run", scriptName, ...resolvedArgs], spawnOptions);
  }
  return (0, import_spawn.spawn)(
    (0, import_node.getExecPath)(),
    [
      ...(0, import_node.getNodeNoWarningsFlags)(),
      ...useNodeRun ? ["--run"] : [import_agents.NPM_REAL_EXEC_PATH, "run"],
      scriptName,
      ...resolvedArgs
    ],
    {
      ...spawnOptions
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  execScript
});
