snyk-docker-plugin
Version:
Snyk CLI docker plugin
57 lines (50 loc) • 1.61 kB
text/typescript
import * as childProcess from "child_process";
import { quoteAll } from "shescape/stateless";
export { execute, CmdOutput };
interface CmdOutput {
stdout: string;
stderr: string;
}
function execute(
command: string,
args: string[],
options?,
): Promise<CmdOutput> {
const spawnOptions: any = {
shell: process.platform !== "win32" ? "/bin/bash" : true,
env: { ...process.env },
};
if (options && options.cwd) {
spawnOptions.cwd = options.cwd;
}
args = quoteAll(args, { ...spawnOptions, flagProtection: false });
// Before spawning an external process, we look if we need to restore the system proxy configuration,
// which overrides the cli internal proxy configuration.
if (process.env.SNYK_SYSTEM_HTTP_PROXY !== undefined) {
spawnOptions.env.HTTP_PROXY = process.env.SNYK_SYSTEM_HTTP_PROXY;
}
if (process.env.SNYK_SYSTEM_HTTPS_PROXY !== undefined) {
spawnOptions.env.HTTPS_PROXY = process.env.SNYK_SYSTEM_HTTPS_PROXY;
}
if (process.env.SNYK_SYSTEM_NO_PROXY !== undefined) {
spawnOptions.env.NO_PROXY = process.env.SNYK_SYSTEM_NO_PROXY;
}
return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";
const proc = childProcess.spawn(command, args, spawnOptions);
proc.stdout.on("data", (data) => {
stdout = stdout + data;
});
proc.stderr.on("data", (data) => {
stderr = stderr + data;
});
proc.on("close", (code) => {
const output = { stdout, stderr };
if (code !== 0) {
return reject(output);
}
resolve(output);
});
}) as Promise<CmdOutput>;
}