Skip to content

Commit

Permalink
refactor: extract command execution logic into buildComment function
Browse files Browse the repository at this point in the history
  • Loading branch information
DE7924 committed Dec 12, 2024
1 parent 5a606b5 commit 7400348
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 76 deletions.
66 changes: 29 additions & 37 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ export type stepResponse = { output: string; error: boolean };
export const failedEmoji = "❌";
export const passedEmoji = "✅";

export const buildComment = async (
commands: { label: string; command: string }[],
): Promise<string[]> => {
let commentBody = "\n";
let errorMessages = "";
for (const { label, command } of commands) {
const result = await runCommand(command, label);
if (result) {
commentBody += `<li>${failedEmoji} - ${label}
<details><summary>See details</summary>${result}</details></li>`;
errorMessages += `${result}`;
} else {
commentBody += `<li>${passedEmoji} - ${label}\n</li>`;
}
}
return [commentBody, errorMessages];
};

export const runCommand = async (
command: string,
label: string,
Expand Down
18 changes: 3 additions & 15 deletions src/scripts/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import { setFailed } from "@actions/core";
import { stepResponse, failedEmoji, passedEmoji, runCommand } from "src/main";
import { stepResponse, buildComment } from "src/main";

export const analyze = async (): Promise<stepResponse> => {
const results = [
const commands = [
{ label: "Custom Elements Manifest Analyzer", command: "npm run analyze" },
{ label: "ESLint", command: "npm run lint" },
{ label: "Lit Analyzer", command: "npm run lint:lit-analyzer" },
];

let commentBody = "\n";
let errorMessages = "";

for (const { label, command } of results) {
const result = await runCommand(command, label);
if (result) {
commentBody += `<li>${failedEmoji} - ${label}
<details><summary>See details</summary>${result}</details></li>`;
errorMessages += `${result}`;
} else {
commentBody += `<li>${passedEmoji} - ${label}\n</li>`;
}
}
let [commentBody, errorMessages] = await buildComment(commands);

Check failure on line 11 in src/scripts/analyze.ts

View workflow job for this annotation

GitHub Actions / code-quality

'commentBody' is never reassigned. Use 'const' instead

Check failure on line 11 in src/scripts/analyze.ts

View workflow job for this annotation

GitHub Actions / code-quality

'errorMessages' is never reassigned. Use 'const' instead

if (errorMessages) {
setFailed(errorMessages.trim());
Expand Down
18 changes: 9 additions & 9 deletions src/scripts/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { exec } from "@actions/exec";
import { setFailed } from "@actions/core";
import { stepResponse, failedEmoji, passedEmoji } from "src/main";
import { stepResponse, buildComment } from "src/main";

export const formatting = async (): Promise<stepResponse> => {
try {
// Run prettier
await exec("npm run prettier");
const commands = [{ label: "Prettier", command: "npm run prettier" }];

return { output: `<li>${passedEmoji} - Formatting</li>`, error: false };
} catch (error) {
if (error instanceof Error) setFailed(error.message);
return { output: `<li>${failedEmoji} - Formatting</li>`, error: true };
let [commentBody, errorMessages] = await buildComment(commands);

Check failure on line 7 in src/scripts/formatting.ts

View workflow job for this annotation

GitHub Actions / code-quality

'commentBody' is never reassigned. Use 'const' instead

Check failure on line 7 in src/scripts/formatting.ts

View workflow job for this annotation

GitHub Actions / code-quality

'errorMessages' is never reassigned. Use 'const' instead

if (errorMessages) {
setFailed(errorMessages.trim());
return { output: commentBody.trim(), error: true };
} else {
return { output: commentBody.trim(), error: false };
}
};
17 changes: 3 additions & 14 deletions src/scripts/testing.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { setFailed } from "@actions/core";
import { stepResponse, failedEmoji, passedEmoji, runCommand } from "src/main";
import { stepResponse, buildComment } from "src/main";

export const testing = async (): Promise<stepResponse> => {
const results = [
const commands = [
{ label: "Testing", command: "npm run test -- --coverage" },
{ label: "TSDoc", command: "npm run docs" },
];

let commentBody = "\n";
let errorMessages = "";

for (const { label, command } of results) {
const result = await runCommand(command, label);
if (result) {
commentBody += `<li>${failedEmoji} - ${label}</li>`;
errorMessages += `${result}`;
} else {
commentBody += `<li>${passedEmoji} - ${label}</li>`;
}
}
let [commentBody, errorMessages] = await buildComment(commands);

Check failure on line 10 in src/scripts/testing.ts

View workflow job for this annotation

GitHub Actions / code-quality

'commentBody' is never reassigned. Use 'const' instead

Check failure on line 10 in src/scripts/testing.ts

View workflow job for this annotation

GitHub Actions / code-quality

'errorMessages' is never reassigned. Use 'const' instead

if (errorMessages) {
setFailed(errorMessages.trim());
Expand Down

0 comments on commit 7400348

Please sign in to comment.