From 9b3770f97f657b1a695a8ccee17f2eddd17700e8 Mon Sep 17 00:00:00 2001 From: Ashcon Partovi Date: Sun, 30 Apr 2023 09:46:10 -0700 Subject: [PATCH] Fix parsing of boolean --- dist/action.js | 9 ++-- src/action.ts | 11 +++- src/setup.ts | 144 ++++++++++++++++++++++++++++--------------------- 3 files changed, 99 insertions(+), 65 deletions(-) diff --git a/dist/action.js b/dist/action.js index 06ea3b6..fa8346c 100644 --- a/dist/action.js +++ b/dist/action.js @@ -51868,16 +51868,19 @@ async function setup_default(options) { } // src/action.ts +var parseBoolean = function(value) { + return /^true$/i.test(value); +}; if (!process.env.RUNNER_TEMP) process.env.RUNNER_TEMP = tmpdir(); setup_default({ customUrl: action2.getInput("bun-download-url") || undefined, - checkLatest: Boolean(action2.getInput("check-latest")), + checkLatest: parseBoolean(action2.getInput("check-latest")), version: action2.getInput("bun-version") || "latest", os: process.platform, arch: process.arch, - baseline: Boolean(action2.getInput("baseline")), - profile: Boolean(action2.getInput("profile")) + baseline: parseBoolean(action2.getInput("baseline")), + profile: parseBoolean(action2.getInput("profile")) }).then(({ version: version3, cacheHit }) => { action2.setOutput("bun-version", version3); action2.setOutput("cache-hit", cacheHit); diff --git a/src/action.ts b/src/action.ts index 8164cc9..27508bf 100644 --- a/src/action.ts +++ b/src/action.ts @@ -6,9 +6,18 @@ if (!process.env.RUNNER_TEMP) { process.env.RUNNER_TEMP = tmpdir(); } +function parseBoolean(value: string): boolean { + return /^true$/i.test(value); +} + setup({ - version: action.getInput("bun-version") || undefined, customUrl: action.getInput("bun-download-url") || undefined, + checkLatest: parseBoolean(action.getInput("check-latest")), + version: action.getInput("bun-version") || "latest", + os: process.platform, + arch: process.arch, + baseline: parseBoolean(action.getInput("baseline")), + profile: parseBoolean(action.getInput("profile")), }) .then(({ version, cacheHit }) => { action.setOutput("bun-version", version); diff --git a/src/setup.ts b/src/setup.ts index 2880c77..0a77cb7 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -1,6 +1,6 @@ import { homedir } from "node:os"; import { join } from "node:path"; -import { readdir } from "node:fs/promises"; +import { readdir, symlink } from "node:fs/promises"; import * as action from "@actions/core"; import { downloadTool, extractZip } from "@actions/tool-cache"; import * as cache from "@actions/cache"; @@ -8,27 +8,38 @@ import { restoreCache, saveCache } from "@actions/cache"; import { mv } from "@actions/io"; import { getExecOutput } from "@actions/exec"; -export default async (options?: { - version?: string; +type Options = { customUrl?: string; -}): Promise<{ + checkLatest?: boolean; + version: string; + os: string; + arch: string; + baseline: boolean; + profile: boolean; +}; + +type Result = { version: string; cacheHit: boolean; -}> => { - const { url, cacheKey } = getDownloadUrl(options); - const cacheEnabled = cacheKey && cache.isFeatureAvailable(); - const dir = join(homedir(), ".bun", "bin"); - action.addPath(dir); - const path = join(dir, "bun"); +}; + +export default async function (options: Options): Promise { + const url = getDownloadUrl(options); + const cacheKey = getCacheKey(options); + const cacheEnabled = cache.isFeatureAvailable(); + const path = join(homedir(), ".bun", "bin"); + action.addPath(path); + const bunPath = join(path, "bun"); + const bunxPath = join(path, "bunx"); let version: string | undefined; let cacheHit = false; - if (cacheEnabled) { - const cacheRestored = await restoreCache([path], cacheKey); + if (!options.checkLatest && cacheEnabled) { + const cacheRestored = await restoreCache([bunPath], cacheKey); if (cacheRestored) { - version = await verifyBun(path); + version = await getVersion(bunPath); if (version) { cacheHit = true; - action.info("Using a cached version of Bun."); + action.info("Found a cached version of Bun."); } else { action.warning( "Found a cached version of Bun, but it appears to be corrupted? Attempting to download a new version." @@ -38,20 +49,27 @@ export default async (options?: { } if (!cacheHit) { action.info(`Downloading a new version of Bun: ${url}`); - const zipPath = await downloadTool(url); + const zipPath = await downloadTool(url.toString()); const extractedPath = await extractZip(zipPath); - const exePath = await extractBun(extractedPath); - await mv(exePath, path); - version = await verifyBun(path); + const exePath = await extractFileFromZip(extractedPath, "bun"); + await mv(exePath, bunPath); + version = await getVersion(bunPath); } if (!version) { throw new Error( "Downloaded a new version of Bun, but failed to check its version? Try again in debug mode." ); } + try { + await symlink(bunPath, bunxPath); + } catch (error) { + if (error.code !== "EEXIST") { + throw error; + } + } if (cacheEnabled) { try { - await saveCache([path], cacheKey); + await saveCache([bunPath], cacheKey); } catch (error) { action.warning("Failed to save Bun to cache."); } @@ -60,63 +78,67 @@ export default async (options?: { version, cacheHit, }; -}; - -function getDownloadUrl(options?: { - customUrl?: string; - version?: string; - os?: string; - arch?: string; - avx2?: boolean; - profile?: boolean; -}): { - url: string; - cacheKey: string | null; -} { - if (options?.customUrl) { - return { - url: options.customUrl, - cacheKey: null, - }; - } - const release = encodeURIComponent(options?.version ?? "latest"); - const os = encodeURIComponent(options?.os ?? process.platform); - const arch = encodeURIComponent(options?.arch ?? process.arch); - const avx2 = encodeURIComponent(options?.avx2 ?? true); - const profile = encodeURIComponent(options?.profile ?? false); - const { href } = new URL( - `${release}/${os}/${arch}?avx2=${avx2}&profile=${profile}`, - "https://bun.sh/download/" - ); - return { - url: href, - cacheKey: /^latest|canary|action/i.test(release) - ? null - : `bun-${release}-${os}-${arch}-${avx2}-${profile}`, - }; } -async function extractBun(path: string): Promise { +function getCacheKey(options: Options): string { + const { customUrl } = options; + if (customUrl) { + return customUrl; + } + const { os, arch, baseline, profile, version } = options; + let name = `bun-${os}-${arch}`; + if (baseline) { + name += "-baseline"; + } + if (profile) { + name += "-profile"; + } + return `${name}@${version}`; +} + +function getDownloadUrl(options: Options): URL { + const { customUrl } = options; + if (customUrl) { + return new URL(customUrl); + } + const { version, os, arch, baseline, profile } = options; + const url = new URL( + `${encodeURIComponent(version)}/${os}/${arch}/bun.zip`, + "https://bun.sh/download/" + ); + if (baseline) { + url.searchParams.set("baseline", "true"); + } + if (profile) { + url.searchParams.set("profile", "true"); + } + return url; +} + +async function extractFileFromZip( + path: string, + filename: string +): Promise { for (const entry of await readdir(path, { withFileTypes: true })) { const { name } = entry; const entryPath = join(path, name); if (entry.isFile()) { - if (name === "bun") { + if (name === filename) { return entryPath; } - if (/^bun.*\.zip/.test(name)) { + if (name.includes(filename) && name.endsWith(".zip")) { const extractedPath = await extractZip(entryPath); - return extractBun(extractedPath); + return extractFileFromZip(extractedPath, filename); } } - if (/^bun/.test(name) && entry.isDirectory()) { - return extractBun(entryPath); + if (name.includes(filename) && entry.isDirectory()) { + return extractFileFromZip(entryPath, filename); } } - throw new Error("Could not find executable: bun"); + throw new Error(`Failed to extract '${filename}' from '${path}'.`); } -async function verifyBun(path: string): Promise { +async function getVersion(path: string): Promise { const { exitCode, stdout } = await getExecOutput(path, ["--version"], { ignoreReturnCode: true, });