From a0f1a9b3d05ffcb0a45acfb825c2852ca0f4f06b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 7 May 2024 20:11:52 +0200 Subject: [PATCH] refactor: reduce read from file code --- src/index.ts | 105 +++------------------------------------------------ src/utils.ts | 33 ++++++++++++++++ 2 files changed, 39 insertions(+), 99 deletions(-) diff --git a/src/index.ts b/src/index.ts index f3ab68c..b2d87db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,110 +1,17 @@ import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { existsSync, readFileSync } from "node:fs"; -import { - getInput, - setOutput, - setFailed, - warning, - getBooleanInput, -} from "@actions/core"; +import { getInput, setOutput, setFailed, getBooleanInput } from "@actions/core"; import runAction from "./action.js"; +import { readVersionFromFile } from "./utils.js"; if (!process.env.RUNNER_TEMP) { process.env.RUNNER_TEMP = tmpdir(); } -function readVersionFromPackageJson(file: string): string | undefined { - const cwd = process.env.GITHUB_WORKSPACE; - if (!cwd) { - return; - } - const path = join(cwd, file); - try { - if (!existsSync(path)) { - return; - } - const { packageManager } = JSON.parse(readFileSync(path, "utf8")); - if (!packageManager?.includes("bun@")) { - return; - } - const [_, version] = packageManager.split("bun@"); - return version; - } catch (error) { - const { message } = error as Error; - warning(`Failed to read ${file}: ${message}`); - } -} - -function readVersionFromToolVersions(file: string): string | undefined { - const cwd = process.env.GITHUB_WORKSPACE; - if (!cwd) { - return; - } - const path = join(cwd, file); - try { - if (!existsSync(path)) { - return; - } - - const match = readFileSync(path, "utf8").match(/^bun\s(?.*?)$/m); - - return match?.groups?.version; - } catch (error) { - const { message } = error as Error; - warning(`Failed to read ${file}: ${message}`); - } -} - -function readVersionFromBumrc(file: string): string | undefined { - const cwd = process.env.GITHUB_WORKSPACE; - if (!cwd) { - return; - } - const path = join(cwd, file); - try { - if (!existsSync(path)) { - return; - } - - const match = readFileSync(path, "utf8"); - - return JSON.parse(match); - } catch (error) { - const { message } = error as Error; - warning(`Failed to read ${file}: ${message}`); - } -} - -function readVersionFromFile(): string | undefined { - const cwd = process.env.GITHUB_WORKSPACE; - if (!cwd) { - return; - } - const file = getInput("bun-version-file"); - const path = join(cwd, file); - try { - if (!existsSync(path)) { - return; - } - - if (file === "package.json") { - return readVersionFromPackageJson(file); - } else if (file === ".tool-versions") { - return readVersionFromToolVersions(file); - } else if (file === ".bumrc") { - return readVersionFromBumrc(file); - } else { - warning(`Not allowed read version from ${file}`); - } - } catch (error) { - const { message } = error as Error; - warning(`Failed to read ${file}: ${message}`); - } -} - runAction({ - version: getInput("bun-version") || readVersionFromFile() || undefined, + version: + getInput("bun-version") || + readVersionFromFile(getInput("bun-version-file")) || + undefined, customUrl: getInput("bun-download-url") || undefined, registryUrl: getInput("registry-url") || undefined, scope: getInput("scope") || undefined, diff --git a/src/utils.ts b/src/utils.ts index 1023a89..0a385da 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,7 @@ +import { warning } from "@actions/core"; +import { existsSync, readFileSync } from "node:fs"; +import { join, basename } from "node:path"; + export function retry( fn: () => Promise, retries: number, @@ -12,3 +16,32 @@ export function retry( ); }); } + +const FILE_VERSION_READERS = { + "package.json": (content: string) => + JSON.parse(content).packageManager?.split("bun@")?.[1], + ".tool-versions": (content: string) => + content.match(/^bun\s(?.*?)$/m)?.groups?.version, + ".bumrc": (content: string) => content, +}; + +export function readVersionFromFile(file: string): string | undefined { + const cwd = process.env.GITHUB_WORKSPACE; + if (!cwd) { + return; + } + + const path = join(cwd, file); + const base = basename(file); + + if (!existsSync(path)) return; + + const reader = FILE_VERSION_READERS[base] ?? (() => undefined); + + try { + return reader(readFileSync(path, "utf8")); + } catch (error) { + const { message } = error as Error; + warning(`Failed to read ${file}: ${message}`); + } +}