Next release of setup-bun

This commit is contained in:
Ashcon Partovi 2023-02-22 17:47:24 -08:00
parent ed9eb0969c
commit 9c14b74b45
1082 changed files with 242557 additions and 173810 deletions

17
dist/action.js vendored Normal file
View file

@ -0,0 +1,17 @@
import { tmpdir } from "node:os";
import * as action from "@actions/core";
import setup from "./setup.js";
if (!process.env.RUNNER_TEMP) {
process.env.RUNNER_TEMP = tmpdir();
}
setup({
version: action.getInput("bun-version") || undefined,
customUrl: action.getInput("bun-download-url") || undefined,
})
.then(({ version, cacheHit }) => {
action.setOutput("bun-version", version);
action.setOutput("cache-hit", cacheHit ? "1" : "0");
})
.catch((error) => {
action.setFailed(error);
});

33
dist/index.js vendored
View file

@ -1,33 +0,0 @@
import { getInput, info, setFailed, setOutput, warning } from '@actions/core';
import getGithubRelease from './utils/getGithubRelease.js';
import install from './utils/install.js';
export const exit = (error, miscTestBuilds) => {
if (miscTestBuilds) {
warning(error);
}
else {
setFailed(error);
process.exit();
}
};
const main = async () => {
try {
const version = getInput('bun-version');
const token = getInput('github-token');
const repository = getInput('repository');
const miscTestBuilds = (getInput('misc-test-builds') === 'true') || (repository.includes('oven-sh/misc-test-builds'));
const customDownloadUrl = getInput('custom-download-url') || null;
if (!version)
return exit('Invalid bun version.');
const release = await getGithubRelease(version, token, repository, customDownloadUrl, miscTestBuilds);
if ((release === null || release === void 0 ? void 0 : release.message) === 'Not Found')
return exit('Invalid bun version.', miscTestBuilds);
info(`Going to install release ${release.version}`);
await install(release, token, customDownloadUrl !== null);
setOutput('bun-version', release.tag_name);
}
catch (e) {
exit(e);
}
};
main();

95
dist/setup.js vendored Normal file
View file

@ -0,0 +1,95 @@
import { homedir } from "node:os";
import { join } from "node:path";
import { readdir } from "node:fs/promises";
import * as action from "@actions/core";
import { downloadTool, extractZip } from "@actions/tool-cache";
import * as cache from "@actions/cache";
import { restoreCache, saveCache } from "@actions/cache";
import { mv } from "@actions/io";
import { getExecOutput } from "@actions/exec";
export default async (options) => {
const { url, cacheKey } = getDownloadUrl(options);
const cacheEnabled = cacheKey && cache.isFeatureAvailable();
const path = join(homedir(), ".bun", "bin", "bun");
let version;
let cacheHit = false;
if (cacheEnabled) {
const cacheRestored = await restoreCache([path], cacheKey);
if (cacheRestored) {
version = await verifyBun(path);
if (version) {
cacheHit = true;
action.info("Using 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."
);
}
}
}
if (!cacheHit) {
action.info(`Downloading a new version of Bun: ${url}`);
const zipPath = await downloadTool(url);
const extractedPath = await extractZip(zipPath);
const exePath = await extractBun(extractedPath);
await mv(exePath, path);
version = await verifyBun(path);
}
if (!version) {
throw new Error(
"Downloaded a new version of Bun, but failed to check its version? Try again in debug mode."
);
}
if (cacheEnabled) {
try {
await saveCache([path], cacheKey);
} catch (error) {
action.warning("Failed to save Bun to cache.");
}
}
return {
version,
cacheHit,
};
};
function getDownloadUrl(options) {
if (options?.customUrl) {
return {
url: options.customUrl,
cacheKey: null,
};
}
const release = options?.version ?? "latest";
const os = options?.os ?? process.platform;
const arch = options?.arch ?? process.arch;
const avx2 = options?.avx2 ?? true;
const profile = options?.profile ?? false;
const { href } = new URL(
`${release}/${os}/${arch}?avx2=${avx2}&profile=${profile}`,
"https://bun.sh/download/"
);
return {
url: href,
cacheKey: /^canary|latest$/i.test(release)
? null
: `bun-${release}-${os}-${arch}-${avx2}-${profile}`,
};
}
async function extractBun(path) {
for (const entry of await readdir(path, { withFileTypes: true })) {
const entryPath = join(path, entry.name);
if (entry.name === "bun" && entry.isFile()) {
return entryPath;
}
if (entry.isDirectory()) {
return extractBun(entryPath);
}
}
throw new Error("Could not find executable: bun");
}
async function verifyBun(path) {
const { exitCode, stdout } = await getExecOutput(path, ["--version"], {
ignoreReturnCode: true,
});
return exitCode === 0 ? stdout.trim() : undefined;
}

View file

@ -1,28 +0,0 @@
import { exit } from '../index.js';
export const getArchitecture = () => {
let arch;
switch (process.arch) {
case 'arm64':
arch = 'aarch64';
break;
case 'x64':
arch = 'x64';
break;
default:
throw new Error(`Unsupported architechture ${process.arch}.`);
}
if (!['linux', 'darwin'].some(platform => process.platform === platform))
throw new Error(`Unsupported platform ${process.platform}.`);
return arch;
};
export default (assets) => {
const arch = getArchitecture();
const assetName = `bun-${process.platform}-${arch}.zip`;
const asset = assets.find(asset => asset.name === assetName);
if (!asset)
exit(`Invalid asset ${assetName}`);
return {
name: `bun-${process.platform}-${arch}`,
asset: assets.find(asset => asset.name === `bun-${process.platform}-${arch}.zip`),
};
};

View file

@ -1,38 +0,0 @@
import { nanoid } from 'nanoid';
import fetch from 'node-fetch';
import { getArchitecture } from './getAsset.js';
export default async (version, token, fullRepository, customDownloadUrl, miscTestBuilds) => {
const repository = miscTestBuilds ? 'oven-sh/misc-test-builds' : fullRepository.split('/').slice(3).join('/');
let url;
if (customDownloadUrl)
url = customDownloadUrl;
else if (version === 'latest' || miscTestBuilds)
url = `https://api.github.com/repos/${repository}/releases/latest`;
else
url = `https://api.github.com/repos/${repository}/releases/tags/${version.includes('canary') ? version : `bun-v${version}`}`;
if (customDownloadUrl) {
return {
name: 'custom',
version: `${version}-${nanoid(10)}`,
html_url: customDownloadUrl,
tag_name: 'custom',
assets: [
{
name: `bun-${process.platform}-${getArchitecture()}.zip`,
browser_download_url: customDownloadUrl
}
]
};
}
const release = await (await fetch(url, {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'setup-bun-github-action',
'Authorization': `token ${token}`
}
})).json();
return {
...release,
version: miscTestBuilds ? `timestamp-v${new Date(release.name).getTime().toString()}` : release.tag_name.replace('bun-v', '')
};
};

View file

@ -1,3 +0,0 @@
export default () => {
return process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'];
};

37
dist/utils/install.js vendored
View file

@ -1,37 +0,0 @@
import { cacheDir, downloadTool, extractZip, find } from '@actions/tool-cache';
import { restoreCache, saveCache } from '@actions/cache';
import { addPath, info } from '@actions/core';
import getAsset from './getAsset.js';
import { join } from 'path';
import { homedir } from 'os';
export default async (release, token, customUrl) => {
const asset = getAsset(release.assets);
const path = join(homedir(), '.bun', 'bin', asset.name);
const cache = find('bun', release.version) || await restoreCache([path], `bun-${process.platform}-${asset.name}-${release.version}`);
if (cache) {
info(`Using cached Bun installation from ${cache}.`);
addPath(path);
return;
}
info(`Downloading Bun from ${asset.asset.browser_download_url}.`);
const zipPath = await downloadTool(asset.asset.browser_download_url, null, new URL(asset.asset.browser_download_url).host.includes('github.com') ? `token ${token}` : '', {
'Authorization': new URL(asset.asset.browser_download_url).host.includes('github.com') ? `token ${token}` : ''
});
let extracted;
if (customUrl && asset.asset.browser_download_url.includes('artifacts')) {
extracted = await extractZip(zipPath, join(homedir(), 'onlyforunzip'));
extracted = await extractZip(join(homedir(), 'onlyforunzip', asset.asset.name), join(homedir(), '.bun', 'bin'));
}
else
extracted = await extractZip(zipPath, join(homedir(), '.bun', 'bin'));
const newCache = await cacheDir(extracted, 'bun', release.version);
if (!customUrl) {
await saveCache([
join(extracted, asset.name)
], `bun-${process.platform}-${asset.name}-${release.version}`);
}
info(`Cached Bun to ${newCache}.`);
addPath(newCache);
const bunPath = join(homedir(), '.bun', 'bin', asset.name);
addPath(bunPath);
};