diff --git a/packages/exec/RELEASES.md b/packages/exec/RELEASES.md index 0e99d15653..0dd2ead1c8 100644 --- a/packages/exec/RELEASES.md +++ b/packages/exec/RELEASES.md @@ -1,5 +1,9 @@ # @actions/exec Releases +### 1.1.0 + +- [Add `getCommandOutput`](https://github.com/actions/toolkit/pull/770) + ### 1.0.3 - [Add \"types\" to package.json](https://github.com/actions/toolkit/pull/221) diff --git a/packages/exec/src/exec.ts b/packages/exec/src/exec.ts index 80a0363c49..a3cd1afde5 100644 --- a/packages/exec/src/exec.ts +++ b/packages/exec/src/exec.ts @@ -28,3 +28,36 @@ export async function exec( const runner: tr.ToolRunner = new tr.ToolRunner(toolPath, args, options) return runner.exec() } + +/** + * Get command output + * Returns promise with stdout + * + * @param commandLine command to execute (can include additional args). Must be correctly escaped. + * @param args optional arguments for tool. Escaping is handled by the lib. + * @param options optional exec options. See ExecOptions + * @returns Promise exit code + */ +export async function getCommandOutput( + commandLine: string, + args?: [], + options: Omit = {} +): Promise { + return new Promise((resolve, reject) => { + let out = '' + let err = '' + + const opts = { + ...options, + listeners: { + stdout: (data: Buffer) => (out += data.toString()), + stderr: (data: Buffer) => (err += data.toString()) + } + } + + exec(commandLine, args, opts).then(() => { + if (err) return reject(err) + resolve(out) + }) + }) +}