-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
execute.ts
58 lines (51 loc) 路 1.48 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {exec} from '@actions/exec'
import buffer from 'buffer'
type ExecuteOutput = {
stdout: string
stderr: string
}
const output: ExecuteOutput = {stdout: '', stderr: ''}
/** Wrapper around the GitHub toolkit exec command which returns the output.
* Also allows you to easily toggle the current working directory.
*
* @param {string} cmd - The command to execute.
* @param {string} cwd - The current working directory.
* @param {boolean} silent - Determines if the in/out should be silenced or not.
* @param {boolean} ignoreReturnCode - Determines whether to throw an error
* on a non-zero exit status or to leave implementation up to the caller.
*/
export async function execute(
cmd: string,
cwd: string,
silent: boolean,
ignoreReturnCode = false
): Promise<ExecuteOutput> {
output.stdout = ''
output.stderr = ''
await exec(cmd, [], {
// Silences the input unless the INPUT_DEBUG flag is set.
silent,
cwd,
listeners: {stdout, stderr},
ignoreReturnCode
})
return Promise.resolve(output)
}
export function stdout(data: Buffer | string): void {
const dataString = data.toString().trim()
if (
output.stdout.length + dataString.length <
buffer.constants.MAX_STRING_LENGTH
) {
output.stdout += dataString
}
}
export function stderr(data: Buffer | string): void {
const dataString = data.toString().trim()
if (
output.stderr.length + dataString.length <
buffer.constants.MAX_STRING_LENGTH
) {
output.stderr += dataString
}
}