UNPKG

snyk-go-plugin

Version:
58 lines • 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runGo = exports.execute = void 0; const childProcess = require("child_process"); const cli_interface_1 = require("@snyk/cli-interface"); const debug_1 = require("./debug"); async function execute(command, args, options, shell = false) { const spawnOptions = { shell, env: { ...process.env }, }; if (options === null || options === void 0 ? void 0 : options.cwd) { spawnOptions.cwd = options.cwd; } if (options === null || options === void 0 ? void 0 : options.env) { spawnOptions.env = { ...process.env, ...options.env }; } spawnOptions.env = (0, cli_interface_1.cleanSnykEnv)(spawnOptions.env, process.env); return new Promise((resolve, reject) => { let stdout = ''; let stderr = ''; const proc = childProcess.spawn(command, args, spawnOptions); if (proc.stdout) { proc.stdout.on('data', (data) => { stdout = stdout + data; }); } if (proc.stderr) { proc.stderr.on('data', (data) => { stderr = stderr + data; }); } proc.on('close', (code) => { if (code !== 0) { return reject(stdout || stderr); } resolve(stdout || stderr); }); }); } exports.execute = execute; async function runGo(args, options, additionalGoCommands = []) { try { return await execute('go', args, options); } catch (err) { const [command] = /(go mod download)|(go get [^"]*)/.exec(err) || []; if (command && !additionalGoCommands.includes(command)) { (0, debug_1.debug)('running command:', command); const newArgs = command.split(' ').slice(1); await execute('go', newArgs, options); return runGo(args, options, additionalGoCommands.concat(command)); } throw err; } } exports.runGo = runGo; //# sourceMappingURL=sub-process.js.map