From d7b762efbefc5bf022c75e536c04d02074e60c9b Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Thu, 8 Aug 2024 15:13:22 +0700 Subject: [PATCH] feat: utilize GitHub Actions Utilities package --- dist/index.mjs | 44 +++++++++++++++++++++++++++++++------------- package.json | 2 +- src/context.ts | 11 +---------- src/index.ts | 13 ++++--------- yarn.lock | 16 ++++++++-------- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/dist/index.mjs b/dist/index.mjs index 9d69773..fe10a25 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -3,7 +3,34 @@ import os from 'node:os'; import { execFileSync } from 'node:child_process'; import path from 'node:path'; -function r(r){return function(r){if("object"==typeof(e=r)&&null!==e&&"message"in e&&"string"==typeof e.message)return r;var e;try{return new Error(JSON.stringify(r))}catch(e){return new Error(String(r))}}(r).message} +/** + * Retrieves the value of a GitHub Actions input. + * + * @param name - The name of the GitHub Actions input. + * @returns The value of the GitHub Actions input, or an empty string if not found. + */ +function getInput(name) { + const value = process.env[`INPUT_${name.toUpperCase()}`] || ""; + return value.trim(); +} +/** + * Sets the value of a GitHub Actions output. + * + * @param name - The name of the GitHub Actions output. + * @param value - The value of the GitHub Actions output + */ +function setOutput(name, value) { + fs.appendFileSync(process.env["GITHUB_OUTPUT"], `${name}=${value}${os.EOL}`); +} +/** + * Logs an error message on GitHub Actions. + * + * @param err - The error, which can be of any type. + */ +function error(err) { + const message = err instanceof Error ? err.message : String(err); + process.stdout.write(`::error::${message}${os.EOL}`); +} /** * Configures the build system of a CMake project. @@ -34,15 +61,6 @@ function buildProject(context) { }); } -/** - * Retrieves an action input. - * @param key - The key of the action input. - * @returns The action input value as a string. - */ -function getInput(key) { - const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; - return value.trim(); -} function getContext() { const sourceDir = getInput("source-dir"); const options = []; @@ -91,12 +109,12 @@ function getContext() { try { const context = getContext(); configureProject(context); - fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`); + setOutput("build-dir", context.buildDir); if (context.build.enabled) { buildProject(context); } } catch (err) { - process.exitCode = 1; - process.stdout.write(`::error::${r(err)}${os.EOL}`); + error(err); + process.exit(1); } diff --git a/package.json b/package.json index 535825d..b8e6f87 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test": "jest" }, "dependencies": { - "catched-error-message": "^0.0.1" + "gha-utils": "^0.1.0" }, "devDependencies": { "@eslint/js": "^9.8.0", diff --git a/src/context.ts b/src/context.ts index 8f6ac2a..c8a20b4 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,3 +1,4 @@ +import { getInput } from "gha-utils"; import path from "node:path"; export interface Context { @@ -14,16 +15,6 @@ export interface Context { }; } -/** - * Retrieves an action input. - * @param key - The key of the action input. - * @returns The action input value as a string. - */ -function getInput(key: string): string { - const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; - return value.trim(); -} - export function getContext(): Context { const sourceDir = getInput("source-dir"); const options: string[] = []; diff --git a/src/index.ts b/src/index.ts index 5238f92..494beee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ -import { getErrorMessage } from "catched-error-message"; -import fs from "node:fs"; -import os from "node:os"; +import { error, setOutput } from "gha-utils"; import { buildProject, configureProject } from "./cmake.js"; import { getContext } from "./context.js"; @@ -9,15 +7,12 @@ try { configureProject(context); - fs.appendFileSync( - process.env["GITHUB_OUTPUT"] as string, - `build-dir=${context.buildDir}${os.EOL}`, - ); + setOutput("build-dir", context.buildDir); if (context.build.enabled) { buildProject(context); } } catch (err) { - process.exitCode = 1; - process.stdout.write(`::error::${getErrorMessage(err)}${os.EOL}`); + error(err); + process.exit(1); } diff --git a/yarn.lock b/yarn.lock index 6f1c40b..b8e7102 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1670,13 +1670,6 @@ __metadata: languageName: node linkType: hard -"catched-error-message@npm:^0.0.1": - version: 0.0.1 - resolution: "catched-error-message@npm:0.0.1" - checksum: 10c0/1f10cd4323a73bec7a57b7495730e5dad9995120b04e85b5f654f7b40dc6a36320d95cc55e49cdf78753158e18790ef725e2ec7309ebced3acd6c9a557ac075b - languageName: node - linkType: hard - "chalk@npm:^2.4.2": version: 2.4.2 resolution: "chalk@npm:2.4.2" @@ -2387,6 +2380,13 @@ __metadata: languageName: node linkType: hard +"gha-utils@npm:^0.1.0": + version: 0.1.0 + resolution: "gha-utils@npm:0.1.0" + checksum: 10c0/8c1cc68e89434a8af8a64d71a2dae9a9248ce581b522718ba51f78b5447cad9c16b8d70097a885f797d547bbafdb7bc481d9d41ac1ce0b2db084d500ebfbc838 + languageName: node + linkType: hard + "glob-parent@npm:^5.1.2": version: 5.1.2 resolution: "glob-parent@npm:5.1.2" @@ -4132,8 +4132,8 @@ __metadata: "@rollup/plugin-typescript": "npm:^11.1.6" "@types/jest": "npm:^29.5.12" "@types/node": "npm:^22.1.0" - catched-error-message: "npm:^0.0.1" eslint: "npm:^9.8.0" + gha-utils: "npm:^0.1.0" jest: "npm:^29.7.0" prettier: "npm:^3.3.3" rollup: "npm:^4.20.0"