From 8d6fade5ad238725b0bd4cc5f736ef06ea23f82f Mon Sep 17 00:00:00 2001 From: GrantBirki Date: Fri, 1 Dec 2023 11:04:51 -0700 Subject: [PATCH] save primary wrangler command output to a file (optionally) --- action.yml | 7 +++++++ src/index.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index b5fda3e..c85f8de 100644 --- a/action.yml +++ b/action.yml @@ -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`." diff --git a/src/index.ts b/src/index.ts index e5a18a5..760ef6d 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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();