Skip to content

Commit

Permalink
update .gitignore, enhance VSCode launch configuration, and refactor …
Browse files Browse the repository at this point in the history
…error handling in main.ts and testing.ts
  • Loading branch information
DE7924 committed Dec 12, 2024
1 parent 3cad312 commit dc1834d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ __tests__/runner/*
# TS Docs
docs

# VSCode
.vscode
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"request": "launch",
"runtimeExecutable": "npx",
"cwd": "${workspaceRoot}",
"args": ["local-action", ".", "src/main.ts", ".env"],
"args": ["local-action", ".", "src/main.ts", ".env", "-- --local"],
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"env": {
"NODE_OPTIONS": "--experimental-specifier-resolution=node"
}
},
"preLaunchTask": "npm: install"
}
]
}
77 changes: 44 additions & 33 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.

22 changes: 21 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBooleanInput, getInput, setFailed } from "@actions/core";
import { getBooleanInput, getInput, setFailed, debug } from "@actions/core";
import { exec } from "@actions/exec";
import { getOctokit, context } from "@actions/github";
import { analyze } from "./scripts/analyze";
Expand All @@ -13,6 +13,26 @@ export type stepResponse = { output: string; error: boolean };
export const failedEmoji = "❌";
export const passedEmoji = "✅";

export const runCommand = async (
command: string,
label: string,
): Promise<string | boolean> => {
try {
await exec(command);
return false;
} catch (error: unknown) {
if (error instanceof Error) {
debug(`${label} failed: ${error.message}`);
return error.message;
} else if (typeof error === "string") {
debug(`${label} failed: ${error}`);
return error;
} else {
return true;
}
}
};

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
Expand Down
25 changes: 2 additions & 23 deletions src/scripts/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { exec } from "@actions/exec";
import { setFailed, debug } from "@actions/core";
import { stepResponse, failedEmoji, passedEmoji } from "src/main";

const runCommand = async (
command: string,
label: string,
): Promise<string | boolean> => {
try {
await exec(command);
return false;
} catch (error: unknown) {
if (error instanceof Error) {
debug(`${label} failed: ${error.message}`);
return error.message;
} else if (typeof error === "string") {
debug(`${label} failed: ${error}`);
return error;
} else {
return true;
}
}
};
import { setFailed } from "@actions/core";
import { stepResponse, failedEmoji, passedEmoji, runCommand } from "src/main";

export const analyze = async (): Promise<stepResponse> => {
const results = [
Expand Down
32 changes: 22 additions & 10 deletions src/scripts/testing.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { exec } from "@actions/exec";

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

View workflow job for this annotation

GitHub Actions / code-quality

'exec' is defined but never used
import { setFailed } from "@actions/core";
import { stepResponse } from "src/main";
import { stepResponse, failedEmoji, passedEmoji, runCommand } from "src/main";

export const testing = async (): Promise<stepResponse> => {
try {
// Run tests and generate coverage
await exec("npm run test -- --coverage");
const results = [
{ label: "Testing", command: "npm run test -- --coverage" },
{ label: "TSDoc", command: "npm run docs" },
];

// Test tsdoc
await exec("npm run docs");
let commentBody = "\n";
let errorMessages = "";

return { output: "Testing complete", error: false };
} catch (error) {
if (error instanceof Error) setFailed(error.message);
return { output: "Testing failed", error: true };
for (const { label, command } of results) {
const result = await runCommand(command, label);
if (result) {
commentBody += `${failedEmoji} - ${label}\n`;
errorMessages += `${result}\n`;
} else {
commentBody += `${passedEmoji} - ${label}\n`;
}
}

if (errorMessages) {
setFailed(errorMessages.trim());
return { output: commentBody.trim(), error: true };
} else {
return { output: commentBody.trim(), error: false };
}
};

0 comments on commit dc1834d

Please sign in to comment.