mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-18 04:28:28 +02:00
refactor: reduce read from file code
This commit is contained in:
parent
3ef79febc9
commit
a0f1a9b3d0
2 changed files with 39 additions and 99 deletions
105
src/index.ts
105
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(?<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({
|
||||
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,
|
||||
|
|
33
src/utils.ts
33
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<T>(
|
||||
fn: () => Promise<T>,
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue