mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-18 04:28:28 +02:00
Fix parsing of boolean
This commit is contained in:
parent
5a4722c39d
commit
9b3770f97f
3 changed files with 99 additions and 65 deletions
9
dist/action.js
vendored
9
dist/action.js
vendored
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
144
src/setup.ts
144
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<Result> {
|
||||
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<string> {
|
||||
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<string> {
|
||||
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<string | undefined> {
|
||||
async function getVersion(path: string): Promise<string | undefined> {
|
||||
const { exitCode, stdout } = await getExecOutput(path, ["--version"], {
|
||||
ignoreReturnCode: true,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue