@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
203 lines (202 loc) • 6.9 kB
JavaScript
;
/* 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 spawn_exports = {};
__export(spawn_exports, {
isSpawnError: () => isSpawnError,
isStdioType: () => isStdioType,
spawn: () => spawn,
spawnSync: () => spawnSync
});
module.exports = __toCommonJS(spawn_exports);
var import_process = require("./constants/process");
var import_arrays = require("./arrays");
var import_objects = require("./objects");
var import_strings = require("./strings");
const abortSignal = (0, import_process.getAbortSignal)();
const spinner = (0, import_process.getSpinner)();
const windowsScriptExtRegExp = /\.(?:cmd|bat|ps1)$/i;
let _child_process;
// @__NO_SIDE_EFFECTS__
function getChildProcess() {
if (_child_process === void 0) {
_child_process = require("node:child_process");
}
return _child_process;
}
let _npmCliPromiseSpawn;
// @__NO_SIDE_EFFECTS__
function getNpmcliPromiseSpawn() {
if (_npmCliPromiseSpawn === void 0) {
_npmCliPromiseSpawn = require("./external/@npmcli/promise-spawn");
}
return _npmCliPromiseSpawn;
}
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
if (_path === void 0) {
_path = require("node:path");
}
return _path;
}
// @__NO_SIDE_EFFECTS__
function isSpawnError(value) {
if (value === null || typeof value !== "object") {
return false;
}
const err = value;
return (0, import_objects.hasOwn)(err, "code") && typeof err["code"] !== "undefined" || (0, import_objects.hasOwn)(err, "errno") && typeof err["errno"] !== "undefined" || (0, import_objects.hasOwn)(err, "syscall") && typeof err["syscall"] === "string";
}
// @__NO_SIDE_EFFECTS__
function isStdioType(stdio, type) {
if (arguments.length === 1) {
const validTypes = ["pipe", "ignore", "inherit", "overlapped"];
return typeof stdio === "string" && validTypes.includes(stdio);
}
return stdio === type || (stdio === null || stdio === void 0) && type === "pipe" || (0, import_arrays.isArray)(stdio) && stdio.length > 2 && stdio[0] === type && stdio[1] === type && stdio[2] === type;
}
// @__NO_SIDE_EFFECTS__
function stripAnsiFromSpawnResult(result) {
const res = result;
const { stderr, stdout } = res;
if (typeof stdout === "string") {
res.stdout = (0, import_strings.stripAnsi)(stdout);
}
if (typeof stderr === "string") {
res.stderr = (0, import_strings.stripAnsi)(stderr);
}
return res;
}
function spawn(cmd, args, options, extra) {
const shell = (0, import_objects.getOwn)(options, "shell");
const WIN32 = process.platform === "win32";
let actualCmd = cmd;
if (WIN32 && shell && windowsScriptExtRegExp.test(actualCmd)) {
const path = /* @__PURE__ */ getPath();
actualCmd = path.basename(actualCmd, path.extname(actualCmd));
}
const {
spinner: optionsSpinner = spinner,
stripAnsi: shouldStripAnsi = true,
...spawnOptions
} = { __proto__: null, ...options };
const spinnerInstance = optionsSpinner;
const { env, stdio, stdioString = true } = spawnOptions;
const wasSpinning = !!spinnerInstance?.isSpinning;
const shouldStopSpinner = wasSpinning && !/* @__PURE__ */ isStdioType(stdio, "ignore") && !/* @__PURE__ */ isStdioType(stdio, "pipe");
const shouldRestartSpinner = shouldStopSpinner;
if (shouldStopSpinner) {
spinnerInstance.stop();
}
const npmCliPromiseSpawn = /* @__PURE__ */ getNpmcliPromiseSpawn();
const envToUse = env ? {
__proto__: null,
...process.env,
...env
} : process.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,
timeout: spawnOptions.timeout,
uid: spawnOptions.uid,
gid: spawnOptions.gid
};
const spawnPromise = npmCliPromiseSpawn(
actualCmd,
args ? [...args] : [],
promiseSpawnOpts,
extra
);
const oldSpawnPromise = spawnPromise;
let newSpawnPromise;
if (shouldStripAnsi && stdioString) {
newSpawnPromise = spawnPromise.then((result) => {
const strippedResult = /* @__PURE__ */ stripAnsiFromSpawnResult(result);
if ("code" in strippedResult) {
;
strippedResult.exitCode = strippedResult.code;
}
return strippedResult;
}).catch((error) => {
throw /* @__PURE__ */ stripAnsiFromSpawnResult(error);
});
} else {
newSpawnPromise = spawnPromise.then((result) => {
if ("code" in result) {
const res = result;
res.exitCode = result.code;
return res;
}
return result;
});
}
if (shouldRestartSpinner) {
newSpawnPromise = newSpawnPromise.finally(() => {
spinnerInstance.start();
});
}
;
newSpawnPromise.process = oldSpawnPromise.process;
newSpawnPromise.stdin = oldSpawnPromise.stdin;
return newSpawnPromise;
}
function spawnSync(cmd, args, options) {
const shell = (0, import_objects.getOwn)(options, "shell");
const WIN32 = process.platform === "win32";
let actualCmd = cmd;
if (WIN32 && shell && windowsScriptExtRegExp.test(actualCmd)) {
const path = /* @__PURE__ */ getPath();
actualCmd = path.basename(actualCmd, path.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 = (/* @__PURE__ */ getChildProcess()).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 ? /* @__PURE__ */ stripAnsiFromSpawnResult(result) : result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
isSpawnError,
isStdioType,
spawn,
spawnSync
});