Refs https://github.com/getpaseo/paseo/discussions/8484 Refs https://github.com/getpaseo/paseo/discussions/8017 Co-authored-by: CL Kao <clkao@datarecce.io> Co-authored-by: Josh Bendavid <Josh.Bendavid@cern.ch>
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import type { ChildProcess } from "node:child_process";
|
|
import {
|
|
spawnProcess as spawnCli,
|
|
execCommand as execCli,
|
|
type SpawnProcessOptions as CliSpawnOptions,
|
|
} from "@getpaseo/plugin/server";
|
|
import { createExternalCommandProcessEnv, type ProcessEnvRecord } from "../server/paseo-env.js";
|
|
|
|
interface ExternalEnvOptions {
|
|
baseEnv?: ProcessEnvRecord;
|
|
envMode?: "external" | "internal";
|
|
env?: ProcessEnvRecord;
|
|
envOverlay?: ProcessEnvRecord;
|
|
}
|
|
|
|
export type SpawnProcessOptions = Omit<CliSpawnOptions, "env"> & ExternalEnvOptions;
|
|
|
|
interface ExecCommandOptions extends ExternalEnvOptions {
|
|
cwd?: string;
|
|
encoding?: BufferEncoding;
|
|
killSignal?: NodeJS.Signals;
|
|
timeout?: number;
|
|
maxBuffer?: number;
|
|
shell?: boolean | string;
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
interface ExecCommandResult {
|
|
stdout: string;
|
|
stderr: string;
|
|
}
|
|
|
|
export function spawnProcess(
|
|
command: string,
|
|
args: string[],
|
|
options?: SpawnProcessOptions,
|
|
): ChildProcess {
|
|
const { baseEnv, env, envOverlay, ...spawnOptions } = options ?? {};
|
|
const resolvedBaseEnv = env ?? baseEnv ?? process.env;
|
|
const childEnv =
|
|
options?.envMode === "internal"
|
|
? ({ ...resolvedBaseEnv, ...envOverlay } as NodeJS.ProcessEnv)
|
|
: createExternalCommandProcessEnv(
|
|
command,
|
|
resolvedBaseEnv,
|
|
...(envOverlay ? [envOverlay] : []),
|
|
);
|
|
|
|
return spawnCli(command, args, {
|
|
...spawnOptions,
|
|
env: childEnv,
|
|
signal: options?.signal,
|
|
});
|
|
}
|
|
|
|
export async function execCommand(
|
|
command: string,
|
|
args: string[],
|
|
options?: ExecCommandOptions,
|
|
): Promise<ExecCommandResult> {
|
|
const { baseEnv, env, envOverlay } = options ?? {};
|
|
const resolvedBaseEnv = env ?? baseEnv ?? process.env;
|
|
const childEnv =
|
|
options?.envMode === "internal"
|
|
? ({ ...resolvedBaseEnv, ...envOverlay } as NodeJS.ProcessEnv)
|
|
: createExternalCommandProcessEnv(
|
|
command,
|
|
resolvedBaseEnv,
|
|
...(envOverlay ? [envOverlay] : []),
|
|
);
|
|
|
|
return execCli(command, args, {
|
|
cwd: options?.cwd,
|
|
env: childEnv,
|
|
shell: options?.shell,
|
|
encoding: options?.encoding ?? "utf8",
|
|
signal: options?.signal,
|
|
killSignal: options?.killSignal,
|
|
timeout: options?.timeout,
|
|
maxBuffer: options?.maxBuffer,
|
|
}) as Promise<ExecCommandResult>;
|
|
}
|