Skip to content

Commit

Permalink
Update .changeset/itchy-buses-grow.md
Browse files Browse the repository at this point in the history
Co-authored-by: Cina Saffary <[email protected]>
  • Loading branch information
JacobMGEvans and 1000hz committed Aug 10, 2023
1 parent 74433eb commit 88466ea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .changeset/itchy-buses-grow.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"wrangler-action": patch
---

Additional Error Handling
Added more error logging when a command fails to execute
Previously, we prevented any error logs from propagating too far to prevent leaking of any potentially sensitive information. However, this made it difficult for developers to debug their code.

In this release, we have updated our error handling to allow for more error messaging from pre/post and custom commands. We still discourage the use of these commands for secrets or other sensitive information, but we believe this change will make it easier for developers to debug their code.
Expand Down
101 changes: 50 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,28 @@ function semverCompare(version1: string, version2: string) {
async function main() {
installWrangler();
authenticationSetup();
await execCommands(getMultilineInput("preCommands"), "Pre");
await execCommands(getMultilineInput("preCommands"), "pre");
await uploadSecrets();
await wranglerCommands();
await execCommands(getMultilineInput("postCommands"), "Post");
await execCommands(getMultilineInput("postCommands"), "post");
}

async function runProcess(
command: Parameters<typeof execAsync>[0],
options: Parameters<typeof execAsync>[1],
) {
try {
const result = await execAsync(command, options);

result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());

return result;
} catch (err: any) {
err.stdout && info(err.stdout.toString());
err.stderr && error(err.stderr.toString());
throw err;
}
}

function checkWorkingDirectory(workingDirectory = ".") {
Expand Down Expand Up @@ -100,7 +118,7 @@ async function execCommands(commands: string[], cmdType: string) {
if (!commands.length) {
return;
}
startGroup(`πŸš€ ${cmdType} Commands Group`);
startGroup(`πŸš€ Running ${cmdType}Commands`);

const arrPromises = commands.map(async (command) => {
const cmd = command.startsWith("wrangler")
Expand All @@ -109,15 +127,16 @@ async function execCommands(commands: string[], cmdType: string) {

info(`πŸš€ Executing command: ${cmd}`);

return await execAsync(cmd, {
return await runProcess(cmd, {
cwd: config["workingDirectory"],
env: process.env,
});
});

await Promise.all(arrPromises).catch((result) => {
error(`🚨 ${cmdType} Command failed`);
setFailed(result);
result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());
setFailed(`🚨 ${cmdType}Commands failed`);
});
endGroup();
}
Expand Down Expand Up @@ -149,7 +168,7 @@ async function legacyUploadSecrets(
const command = `echo ${getSecret(
secret,
)} | ${getNpxCmd()} wrangler secret put ${secret}`;
return environment ? command.concat(`--env ${environment}`) : command;
return environment ? command.concat(` --env ${environment}`) : command;
})
.map(
async (command) =>
Expand Down Expand Up @@ -219,63 +238,43 @@ function getVarArgs() {
return envVarArray.length > 0 ? `--var ${envVarArray.join(" ").trim()}` : "";
}

function defaultCommandBehavior() {
const environment = config["ENVIRONMENT"];
const wranglerVersion = config["WRANGLER_VERSION"];
const workingDirectory = config["workingDirectory"];

const deployCommand = semverCompare("2.20.0", wranglerVersion)
? "deploy"
: "publish";

info(`πŸ“Œ No Wrangler commands were provided, executing default deployment.`);

if (environment.length === 0) {
execSync(
`${getNpxCmd()} wrangler ${deployCommand} ${getVarArgs()}`.trim(),
{
cwd: workingDirectory,
env: process.env,
},
);
} else {
execSync(
`${getNpxCmd()} wrangler ${deployCommand} --env ${environment} ${getVarArgs()}`.trim(),
{ cwd: workingDirectory, env: process.env },
);
}
endGroup();
}

async function wranglerCommands() {
startGroup("πŸš€ Running Wrangler Commands");
const commands = config["COMMANDS"];
const environment = config["ENVIRONMENT"];

if (!commands.length) {
defaultCommandBehavior();
return;
const wranglerVersion = config["WRANGLER_VERSION"];
const deployCommand = semverCompare("2.20.0", wranglerVersion)
? "deploy"
: "publish";
commands.push(deployCommand);
}
startGroup("πŸš€ Executing Wrangler Commands");

const arrPromises = commands.map(async (command) => {
if (environment.length > 0 && !command.includes(`--env ${environment}`)) {
command.concat(`--env ${environment}`);
if (environment.length > 0 && !command.includes(`--env`)) {
command = command.concat(` --env ${environment}`);
}
const result = await execAsync(
`${getNpxCmd()} wrangler ${command} ${getVarArgs()}`,
{
cwd: config["workingDirectory"],
env: process.env,
},
);

info(result.stdout);
return result;
const cmd = `${getNpxCmd()} wrangler ${command} ${
(command.startsWith("deploy") || command.startsWith("publish")) &&
!command.includes(`--var`)
? getVarArgs()
: ""
}`.trim();

info(`πŸš€ Executing command: ${cmd}`);

return await runProcess(cmd, {
cwd: config["workingDirectory"],
env: process.env,
});
});

await Promise.all(arrPromises).catch((result) => {
error(`🚨 Command failed`);
setFailed(result.stderr);
result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());
setFailed(`🚨 Command failed`);
});

endGroup();
Expand Down

0 comments on commit 88466ea

Please sign in to comment.