"use strict";
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var agent_exports = {};
__export(agent_exports, {
  execNpm: () => execNpm,
  execPnpm: () => execPnpm,
  execScript: () => execScript,
  execYarn: () => execYarn,
  isNpmAuditFlag: () => isNpmAuditFlag,
  isNpmFundFlag: () => isNpmFundFlag,
  isNpmLoglevelFlag: () => isNpmLoglevelFlag,
  isNpmNodeOptionsFlag: () => isNpmNodeOptionsFlag,
  isNpmProgressFlag: () => isNpmProgressFlag,
  isPnpmFrozenLockfileFlag: () => isPnpmFrozenLockfileFlag,
  isPnpmIgnoreScriptsFlag: () => isPnpmIgnoreScriptsFlag,
  isPnpmInstallCommand: () => isPnpmInstallCommand,
  isPnpmLoglevelFlag: () => isPnpmLoglevelFlag
});
module.exports = __toCommonJS(agent_exports);
var import_agents = require("./constants/agents");
var import_node = require("./constants/node");
var import_platform = require("./constants/platform");
var import_ci = require("./env/ci");
var import_bin = require("./bin");
var import_debug = require("./debug");
var import_fs = require("./fs");
var import_objects = require("./objects");
var import_spawn = require("./spawn");
const pnpmIgnoreScriptsFlags = /* @__PURE__ */ new Set([
  "--ignore-scripts",
  "--no-ignore-scripts"
]);
const pnpmFrozenLockfileFlags = /* @__PURE__ */ new Set([
  "--frozen-lockfile",
  "--no-frozen-lockfile"
]);
const pnpmInstallCommands = /* @__PURE__ */ new Set(["install", "i"]);
const pnpmInstallLikeCommands = /* @__PURE__ */ new Set([
  "install",
  "i",
  "add",
  "update",
  "up",
  "remove",
  "rm",
  "link",
  "ln",
  "unlink",
  "import",
  "rebuild",
  "rb"
]);
const yarnInstallLikeCommands = /* @__PURE__ */ new Set([
  "install",
  "add",
  "upgrade",
  "remove",
  "link",
  "unlink",
  "import"
]);
function execNpm(args, options) {
  const useDebug = (0, import_debug.isDebug)();
  const terminatorPos = args.indexOf("--");
  const npmArgs = (terminatorPos === -1 ? args : args.slice(0, terminatorPos)).filter(
    (a) => !/* @__PURE__ */ isNpmAuditFlag(a) && !/* @__PURE__ */ isNpmFundFlag(a) && !/* @__PURE__ */ isNpmProgressFlag(a)
  );
  const otherArgs = terminatorPos === -1 ? [] : args.slice(terminatorPos);
  const logLevelArgs = (
    // The default value of loglevel is "notice". We default to "warn" which is
    // one level quieter.
    useDebug || npmArgs.some(isNpmLoglevelFlag) ? [] : ["--loglevel", "warn"]
  );
  const npmBin = import_agents.NPM_BIN_PATH;
  return (0, import_spawn.spawn)(
    npmBin,
    [
      // Even though '--loglevel=error' is passed npm will still run through
      // code paths for 'audit' and 'fund' unless '--no-audit' and '--no-fund'
      // flags are passed.
      "--no-audit",
      "--no-fund",
      // Add `--no-progress` and `--silent` flags to fix input being swallowed
      // by the spinner when running the command with recent versions of npm.
      "--no-progress",
      // Add '--loglevel=error' if a loglevel flag is not provided and the
      // SOCKET_DEBUG environment variable is not truthy.
      ...logLevelArgs,
      ...npmArgs,
      ...otherArgs
    ],
    {
      __proto__: null,
      // On Windows, npm is a .cmd file that requires shell to execute.
      shell: import_platform.WIN32,
      ...options
    }
  );
}
function execPnpm(args, options) {
  const { allowLockfileUpdate, ...extBinOpts } = {
    __proto__: null,
    ...options
  };
  const useDebug = (0, import_debug.isDebug)();
  const terminatorPos = args.indexOf("--");
  const pnpmArgs = (terminatorPos === -1 ? args : args.slice(0, terminatorPos)).filter((a) => !/* @__PURE__ */ isNpmProgressFlag(a));
  const otherArgs = terminatorPos === -1 ? [] : args.slice(terminatorPos);
  const firstArg = pnpmArgs[0];
  const supportsIgnoreScripts = firstArg ? pnpmInstallLikeCommands.has(firstArg) : false;
  const logLevelArgs = useDebug || pnpmArgs.some(isPnpmLoglevelFlag) ? [] : ["--loglevel", "warn"];
  const hasIgnoreScriptsFlag = pnpmArgs.some(isPnpmIgnoreScriptsFlag);
  const ignoreScriptsArgs = !supportsIgnoreScripts || hasIgnoreScriptsFlag ? [] : ["--ignore-scripts"];
  const frozenLockfileArgs = [];
  if ((0, import_ci.getCI)() && allowLockfileUpdate && firstArg && /* @__PURE__ */ isPnpmInstallCommand(firstArg) && !pnpmArgs.some(isPnpmFrozenLockfileFlag)) {
    frozenLockfileArgs.push("--no-frozen-lockfile");
  }
  return (0, import_bin.execBin)(
    "pnpm",
    [
      // Add '--loglevel=warn' if a loglevel flag is not provided and debug is off.
      ...logLevelArgs,
      // Add '--ignore-scripts' by default for security (only for installation commands).
      ...ignoreScriptsArgs,
      // Add '--no-frozen-lockfile' in CI when lockfile updates are needed.
      ...frozenLockfileArgs,
      ...pnpmArgs,
      ...otherArgs
    ],
    extBinOpts
  );
}
function execYarn(args, options) {
  const useDebug = (0, import_debug.isDebug)();
  const terminatorPos = args.indexOf("--");
  const yarnArgs = (terminatorPos === -1 ? args : args.slice(0, terminatorPos)).filter((a) => !/* @__PURE__ */ isNpmProgressFlag(a));
  const otherArgs = terminatorPos === -1 ? [] : args.slice(terminatorPos);
  const firstArg = yarnArgs[0];
  const supportsIgnoreScripts = firstArg ? yarnInstallLikeCommands.has(firstArg) : false;
  const logLevelArgs = useDebug || yarnArgs.some(isNpmLoglevelFlag) ? [] : ["--silent"];
  const hasIgnoreScriptsFlag = yarnArgs.some(isPnpmIgnoreScriptsFlag);
  const ignoreScriptsArgs = !supportsIgnoreScripts || hasIgnoreScriptsFlag ? [] : ["--ignore-scripts"];
  return (0, import_bin.execBin)(
    "yarn",
    [
      // Add '--silent' if a loglevel flag is not provided and debug is off.
      ...logLevelArgs,
      // Add '--ignore-scripts' by default for security (only for installation commands).
      ...ignoreScriptsArgs,
      ...yarnArgs,
      ...otherArgs
    ],
    {
      __proto__: null,
      ...options
    }
  );
}
// @__NO_SIDE_EFFECTS__
function isNpmAuditFlag(cmdArg) {
  return /^--(no-)?audit(=.*)?$/.test(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isNpmFundFlag(cmdArg) {
  return /^--(no-)?fund(=.*)?$/.test(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isNpmLoglevelFlag(cmdArg) {
  if (/^--loglevel(=.*)?$/.test(cmdArg)) {
    return true;
  }
  if (/^--(silent|verbose|info|warn|error|quiet)$/.test(cmdArg)) {
    return true;
  }
  return /^-(s|q|d|dd|ddd|v)$/.test(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isNpmNodeOptionsFlag(cmdArg) {
  return /^--node-options(=.*)?$/.test(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isNpmProgressFlag(cmdArg) {
  return /^--(no-)?progress(=.*)?$/.test(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isPnpmIgnoreScriptsFlag(cmdArg) {
  return pnpmIgnoreScriptsFlags.has(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isPnpmFrozenLockfileFlag(cmdArg) {
  return pnpmFrozenLockfileFlags.has(cmdArg);
}
// @__NO_SIDE_EFFECTS__
function isPnpmInstallCommand(cmdArg) {
  return pnpmInstallCommands.has(cmdArg);
}
const isPnpmLoglevelFlag = isNpmLoglevelFlag;
function execScript(scriptName, args, options) {
  let resolvedOptions;
  let resolvedArgs;
  if (!Array.isArray(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_objects.getOwn)(spawnOptions, "cwd") ?? process.cwd();
  const pnpmLockPath = (0, import_fs.findUpSync)(import_agents.PNPM_LOCK_YAML, { cwd });
  if (pnpmLockPath) {
    return execPnpm(["run", scriptName, ...resolvedArgs], spawnOptions);
  }
  const packageLockPath = (0, import_fs.findUpSync)(import_agents.PACKAGE_LOCK_JSON, { cwd });
  if (packageLockPath) {
    return execNpm(["run", scriptName, ...resolvedArgs], spawnOptions);
  }
  const yarnLockPath = (0, import_fs.findUpSync)(import_agents.YARN_LOCK, { cwd });
  if (yarnLockPath) {
    return 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 = {
  execNpm,
  execPnpm,
  execScript,
  execYarn,
  isNpmAuditFlag,
  isNpmFundFlag,
  isNpmLoglevelFlag,
  isNpmNodeOptionsFlag,
  isNpmProgressFlag,
  isPnpmFrozenLockfileFlag,
  isPnpmIgnoreScriptsFlag,
  isPnpmInstallCommand,
  isPnpmLoglevelFlag
});
