refactor: reduce read from file code

This commit is contained in:
Jozef Steinhübl 2024-05-07 20:11:52 +02:00
parent 3ef79febc9
commit a0f1a9b3d0
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
2 changed files with 39 additions and 99 deletions

View file

@ -1,110 +1,17 @@
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { join } from "node:path"; import { getInput, setOutput, setFailed, getBooleanInput } from "@actions/core";
import { existsSync, readFileSync } from "node:fs";
import {
getInput,
setOutput,
setFailed,
warning,
getBooleanInput,
} from "@actions/core";
import runAction from "./action.js"; import runAction from "./action.js";
import { readVersionFromFile } from "./utils.js";
if (!process.env.RUNNER_TEMP) { if (!process.env.RUNNER_TEMP) {
process.env.RUNNER_TEMP = tmpdir(); 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(?<version>.*?)$/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({ runAction({
version: getInput("bun-version") || readVersionFromFile() || undefined, version:
getInput("bun-version") ||
readVersionFromFile(getInput("bun-version-file")) ||
undefined,
customUrl: getInput("bun-download-url") || undefined, customUrl: getInput("bun-download-url") || undefined,
registryUrl: getInput("registry-url") || undefined, registryUrl: getInput("registry-url") || undefined,
scope: getInput("scope") || undefined, scope: getInput("scope") || undefined,

View file

@ -1,3 +1,7 @@
import { warning } from "@actions/core";
import { existsSync, readFileSync } from "node:fs";
import { join, basename } from "node:path";
export function retry<T>( export function retry<T>(
fn: () => Promise<T>, fn: () => Promise<T>,
retries: number, retries: number,
@ -12,3 +16,32 @@ export function retry<T>(
); );
}); });
} }
const FILE_VERSION_READERS = {
"package.json": (content: string) =>
JSON.parse(content).packageManager?.split("bun@")?.[1],
".tool-versions": (content: string) =>
content.match(/^bun\s(?<version>.*?)$/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}`);
}
}