Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/exec/RELEASES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
33 changes: 33 additions & 0 deletions packages/exec/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> exit code
*/
export async function getCommandOutput(
commandLine: string,
args?: [],
options: Omit<ExecOptions, 'listeners'> = {}
): Promise<string> {
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)
})
})
}