"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const exec = __importStar(require("@actions/exec")); /** * Wrapper for exec.exec which checks for specific return code and/or regex matches in console output. * Output will be streamed to the live console as well as captured for subsequent processing. * Returns promise with return code * * @param commandLine command to execute (can include additional args). Must be correctly escaped. * @param matchers defines specific codes and/or regexes that should lead to return of a custom error * @param args optional arguments for tool. Escaping is handled by the lib. * @param options optional exec options. See ExecOptions * @returns Promise exit code */ async function exec_wrapper(commandLine, args, matchers, options) { var _a; let stdout = ''; let stderr = ''; // custom listeners to store stdout and stderr, while also replicating the behaviour of the passed listeners const originalListener = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners; let listeners = { stdout: (data) => { var _a; stdout += data.toString(); if (((_a = originalListener) === null || _a === void 0 ? void 0 : _a.stdout) !== undefined) { originalListener.stdout(data); } else { // if no stdout listener was originally defined then match behaviour of exec.exec process.stdout.write(data); } }, stderr: (data) => { var _a; stderr += data.toString(); if (((_a = originalListener) === null || _a === void 0 ? void 0 : _a.stderr) !== undefined) { originalListener.stderr(data); } else { // if no stderr listener was originally defined then match behaviour of exec.exec process.stderr.write(data); } } }; // we capture the original return code and error so that (if no match is found) we can duplicate the behaviour let originalReturnCode; let originalError; try { originalReturnCode = await exec.exec(commandLine, args, { listeners: listeners, ...options }); } catch (e) { originalError = e; originalReturnCode = 1; // TODO linter insists, but presumably there's a better way to do _all_ this... } if (matchers) { for (const [customCode, regex, message] of matchers) { if (customCode === originalReturnCode || regex.test(stderr) || regex.test(stdout)) { throw new Error(message); } } } if (originalError) throw originalError; return originalReturnCode; } exports.exec_wrapper = exec_wrapper; //# sourceMappingURL=exec_wrapper.js.map