-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
67 lines (53 loc) · 1.67 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
const { spawn } = require('child_process')
const { Transform } = require('stream')
const { Buffer } = require('buffer')
const process = require('process')
const core = require('@actions/core')
// Default shell invocation used by GitHub Action 'run:'
const shellArgs = {
bash: ['--noprofile', '--norc', '-eo', 'pipefail', '-c'],
sh: ['-e', '-c'],
python: ['-c'],
pwsh: ['-command', '.'],
powershell: ['-command', '.']
}
class RecordStream extends Transform {
constructor () {
super()
this._data = Buffer.from([])
}
get output () {
return this._data
}
_transform (chunk, encoding, callback) {
this._data = Buffer.concat([this._data, chunk])
callback(null, chunk)
}
}
function run (command, shell) {
return new Promise((resolve, reject) => {
const outRec = new RecordStream()
const errRec = new RecordStream()
const args = shellArgs[shell]
if (!args) {
return reject(new Error(`Option "shell" must be one of: ${Object.keys(shellArgs).join(', ')}.`))
}
// Execute the command
const cmd = spawn(shell, [...args, command])
// Record stream output and pass it through main process
cmd.stdout.pipe(outRec).pipe(process.stdout)
cmd.stderr.pipe(errRec).pipe(process.stderr)
cmd.on('error', error => reject(error))
cmd.on('close', code => {
core.setOutput('stdout', outRec.output.toString())
core.setOutput('stderr', errRec.output.toString())
if (code === 0) {
resolve()
} else {
reject(new Error(`Process completed with exit code ${code}.`))
}
})
})
}
run(core.getInput('run'), core.getInput('shell'))
.catch(error => core.setFailed(error.message))