Skip to content

Commit

Permalink
Merge pull request #171 from cloudflare/cina/refactor-subprocess-exec
Browse files Browse the repository at this point in the history
Refactor subprocess execution to use `@actions/exec`
  • Loading branch information
1000hz authored Oct 19, 2023
2 parents 5330973 + 473d9cb commit 819ea00
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-insects-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler-action": patch
---

Fixed issues that caused the action to fail if any secret or var values contained shell metacharacters.
5 changes: 5 additions & 0 deletions .changeset/warm-mugs-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler-action": patch
---

Bumped `DEFAULT_WRANGLER_VERSION` to 3.13.2
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"check": "prettier --check ."
},
"dependencies": {
"@actions/core": "^1.10.0"
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1"
},
"devDependencies": {
"@changesets/changelog-github": "^0.4.8",
Expand Down
54 changes: 54 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
exec as _childProcessExec,
type ExecException,
} from "node:child_process";
import { EOL } from "node:os";
import { promisify } from "node:util";

export { exec } from "@actions/exec";

const childProcessExec = promisify(_childProcessExec);

type ExecAsyncException = ExecException & {
stderr: string;
stdout: string;
};

function isExecAsyncException(err: unknown): err is ExecAsyncException {
return err instanceof Error && "code" in err && "stderr" in err;
}

export async function execShell(
command: string,
{
silent = false,
...options
}: Parameters<typeof childProcessExec>[1] & { silent?: boolean } = {},
) {
if (!silent) {
process.stdout.write("[command]" + command + EOL);
}

try {
const promise = childProcessExec(command, {
...options,
});

const { child } = promise;

if (!silent) {
child.stdout?.on("data", (data: Buffer) => process.stdout.write(data));
child.stderr?.on("data", (data: Buffer) => process.stderr.write(data));
}

await promise;
return child.exitCode;
} catch (err: any) {
if (isExecAsyncException(err)) {
process.stderr.write(err.stderr);
throw new Error(`Process failed with exit code ${err.code}`);
}

throw err;
}
}
Loading

0 comments on commit 819ea00

Please sign in to comment.