refactor: cleanup

This commit is contained in:
Jozef Steinhübl 2026-01-04 23:58:17 +01:00
parent cafd14a538
commit 0df7a9b56c
No known key found for this signature in database
GPG Key ID: 8D9A5E4549AA84FD
3 changed files with 20 additions and 27 deletions

View File

@ -17,7 +17,7 @@ import { Registry } from "./registry";
import { writeBunfig } from "./bunfig"; import { writeBunfig } from "./bunfig";
import { saveState } from "@actions/core"; import { saveState } from "@actions/core";
import { addExtension } from "./utils"; import { addExtension } from "./utils";
import { DownloadMeta, getDownloadMeta } from "./download-url"; import { getDownloadUrl } from "./download-url";
import { cwd } from "node:process"; import { cwd } from "node:process";
export type Input = { export type Input = {
@ -29,7 +29,7 @@ export type Input = {
profile?: boolean; profile?: boolean;
registries?: Registry[]; registries?: Registry[];
noCache?: boolean; noCache?: boolean;
token: string; token?: string;
}; };
export type Output = { export type Output = {
@ -51,8 +51,7 @@ export default async (options: Input): Promise<Output> => {
const bunfigPath = join(cwd(), "bunfig.toml"); const bunfigPath = join(cwd(), "bunfig.toml");
writeBunfig(bunfigPath, options.registries); writeBunfig(bunfigPath, options.registries);
const downloadMeta = await getDownloadMeta(options); const url = await getDownloadUrl(options);
const url = downloadMeta.url;
const cacheEnabled = isCacheEnabled(options); const cacheEnabled = isCacheEnabled(options);
@ -110,7 +109,7 @@ export default async (options: Input): Promise<Output> => {
if (!cacheHit) { if (!cacheHit) {
info(`Downloading a new version of Bun: ${url}`); info(`Downloading a new version of Bun: ${url}`);
revision = await downloadBun(downloadMeta, bunPath); revision = await downloadBun(url, bunPath);
} }
} }
@ -164,11 +163,11 @@ function isVersionMatch(
} }
async function downloadBun( async function downloadBun(
downloadMeta: DownloadMeta, url: string,
bunPath: string, bunPath: string,
): Promise<string | undefined> { ): Promise<string | undefined> {
// Workaround for https://github.com/oven-sh/setup-bun/issues/79 and https://github.com/actions/toolkit/issues/1179 // Workaround for https://github.com/oven-sh/setup-bun/issues/79 and https://github.com/actions/toolkit/issues/1179
const zipPath = addExtension(await downloadTool(downloadMeta.url), ".zip"); const zipPath = addExtension(await downloadTool(url), ".zip");
const extractedZipPath = await extractZip(zipPath); const extractedZipPath = await extractZip(zipPath);
const extractedBunPath = await extractBun(extractedZipPath); const extractedBunPath = await extractBun(extractedZipPath);
try { try {

View File

@ -2,28 +2,16 @@ import { compareVersions, satisfies, validate } from "compare-versions";
import { Input } from "./action"; import { Input } from "./action";
import { getArchitecture, getPlatform, request } from "./utils"; import { getArchitecture, getPlatform, request } from "./utils";
export interface DownloadMeta { export async function getDownloadUrl(options: Input): Promise<string> {
url: string;
auth?: string;
}
export async function getDownloadMeta(options: Input): Promise<DownloadMeta> {
const { customUrl } = options; const { customUrl } = options;
if (customUrl) { if (customUrl) {
return { return customUrl;
url: customUrl,
};
} }
return await getSemverDownloadMeta(options); return await getSemverDownloadUrl(options);
} }
interface Run { async function getSemverDownloadUrl(options: Input): Promise<string> {
id: string;
head_sha: string;
}
async function getSemverDownloadMeta(options: Input): Promise<DownloadMeta> {
const res = (await ( const res = (await (
await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags", { await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags", {
headers: options.token headers: options.token
@ -71,7 +59,5 @@ async function getSemverDownloadMeta(options: Input): Promise<DownloadMeta> {
"https://github.com/oven-sh/bun/releases/download/", "https://github.com/oven-sh/bun/releases/download/",
); );
return { return href;
url: href,
};
} }

View File

@ -7,7 +7,15 @@ export async function request(
url: string, url: string,
init?: RequestInit, init?: RequestInit,
): Promise<Response> { ): Promise<Response> {
const res = await fetch(url, init); const headers = new Headers(init?.headers);
if (!headers.has("User-Agent")) {
headers.set("User-Agent", "@oven-sh/setup-bun");
}
const res = await fetch(url, {
...init,
headers,
});
if (!res.ok) { if (!res.ok) {
const body = await res.text().catch(() => ""); const body = await res.text().catch(() => "");
throw new Error( throw new Error(