feat: get download url from github's api

This commit is contained in:
Jozef Steinhübl 2024-07-29 20:30:19 +02:00
parent 99d7a7aeb9
commit 3ed283caaf
No known key found for this signature in database
GPG Key ID: E6BC90C91973B08F
7 changed files with 271 additions and 127 deletions

BIN
bun.lockb

Binary file not shown.

198
dist/cache-save/index.js generated vendored

File diff suppressed because one or more lines are too long

69
dist/setup/index.js generated vendored

File diff suppressed because one or more lines are too long

View File

@ -3,13 +3,7 @@
"name": "setup-bun",
"version": "2.0.1",
"description": "Setup Bun on GitHub Actions.",
"keywords": [
"bun",
"bun.js",
"github",
"actions",
"setup"
],
"keywords": ["bun", "bun.js", "github", "actions", "setup"],
"homepage": "https://bun.sh/",
"main": "dist/index.js",
"repository": "git@github.com:oven-sh/setup-bun.git",
@ -27,7 +21,8 @@
"@actions/exec": "^1.1.1",
"@actions/glob": "^0.4.0",
"@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1"
"@actions/tool-cache": "^2.0.1",
"compare-versions": "^6.1.1"
},
"devDependencies": {
"@types/node": "^20.8.2",
@ -37,5 +32,8 @@
},
"prettier": {
"quoteProps": "preserve"
},
"patchedDependencies": {
"compare-versions@6.1.1": "patches/compare-versions@6.1.1.patch"
}
}

View File

@ -0,0 +1,67 @@
diff --git a/lib/esm/satisfies.js b/lib/esm/satisfies.js
index 7586b71657332f855431c4dd4f05e9394fd9aac3..a6ec29bfc98907c67ed4af71fca73bd8bff88798 100644
--- a/lib/esm/satisfies.js
+++ b/lib/esm/satisfies.js
@@ -40,8 +40,9 @@ export const satisfies = (version, range) => {
// else range of either "~" or "^" is assumed
const [v1, v2, v3, , vp] = validateAndParse(version);
const [r1, r2, r3, , rp] = validateAndParse(range);
- const v = [v1, v2, v3];
+ const v = [v1, v2 !== null && v2 !== void 0 ? v2 : 'x', v3 !== null && v3 !== void 0 ? v3 : 'x'];
const r = [r1, r2 !== null && r2 !== void 0 ? r2 : 'x', r3 !== null && r3 !== void 0 ? r3 : 'x'];
+
// validate pre-release
if (rp) {
if (!vp)
diff --git a/lib/esm/utils.js b/lib/esm/utils.js
index b5cc8b9927ab38fc67032c133b531e95ec4cec15..ec56105fd2d806aa922f1488a27b02c56aff1865 100644
--- a/lib/esm/utils.js
+++ b/lib/esm/utils.js
@@ -28,7 +28,7 @@ const compareStrings = (a, b) => {
};
export const compareSegments = (a, b) => {
for (let i = 0; i < Math.max(a.length, b.length); i++) {
- const r = compareStrings(a[i] || '0', b[i] || '0');
+ const r = compareStrings(a[i] || 'x', b[i] || 'x');
if (r !== 0)
return r;
}
diff --git a/lib/umd/index.js b/lib/umd/index.js
index 2cfef261bca520e21ed41fc14950732b8aa6339b..1059784db86635f3aaaba83b5a72c5015e1d8490 100644
--- a/lib/umd/index.js
+++ b/lib/umd/index.js
@@ -152,7 +152,7 @@
// else range of either "~" or "^" is assumed
const [v1, v2, v3, , vp] = validateAndParse(version);
const [r1, r2, r3, , rp] = validateAndParse(range);
- const v = [v1, v2, v3];
+ const v = [v1, v2 !== null && v2 !== void 0 ? v2 : 'x', v3 !== null && v3 !== void 0 ? v3 : 'x'];
const r = [r1, r2 !== null && r2 !== void 0 ? r2 : 'x', r3 !== null && r3 !== void 0 ? r3 : 'x'];
// validate pre-release
if (rp) {
diff --git a/package.json b/package.json
index b05b3daf706d7ba4e594233f8791fc3007a8e2cd..e51e76b86f95e9ebf0b5dba3b82aeb119628528d 100644
--- a/package.json
+++ b/package.json
@@ -26,7 +26,7 @@
"prepublishOnly": "npm run build",
"test": "c8 --reporter=lcov mocha"
},
- "main": "./lib/umd/index.js",
+ "main": "./lib/src/index.ts",
"module": "./lib/esm/index.js",
"types": "./lib/esm/index.d.ts",
"sideEffects": false,
diff --git a/src/satisfies.ts b/src/satisfies.ts
index 66cb171d7f32e68fdda6929d2da223b97a053737..6b4973f037843f264338a01efdc4ace5dcf042cd 100644
--- a/src/satisfies.ts
+++ b/src/satisfies.ts
@@ -43,7 +43,7 @@ export const satisfies = (version: string, range: string): boolean => {
// else range of either "~" or "^" is assumed
const [v1, v2, v3, , vp] = validateAndParse(version);
const [r1, r2, r3, , rp] = validateAndParse(range);
- const v = [v1, v2, v3];
+ const v = [v1, v2 ?? 'x', v3 ?? 'x'];
const r = [r1, r2 ?? 'x', r3 ?? 'x'];
// validate pre-release

View File

@ -13,7 +13,8 @@ import { downloadTool, extractZip } from "@actions/tool-cache";
import { getExecOutput } from "@actions/exec";
import { writeBunfig } from "./bunfig";
import { saveState } from "@actions/core";
import { addExtension } from "./utils";
import { addExtension, request } from "./utils";
import { compareVersions, satisfies, validate } from "compare-versions";
export type Input = {
customUrl?: string;
@ -46,7 +47,7 @@ export default async (options: Input): Promise<Output> => {
const bunfigPath = join(process.cwd(), "bunfig.toml");
writeBunfig(bunfigPath, options);
const url = getDownloadUrl(options);
const url = await getDownloadUrl(options);
const cacheEnabled = isCacheEnabled(options);
const binPath = join(homedir(), ".bun", "bin");
@ -151,21 +152,43 @@ function isCacheEnabled(options: Input): boolean {
return isFeatureAvailable();
}
function getDownloadUrl(options: Input): string {
async function getDownloadUrl(options: Input): Promise<string> {
const { customUrl } = options;
if (customUrl) {
return customUrl;
}
const res = (await (
await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags")
).json()) as { ref: string }[];
let tags = res
.filter(
(tag) =>
tag.ref.startsWith("refs/tags/bun-v") || tag.ref === "refs/tags/canary"
)
.map((item) => item.ref.replace(/refs\/tags\/(bun-)?/g, ""));
const { version, os, arch, avx2, profile } = options;
const eversion = encodeURIComponent(version ?? "latest");
let tag = tags.find((t) => t === version);
if (!tag) {
tags = tags.filter((t) => validate(t)).sort(compareVersions);
if (version === "latest") tag = tags.at(-1);
else tag = tags.filter((t) => satisfies(version, t)).at(-1);
}
const eversion = encodeURIComponent(tag);
const eos = encodeURIComponent(os ?? process.platform);
const earch = encodeURIComponent(arch ?? process.arch);
const eavx2 = encodeURIComponent(avx2 ?? true);
const eprofile = encodeURIComponent(profile ?? false);
const eavx2 = encodeURIComponent(avx2 ? "-baseline" : "");
const eprofile = encodeURIComponent(profile ? "-profile" : "");
const { href } = new URL(
`${eversion}/${eos}/${earch}?avx2=${eavx2}&profile=${eprofile}`,
"https://bun.sh/download/"
`bun-${eversion}/bun-${eos}-${earch}${eavx2}${eprofile}.zip`,
"https://github.com/oven-sh/bun/releases/download/"
);
return href;
}

View File

@ -3,6 +3,17 @@ import { info } from "node:console";
import { existsSync, readFileSync, renameSync } from "node:fs";
import { join, basename } from "node:path";
export async function request(url: string): Promise<Response> {
const res = await fetch(url);
if (!res.ok) {
throw new Error(
`Failed to fetch url ${url}. (status code: ${res.status}, status text: ${res.statusText})\n${res}`
);
}
return res;
}
export function addExtension(path: string, ext: string): string {
if (!path.endsWith(ext)) {
renameSync(path, path + ext);