"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 spawn_exports = {};
__export(spawn_exports, {
  spawn: () => spawn,
  spawnSync: () => spawnSync
});
module.exports = __toCommonJS(spawn_exports);
var import_node_process = __toESM(require("node:process"));
var import_which = require("../bin/which");
var import_abort = require("../process/abort");
var import_child_process = require("../node/child-process");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
var import_inspect = require("../objects/inspect");
var import_normalize = require("../paths/normalize");
var import_regexp = require("../primordials/regexp");
var import_registry = require("../spinner/registry");
var import_internal = require("./_internal");
var import_errors = require("./errors");
var import_stdio = require("./stdio");
const abortSignal = (0, import_abort.getAbortSignal)();
const spinner = (0, import_registry.getDefaultSpinner)();
function spawn(cmd, args, options, extra) {
  const {
    spinner: optionsSpinner = spinner,
    stripAnsi: shouldStripAnsi = true,
    ...rawSpawnOptions
  } = { __proto__: null, ...options };
  const spinnerInstance = optionsSpinner;
  const spawnOptions = { __proto__: null, ...rawSpawnOptions };
  const { env, shell, stdio, stdioString = true } = spawnOptions;
  const cwd = spawnOptions.cwd ? String(spawnOptions.cwd) : void 0;
  let actualCmd = cmd;
  if (!(0, import_normalize.isPath)(cmd)) {
    const fs = (0, import_fs.getNodeFs)();
    const cached = import_internal.spawnBinPathCache.get(cmd);
    if (cached) {
      if (fs.existsSync(cached)) {
        actualCmd = cached;
      } else {
        import_internal.spawnBinPathCache.delete(cmd);
      }
    }
    if (actualCmd === cmd) {
      const resolved = (0, import_which.whichSync)(cmd, { cwd, nothrow: true });
      if (resolved && typeof resolved === "string") {
        actualCmd = resolved;
        import_internal.spawnBinPathCache.set(cmd, resolved);
      }
    }
  }
  const WIN32 = import_node_process.default.platform === "win32";
  if (WIN32 && shell && (0, import_regexp.RegExpPrototypeTest)(import_internal.windowsScriptExtRegExp, actualCmd)) {
    if (!(0, import_normalize.isPath)(actualCmd)) {
      actualCmd = (0, import_path.getNodePath)().basename(
        actualCmd,
        (0, import_path.getNodePath)().extname(actualCmd)
      );
    }
  }
  const wasSpinning = !!spinnerInstance?.isSpinning;
  const shouldStopSpinner = wasSpinning && !(0, import_stdio.isStdioType)(stdio, "ignore") && !(0, import_stdio.isStdioType)(stdio, "pipe");
  const shouldRestartSpinner = shouldStopSpinner;
  if (shouldStopSpinner) {
    spinnerInstance.stop();
  }
  const envToUse = env ? {
    __proto__: null,
    ...import_node_process.default.env,
    ...env
  } : import_node_process.default.env;
  const promiseSpawnOpts = {
    __proto__: null,
    cwd: typeof spawnOptions.cwd === "string" ? spawnOptions.cwd : void 0,
    env: envToUse,
    signal: abortSignal,
    stdio: spawnOptions.stdio,
    stdioString,
    shell: spawnOptions.shell,
    windowsVerbatimArguments: spawnOptions.windowsVerbatimArguments,
    timeout: spawnOptions.timeout,
    uid: spawnOptions.uid,
    gid: spawnOptions.gid
  };
  const npmCliPromiseSpawn = (0, import_internal.getNpmCliPromiseSpawn)();
  const spawnPromise = npmCliPromiseSpawn(
    actualCmd,
    args ? [...args] : [],
    promiseSpawnOpts,
    extra
  );
  const oldSpawnPromise = spawnPromise;
  let newSpawnPromise;
  if (shouldStripAnsi && stdioString) {
    newSpawnPromise = (async () => {
      try {
        const result = await spawnPromise;
        const strippedResult = (0, import_internal.stripAnsiFromSpawnResult)(result);
        if ("code" in strippedResult) {
          ;
          strippedResult.exitCode = strippedResult.code;
        }
        return strippedResult;
      } catch (error) {
        const strippedError = (0, import_internal.stripAnsiFromSpawnResult)(error);
        throw (0, import_errors.enhanceSpawnError)(strippedError);
      }
    })();
  } else {
    newSpawnPromise = (async () => {
      try {
        const result = await spawnPromise;
        if (result !== null && typeof result === "object" && "code" in result) {
          const res = result;
          res.exitCode = res.code;
          return res;
        }
        return result;
      } catch (error) {
        throw (0, import_errors.enhanceSpawnError)(error);
      }
    })();
  }
  if (shouldRestartSpinner) {
    const prevPromise = newSpawnPromise;
    newSpawnPromise = (async () => {
      try {
        return await prevPromise;
      } finally {
        spinnerInstance.start();
      }
    })();
  }
  ;
  newSpawnPromise.process = oldSpawnPromise.process;
  newSpawnPromise.stdin = oldSpawnPromise.stdin;
  return newSpawnPromise;
}
function spawnSync(cmd, args, options) {
  let actualCmd = cmd;
  if (!(0, import_normalize.isPath)(cmd)) {
    const resolved = (0, import_which.whichSync)(cmd, {
      cwd: (0, import_inspect.getOwn)(options, "cwd"),
      nothrow: true
    });
    if (resolved && typeof resolved === "string") {
      actualCmd = resolved;
    }
  }
  const shell = (0, import_inspect.getOwn)(options, "shell");
  const WIN32 = import_node_process.default.platform === "win32";
  if (WIN32 && shell && (0, import_regexp.RegExpPrototypeTest)(import_internal.windowsScriptExtRegExp, actualCmd)) {
    if (!(0, import_normalize.isPath)(actualCmd)) {
      actualCmd = (0, import_path.getNodePath)().basename(
        actualCmd,
        (0, import_path.getNodePath)().extname(actualCmd)
      );
    }
  }
  const { stripAnsi: shouldStripAnsi = true, ...rawSpawnOptions } = {
    __proto__: null,
    ...options
  };
  const { stdioString: rawStdioString = true } = rawSpawnOptions;
  const rawEncoding = rawStdioString ? "utf8" : "buffer";
  const spawnOptions = {
    encoding: rawEncoding,
    ...rawSpawnOptions
  };
  const stdioString = spawnOptions.encoding !== "buffer";
  const result = (0, import_child_process.getNodeChildProcess)().spawnSync(actualCmd, args, spawnOptions);
  if (stdioString) {
    const { stderr, stdout } = result;
    if (stdout) {
      result.stdout = stdout.toString().trim();
    }
    if (stderr) {
      result.stderr = stderr.toString().trim();
    }
  }
  return shouldStripAnsi && stdioString ? (0, import_internal.stripAnsiFromSpawnResult)(result) : result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  spawn,
  spawnSync
});
