save primary wrangler command output to a file (optionally)

This commit is contained in:
GrantBirki 2023-12-01 11:04:51 -07:00
parent 65996a5230
commit 8d6fade5ad
No known key found for this signature in database
GPG key ID: 9029BBCCC15C6F8C
2 changed files with 52 additions and 2 deletions

View file

@ -44,3 +44,10 @@ inputs:
packageManager:
description: "The package manager you'd like to use to install and run wrangler. If not specified, the preferred package manager will be inferred based on the presence of a lockfile or fallback to using npm if no lockfile is found. Valid values are `npm` | `pnpm` | `yarn` | `bun`."
required: false
outputToFile:
description: "The path to a file to write the output of the Wrangler command to. If not specified, the output will be written to the console."
required: false
default: "false"
outputs:
wranglerCommandOutputFile:
description: "The path to a file containing the output of the Wrangler command if `outputToFile` is set to `true`."

View file

@ -7,7 +7,10 @@ import {
info as originalInfo,
startGroup as originalStartGroup,
setFailed,
setOutput,
} from "@actions/core";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils";
import { getPackageManager } from "./packageManagers";
@ -28,6 +31,7 @@ const config = {
COMMANDS: getMultilineInput("command"),
QUIET_MODE: getBooleanInput("quiet"),
PACKAGE_MANAGER: getInput("packageManager"),
OUTPUT_TO_FILE: getBooleanInput("outputToFile"),
} as const;
const packageManager = getPackageManager(config.PACKAGE_MANAGER, {
@ -242,10 +246,49 @@ async function wranglerCommands() {
}
}
await exec(`${packageManager.exec} wrangler ${command}`, args, {
// Used for saving the wrangler output to a file
let stdOut = "";
let stdErr = "";
// Construct the options for the exec command
const options = {
cwd: config["workingDirectory"],
silent: config["QUIET_MODE"],
});
listeners: {
stdout: (data: Buffer) => {
stdOut += data.toString();
},
stderr: (data: Buffer) => {
stdErr += data.toString();
},
},
};
// Execute the wrangler command
await exec(`${packageManager.exec} wrangler ${command}`, args, options);
// If the user has specified to output the wrangler command to a file save the stdout and stderr
if (config["OUTPUT_TO_FILE"]) {
// Create the output data in a machine readable format
const outputData = {
stdOut: stdOut,
stdErr: stdErr,
};
// Consturct the file output path to use the current working directory
const outputFilePath = join(
config["workingDirectory"],
"wrangler-command-output.json",
);
// Write the output to a JSON file
writeFileSync(outputFilePath, JSON.stringify(outputData));
info(
`✅ wrangler-command-output output saved to ${outputFilePath}`,
true,
);
setOutput("wranglerCommandOutputFile", outputFilePath);
}
}
} finally {
endGroup();