|
21 | 21 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
22 | 22 | // THE POSSIBILITY OF SUCH DAMAGE. |
23 | 23 |
|
24 | | -import { styleText } from "node:util"; |
25 | 24 | import core from "@actions/core"; |
| 25 | +import { spawn } from "child_process"; |
26 | 26 | import commandLineUsage from "command-line-usage"; |
| 27 | +import { styleText } from "node:util"; |
27 | 28 |
|
28 | | -export const GITHUB_ACTIONS_OUTPUT = "GITHUB_ACTIONS_OUTPUT" in process.env; |
| 29 | +export const GITHUB_ACTIONS_OUTPUT = ("GITHUB_ACTIONS_OUTPUT" in process.env) || ("GITHUB_EVENT_PATH" in process.env); |
29 | 30 |
|
30 | 31 | export function logInfo(...args) { |
31 | 32 | const text = args.join(" ") |
@@ -53,6 +54,16 @@ export function logError(...args) { |
53 | 54 | } |
54 | 55 | } |
55 | 56 |
|
| 57 | +export function logCommand(...args) { |
| 58 | + const cmd = args.join(" "); |
| 59 | + if (GITHUB_ACTIONS_OUTPUT) { |
| 60 | + core.notice(styleText("blue", cmd)); |
| 61 | + } else { |
| 62 | + console.log(styleText("blue", cmd)); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
56 | 67 | export async function logGroup(name, body) { |
57 | 68 | if (GITHUB_ACTIONS_OUTPUT) { |
58 | 69 | core.startGroup(name); |
@@ -103,3 +114,49 @@ export async function runTest(label, testFunction) { |
103 | 114 | } |
104 | 115 | return true; |
105 | 116 | } |
| 117 | + |
| 118 | + |
| 119 | +export async function sh(binary, ...args) { |
| 120 | + const cmd = `${binary} ${args.join(" ")}`; |
| 121 | + if (GITHUB_ACTIONS_OUTPUT) |
| 122 | + core.startGroup(binary); |
| 123 | + logCommand(cmd); |
| 124 | + try { |
| 125 | + return await spawnCaptureStdout(binary, args); |
| 126 | + } catch(e) { |
| 127 | + logError(e.stdoutString); |
| 128 | + throw e; |
| 129 | + } finally { |
| 130 | + if (GITHUB_ACTIONS_OUTPUT) |
| 131 | + core.endGroup(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +const SPAWN_OPTIONS = Object.freeze({ |
| 136 | + stdio: ["inherit", "pipe", "inherit"] |
| 137 | +}); |
| 138 | + |
| 139 | +async function spawnCaptureStdout(binary, args, options={}) { |
| 140 | + options = Object.assign(options, SPAWN_OPTIONS); |
| 141 | + const childProcess = spawn(binary, args, options); |
| 142 | + childProcess.stdout.pipe(process.stdout); |
| 143 | + return new Promise((resolve, reject) => { |
| 144 | + childProcess.stdoutString = ""; |
| 145 | + childProcess.stdio[1].on("data", (data) => { |
| 146 | + childProcess.stdoutString += data.toString(); |
| 147 | + }); |
| 148 | + childProcess.on("close", (code) => { |
| 149 | + if (code === 0) { |
| 150 | + resolve(childProcess); |
| 151 | + } else { |
| 152 | + // Reject the Promise with an Error on failure |
| 153 | + const error = new Error(`Command failed with exit code ${code}: ${binary} ${args.join(" ")}`); |
| 154 | + error.process = childProcess; |
| 155 | + error.stdout = childProcess.stdoutString; |
| 156 | + error.exitCode = code; |
| 157 | + reject(error); |
| 158 | + } |
| 159 | + }); |
| 160 | + childProcess.on("error", reject); |
| 161 | + }) |
| 162 | +} |
0 commit comments