diff options
Diffstat (limited to 'packages/bun-release/src')
-rw-r--r-- | packages/bun-release/src/console.ts | 8 | ||||
-rw-r--r-- | packages/bun-release/src/fetch.ts | 9 | ||||
-rw-r--r-- | packages/bun-release/src/fs.ts | 11 | ||||
-rw-r--r-- | packages/bun-release/src/github.ts | 45 | ||||
-rw-r--r-- | packages/bun-release/src/npm/install.ts | 23 | ||||
-rw-r--r-- | packages/bun-release/src/platform.ts | 19 |
6 files changed, 35 insertions, 80 deletions
diff --git a/packages/bun-release/src/console.ts b/packages/bun-release/src/console.ts index 69e0911d6..5c9db8872 100644 --- a/packages/bun-release/src/console.ts +++ b/packages/bun-release/src/console.ts @@ -4,9 +4,7 @@ import { createInterface } from "readline"; export const isAction = !!process.env["GITHUB_ACTION"]; export const isDebug = - process.env["DEBUG"] === "1" || - process.env["LOG_LEVEL"] === "debug" || - process.env["RUNNER_DEBUG"] === "1"; + process.env["DEBUG"] === "1" || process.env["LOG_LEVEL"] === "debug" || process.env["RUNNER_DEBUG"] === "1"; export function debug(...message: any[]): void { if (isAction) { @@ -54,10 +52,10 @@ export async function stdin(question: string): Promise<string> { terminal: false, }); let buffer = ""; - reader.on("line", (line) => { + reader.on("line", line => { buffer += line; }); - return new Promise((resolve) => { + return new Promise(resolve => { reader.once("close", () => resolve(buffer)); }); } diff --git a/packages/bun-release/src/fetch.ts b/packages/bun-release/src/fetch.ts index d9c85a5f3..ce385ffd3 100644 --- a/packages/bun-release/src/fetch.ts +++ b/packages/bun-release/src/fetch.ts @@ -17,13 +17,10 @@ async function webFetch(url: string, options: Options = {}): Promise<Response> { return response; } -async function nodeFetch( - url: string, - options: Options = {}, -): Promise<Response> { +async function nodeFetch(url: string, options: Options = {}): Promise<Response> { const { get } = await import("node:http"); return new Promise((resolve, reject) => { - get(url, (response) => { + get(url, response => { debug("http.get", url, response.statusCode); const status = response.statusCode ?? 501; if (response.headers.location && isRedirect(status)) { @@ -33,7 +30,7 @@ async function nodeFetch( return reject(new Error(`${status}: ${url}`)); } const body: Buffer[] = []; - response.on("data", (chunk) => { + response.on("data", chunk => { body.push(chunk); }); response.on("end", () => { diff --git a/packages/bun-release/src/fs.ts b/packages/bun-release/src/fs.ts index ef20c224a..34b1c4076 100644 --- a/packages/bun-release/src/fs.ts +++ b/packages/bun-release/src/fs.ts @@ -73,10 +73,7 @@ export function rename(path: string, newPath: string): void { fs.renameSync(path, newPath); } -export function write( - dst: string, - content: string | ArrayBuffer | ArrayBufferView, -): void { +export function write(dst: string, content: string | ArrayBuffer | ArrayBufferView): void { debug("write", dst); try { fs.writeFileSync(dst, content); @@ -123,11 +120,7 @@ export function blob(path: string): Blob { } const buffer = fs.readFileSync(path); return new Blob([buffer], { - type: path.endsWith(".zip") - ? "application/zip" - : path.endsWith(".txt") - ? "text/plain" - : "application/octet-stream", + type: path.endsWith(".zip") ? "application/zip" : path.endsWith(".txt") ? "text/plain" : "application/octet-stream", }); } diff --git a/packages/bun-release/src/github.ts b/packages/bun-release/src/github.ts index fbbbbf267..fd743d17e 100644 --- a/packages/bun-release/src/github.ts +++ b/packages/bun-release/src/github.ts @@ -3,10 +3,7 @@ import { Octokit } from "octokit"; import { fetch } from "./fetch"; import { debug, log, warn, error } from "./console"; -const [owner, repo] = process.env["GITHUB_REPOSITORY"]?.split("/") ?? [ - "oven-sh", - "bun", -]; +const [owner, repo] = process.env["GITHUB_REPOSITORY"]?.split("/") ?? ["oven-sh", "bun"]; const octokit = new Octokit({ auth: process.env["GITHUB_TOKEN"], @@ -24,14 +21,10 @@ const octokit = new Octokit({ export async function github<R extends Route>( url: R | keyof Endpoints, options?: Omit< - R extends keyof Endpoints - ? Endpoints[R]["parameters"] & RequestParameters - : RequestParameters, + R extends keyof Endpoints ? Endpoints[R]["parameters"] & RequestParameters : RequestParameters, "owner" | "repo" >, -): Promise< - R extends keyof Endpoints ? Endpoints[R]["response"]["data"] : unknown -> { +): Promise<R extends keyof Endpoints ? Endpoints[R]["response"]["data"] : unknown> { // @ts-ignore const { data } = await octokit.request(url, { owner, @@ -52,7 +45,7 @@ export async function getRelease(tag?: string) { export async function uploadAsset(tag: string, name: string, blob: Blob) { const release = await getRelease(tag); - const asset = release.assets.find((asset) => asset.name === name); + const asset = release.assets.find(asset => asset.name === name); // Github requires that existing assets are deleted before uploading // a new asset, but does not provide a rename or re-upload API?!? if (asset) { @@ -60,24 +53,21 @@ export async function uploadAsset(tag: string, name: string, blob: Blob) { asset_id: asset.id, }); } - return github( - "POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}", - { - baseUrl: "https://uploads.github.com", - release_id: release.id, - name, - headers: { - "content-type": blob.type, - "content-length": blob.size, - }, - data: Buffer.from(await blob.arrayBuffer()), + return github("POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}", { + baseUrl: "https://uploads.github.com", + release_id: release.id, + name, + headers: { + "content-type": blob.type, + "content-length": blob.size, }, - ); + data: Buffer.from(await blob.arrayBuffer()), + }); } export async function downloadAsset(tag: string, name: string): Promise<Blob> { const release = await getRelease(tag); - const asset = release.assets.find((asset) => asset.name === name); + const asset = release.assets.find(asset => asset.name === name); if (!asset) { throw new Error(`Asset not found: ${name}`); } @@ -86,8 +76,11 @@ export async function downloadAsset(tag: string, name: string): Promise<Blob> { } export async function getSha(tag: string, format?: "short" | "long") { - const { sha } = await github("GET /repos/{owner}/{repo}/git/tags/{tag_sha}", { - tag_sha: formatTag(tag), + const ref = formatTag(tag); + const { + object: { sha }, + } = await github("GET /repos/{owner}/{repo}/git/ref/{ref}", { + ref: ref === "canary" ? "heads/main" : `tags/${ref}`, }); return format === "short" ? sha.substring(0, 7) : sha; } diff --git a/packages/bun-release/src/npm/install.ts b/packages/bun-release/src/npm/install.ts index ac6f2e670..12d30ff94 100644 --- a/packages/bun-release/src/npm/install.ts +++ b/packages/bun-release/src/npm/install.ts @@ -53,10 +53,7 @@ async function requireBun(platform: Platform): Promise<string> { await downloadBun(platform, cwd); } catch (cause) { debug("downloadBun failed", cause); - error( - `Failed to download package "${module}" from "registry.npmjs.org".`, - cause, - ); + error(`Failed to download package "${module}" from "registry.npmjs.org".`, cause); } } return resolveBun(); @@ -69,14 +66,7 @@ function installBun(platform: Platform, dst: string): void { write(join(cwd, "package.json"), "{}"); const { exitCode } = spawn( "npm", - [ - "install", - "--loglevel=error", - "--prefer-offline", - "--no-audit", - "--progress=false", - `${module}@${version}`, - ], + ["install", "--loglevel=error", "--prefer-offline", "--no-audit", "--progress=false", `${module}@${version}`], { cwd, stdio: "pipe", @@ -100,9 +90,7 @@ function installBun(platform: Platform, dst: string): void { } async function downloadBun(platform: Platform, dst: string): Promise<void> { - const response = await fetch( - `https://registry.npmjs.org/${owner}/${platform.bin}/-/${platform.bin}-${version}.tgz`, - ); + const response = await fetch(`https://registry.npmjs.org/${owner}/${platform.bin}/-/${platform.bin}-${version}.tgz`); const tgz = await response.arrayBuffer(); let buffer: Buffer; try { @@ -111,10 +99,7 @@ async function downloadBun(platform: Platform, dst: string): Promise<void> { throw new Error("Invalid gzip data", { cause }); } function str(i: number, n: number): string { - return String.fromCharCode(...buffer.subarray(i, i + n)).replace( - /\0.*$/, - "", - ); + return String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, ""); } let offset = 0; while (offset < buffer.length) { diff --git a/packages/bun-release/src/platform.ts b/packages/bun-release/src/platform.ts index fb438d824..98fd6fe36 100644 --- a/packages/bun-release/src/platform.ts +++ b/packages/bun-release/src/platform.ts @@ -4,14 +4,9 @@ import { debug } from "./console"; export const os = process.platform; -export const arch = - os === "darwin" && process.arch === "x64" && isRosetta2() - ? "arm64" - : process.arch; +export const arch = os === "darwin" && process.arch === "x64" && isRosetta2() ? "arm64" : process.arch; -export const avx2 = - (arch === "x64" && os === "linux" && isLinuxAVX2()) || - (os === "darwin" && isDarwinAVX2()); +export const avx2 = (arch === "x64" && os === "linux" && isLinuxAVX2()) || (os === "darwin" && isDarwinAVX2()); export type Platform = { os: string; @@ -63,10 +58,7 @@ export const platforms: Platform[] = [ ]; export const supportedPlatforms: Platform[] = platforms - .filter( - (platform) => - platform.os === os && platform.arch === arch && (!platform.avx2 || avx2), - ) + .filter(platform => platform.os === os && platform.arch === arch && (!platform.avx2 || avx2)) .sort((a, b) => (a.avx2 === b.avx2 ? 0 : a.avx2 ? -1 : 1)); function isLinuxAVX2(): boolean { @@ -90,10 +82,7 @@ function isDarwinAVX2(): boolean { function isRosetta2(): boolean { try { - const { exitCode, stdout } = spawn("sysctl", [ - "-n", - "sysctl.proc_translated", - ]); + const { exitCode, stdout } = spawn("sysctl", ["-n", "sysctl.proc_translated"]); return exitCode === 0 && stdout.includes("1"); } catch (error) { debug("isRosetta2 failed", error); |