Fix parsing of boolean

This commit is contained in:
Ashcon Partovi 2023-04-30 09:46:10 -07:00
parent 5a4722c39d
commit 9b3770f97f
3 changed files with 99 additions and 65 deletions

9
dist/action.js vendored
View file

@ -51868,16 +51868,19 @@ async function setup_default(options) {
} }
// src/action.ts // src/action.ts
var parseBoolean = function(value) {
return /^true$/i.test(value);
};
if (!process.env.RUNNER_TEMP) if (!process.env.RUNNER_TEMP)
process.env.RUNNER_TEMP = tmpdir(); process.env.RUNNER_TEMP = tmpdir();
setup_default({ setup_default({
customUrl: action2.getInput("bun-download-url") || undefined, 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", version: action2.getInput("bun-version") || "latest",
os: process.platform, os: process.platform,
arch: process.arch, arch: process.arch,
baseline: Boolean(action2.getInput("baseline")), baseline: parseBoolean(action2.getInput("baseline")),
profile: Boolean(action2.getInput("profile")) profile: parseBoolean(action2.getInput("profile"))
}).then(({ version: version3, cacheHit }) => { }).then(({ version: version3, cacheHit }) => {
action2.setOutput("bun-version", version3); action2.setOutput("bun-version", version3);
action2.setOutput("cache-hit", cacheHit); action2.setOutput("cache-hit", cacheHit);

View file

@ -6,9 +6,18 @@ if (!process.env.RUNNER_TEMP) {
process.env.RUNNER_TEMP = tmpdir(); process.env.RUNNER_TEMP = tmpdir();
} }
function parseBoolean(value: string): boolean {
return /^true$/i.test(value);
}
setup({ setup({
version: action.getInput("bun-version") || undefined,
customUrl: action.getInput("bun-download-url") || 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 }) => { .then(({ version, cacheHit }) => {
action.setOutput("bun-version", version); action.setOutput("bun-version", version);

View file

@ -1,6 +1,6 @@
import { homedir } from "node:os"; import { homedir } from "node:os";
import { join } from "node:path"; 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 * as action from "@actions/core";
import { downloadTool, extractZip } from "@actions/tool-cache"; import { downloadTool, extractZip } from "@actions/tool-cache";
import * as cache from "@actions/cache"; import * as cache from "@actions/cache";
@ -8,27 +8,38 @@ import { restoreCache, saveCache } from "@actions/cache";
import { mv } from "@actions/io"; import { mv } from "@actions/io";
import { getExecOutput } from "@actions/exec"; import { getExecOutput } from "@actions/exec";
export default async (options?: { type Options = {
version?: string;
customUrl?: string; customUrl?: string;
}): Promise<{ checkLatest?: boolean;
version: string;
os: string;
arch: string;
baseline: boolean;
profile: boolean;
};
type Result = {
version: string; version: string;
cacheHit: boolean; cacheHit: boolean;
}> => { };
const { url, cacheKey } = getDownloadUrl(options);
const cacheEnabled = cacheKey && cache.isFeatureAvailable(); export default async function (options: Options): Promise<Result> {
const dir = join(homedir(), ".bun", "bin"); const url = getDownloadUrl(options);
action.addPath(dir); const cacheKey = getCacheKey(options);
const path = join(dir, "bun"); 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 version: string | undefined;
let cacheHit = false; let cacheHit = false;
if (cacheEnabled) { if (!options.checkLatest && cacheEnabled) {
const cacheRestored = await restoreCache([path], cacheKey); const cacheRestored = await restoreCache([bunPath], cacheKey);
if (cacheRestored) { if (cacheRestored) {
version = await verifyBun(path); version = await getVersion(bunPath);
if (version) { if (version) {
cacheHit = true; cacheHit = true;
action.info("Using a cached version of Bun."); action.info("Found a cached version of Bun.");
} else { } else {
action.warning( action.warning(
"Found a cached version of Bun, but it appears to be corrupted? Attempting to download a new version." "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) { if (!cacheHit) {
action.info(`Downloading a new version of Bun: ${url}`); 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 extractedPath = await extractZip(zipPath);
const exePath = await extractBun(extractedPath); const exePath = await extractFileFromZip(extractedPath, "bun");
await mv(exePath, path); await mv(exePath, bunPath);
version = await verifyBun(path); version = await getVersion(bunPath);
} }
if (!version) { if (!version) {
throw new Error( throw new Error(
"Downloaded a new version of Bun, but failed to check its version? Try again in debug mode." "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) { if (cacheEnabled) {
try { try {
await saveCache([path], cacheKey); await saveCache([bunPath], cacheKey);
} catch (error) { } catch (error) {
action.warning("Failed to save Bun to cache."); action.warning("Failed to save Bun to cache.");
} }
@ -60,63 +78,67 @@ export default async (options?: {
version, version,
cacheHit, 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); function getCacheKey(options: Options): string {
const arch = encodeURIComponent(options?.arch ?? process.arch); const { customUrl } = options;
const avx2 = encodeURIComponent(options?.avx2 ?? true); if (customUrl) {
const profile = encodeURIComponent(options?.profile ?? false); return customUrl;
const { href } = new URL( }
`${release}/${os}/${arch}?avx2=${avx2}&profile=${profile}`, 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/" "https://bun.sh/download/"
); );
return { if (baseline) {
url: href, url.searchParams.set("baseline", "true");
cacheKey: /^latest|canary|action/i.test(release) }
? null if (profile) {
: `bun-${release}-${os}-${arch}-${avx2}-${profile}`, url.searchParams.set("profile", "true");
}; }
return url;
} }
async function extractBun(path: string): Promise<string> { async function extractFileFromZip(
path: string,
filename: string
): Promise<string> {
for (const entry of await readdir(path, { withFileTypes: true })) { for (const entry of await readdir(path, { withFileTypes: true })) {
const { name } = entry; const { name } = entry;
const entryPath = join(path, name); const entryPath = join(path, name);
if (entry.isFile()) { if (entry.isFile()) {
if (name === "bun") { if (name === filename) {
return entryPath; return entryPath;
} }
if (/^bun.*\.zip/.test(name)) { if (name.includes(filename) && name.endsWith(".zip")) {
const extractedPath = await extractZip(entryPath); const extractedPath = await extractZip(entryPath);
return extractBun(extractedPath); return extractFileFromZip(extractedPath, filename);
} }
} }
if (/^bun/.test(name) && entry.isDirectory()) { if (name.includes(filename) && entry.isDirectory()) {
return extractBun(entryPath); 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<string | undefined> { async function getVersion(path: string): Promise<string | undefined> {
const { exitCode, stdout } = await getExecOutput(path, ["--version"], { const { exitCode, stdout } = await getExecOutput(path, ["--version"], {
ignoreReturnCode: true, ignoreReturnCode: true,
}); });