vlt
Version:
The vlt CLI
96 lines (93 loc) • 3.25 kB
JavaScript
var global = globalThis;
import {Buffer} from "node:buffer";
import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers";
import {createRequire as _vlt_createRequire} from "node:module";
var require = _vlt_createRequire(import.meta.filename);
import {
error
} from "./chunk-RV3EHS4P.js";
// ../../src/promise-spawn/src/index.ts
import { spawn } from "node:child_process";
var isPipe = (stdio = "pipe", fd) => stdio === "pipe" || stdio === "overlapped" || stdio === null ? true : Array.isArray(stdio) ? isPipe(stdio[fd], fd) : false;
function stdioResult(stdout, stderr, o) {
return {
stdout: !isPipe(o.stdio, 1) ? null : o.stdioString !== false ? Buffer.concat(stdout).toString().trim() : Buffer.concat(stdout),
stderr: !isPipe(o.stdio, 2) ? null : o.stdioString !== false ? Buffer.concat(stderr).toString().trim() : Buffer.concat(stderr)
};
}
var SpawnPromise = class extends Promise {
[Symbol.toStringTag] = "SpawnPromise";
/** The spawned process this promise references */
process;
/** Expose the child process stdin, if available */
stdin;
/**
* Set static `Symbol.species` back to the base Promise class so that
* v8 doesn't get confused by the changed constructor signature.
*/
static get [Symbol.species]() {
return Promise;
}
constructor(command, args, opts, extra = {}) {
let proc;
super((res, rej) => {
proc = spawn(command, args, opts);
const stdout = [];
const stderr = [];
const reject = (er, status, signal) => {
const stdio = stdioResult(stdout, stderr, opts);
const errorResult = {
command,
args,
stdout: stdio.stdout,
stderr: stdio.stderr,
cwd: opts.cwd?.toString() ?? process.cwd()
};
if (er !== void 0) {
errorResult.cause = er;
}
if (status !== void 0) {
errorResult.status = status;
}
if (signal !== void 0) {
errorResult.signal = signal;
}
Object.assign(errorResult, extra);
rej(error("command failed", errorResult));
};
const resolve = (status, signal) => {
const stdio = stdioResult(stdout, stderr, opts);
const successResult = {
command,
args,
status,
signal,
stdout: stdio.stdout,
stderr: stdio.stderr,
cwd: opts.cwd?.toString() ?? process.cwd()
};
Object.assign(successResult, extra);
res(successResult);
};
proc.on("error", reject);
proc.stdout?.on("data", (c) => stdout.push(c)).on("error", (er) => reject(er));
proc.stderr?.on("data", (c) => stderr.push(c)).on("error", (er) => reject(er));
proc.on("close", (status = null, signal = null) => {
if ((status || signal) && !opts.acceptFail) {
reject(void 0, status, signal);
} else {
resolve(status, signal);
}
});
});
this.process = proc;
this.stdin = proc.stdin;
}
};
function promiseSpawn(command, args, opts = {}, extra = {}) {
return new SpawnPromise(command, args, opts, extra);
}
export {
promiseSpawn
};
//# sourceMappingURL=chunk-TJHWNOOA.js.map