From 4b41043e201e54c3e54a8d2d1780aa548791ac8d Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 15 Jul 2024 13:50:00 +0200 Subject: [PATCH] fix: switch to deno release binary for publishing by default (#100) * fix: switch to deno release binary for publishing by default This adds a new `DENO_BIN_CANARY` environment variable check to use the canary binary instead as well. * fix: update label --- src/bin.ts | 6 ++++++ src/commands.ts | 7 ++++--- src/download.ts | 26 +++++++++++++++++++------- test/commands.test.ts | 24 +++++++++++++++++++++++- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 04e6d61..b159b19 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -107,6 +107,10 @@ ${ "DENO_BIN_PATH", "Use specified Deno binary instead of local downloaded one.", ], + [ + "DENO_BIN_CANARY", + "Use the canary Deno binary instead of latest for publishing.", + ], ]) } `); @@ -148,6 +152,7 @@ if (args.length === 0) { run(async () => { const projectInfo = await findProjectDir(process.cwd()); return publish(process.cwd(), { + canary: process.env.DENO_BIN_CANARY !== undefined, binFolder, publishArgs: args.slice(1), pkgJsonPath: projectInfo.pkgJsonPath, @@ -185,6 +190,7 @@ if (args.length === 0) { pnpm: { type: "boolean", default: false }, bun: { type: "boolean", default: false }, debug: { type: "boolean", default: false }, + canary: { type: "boolean", default: false }, help: { type: "boolean", default: false, short: "h" }, version: { type: "boolean", default: false, short: "v" }, }, diff --git a/src/commands.ts b/src/commands.ts index 233ba5f..63464b1 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -129,10 +129,11 @@ export interface PublishOptions { binFolder: string; pkgJsonPath: string | null; publishArgs: string[]; + canary: boolean; } -async function getOrDownloadBinPath(binFolder: string) { - const info = await getDenoDownloadUrl(); +async function getOrDownloadBinPath(binFolder: string, canary: boolean) { + const info = await getDenoDownloadUrl(canary); const binPath = path.join( binFolder, @@ -164,7 +165,7 @@ async function getOrDownloadBinPath(binFolder: string) { export async function publish(cwd: string, options: PublishOptions) { const binPath = process.env.DENO_BIN_PATH ?? - await getOrDownloadBinPath(options.binFolder); + await getOrDownloadBinPath(options.binFolder, options.canary); // Ready to publish now! const args = [ diff --git a/src/download.ts b/src/download.ts index 86df311..4706164 100644 --- a/src/download.ts +++ b/src/download.ts @@ -10,6 +10,7 @@ import * as StreamZip from "node-stream-zip"; const streamFinished = util.promisify(stream.finished); const DENO_CANARY_INFO_URL = "https://dl.deno.land/canary-latest.txt"; +const DENO_RELEASE_INFO_URL = "https://dl.deno.land/release-latest.txt"; // Example: https://github.com/denoland/deno/releases/download/v1.41.0/deno-aarch64-apple-darwin.zip // Example: https://dl.deno.land/canary/d722de886b85093eeef08d1e9fd6f3193405762d/deno-aarch64-apple-darwin.zip @@ -25,9 +26,12 @@ export interface DownloadInfo { url: string; filename: string; version: string; + canary: boolean; } -export async function getDenoDownloadUrl(): Promise { +export async function getDenoDownloadUrl( + canary: boolean, +): Promise { const key = `${process.platform} ${os.arch()}`; if (!(key in FILENAMES)) { throw new Error(`Unsupported platform: ${key}`); @@ -35,20 +39,26 @@ export async function getDenoDownloadUrl(): Promise { const name = FILENAMES[key]; - const res = await fetch(DENO_CANARY_INFO_URL); + const url = canary ? DENO_CANARY_INFO_URL : DENO_RELEASE_INFO_URL; + const res = await fetch(url); if (!res.ok) { await res.body?.cancel(); throw new Error( - `${res.status}: Unable to retrieve canary version information from ${DENO_CANARY_INFO_URL}.`, + `${res.status}: Unable to retrieve ${ + canary ? "canary" : "release" + } version information from ${url}.`, ); } - const sha = (await res.text()).trim(); + const version = (await res.text()).trim(); const filename = name + ".zip"; return { - url: `https://dl.deno.land/canary/${decodeURI(sha)}/${filename}`, + canary, + url: canary + ? `https://dl.deno.land/canary/${decodeURI(version)}/${filename}` + : `https://dl.deno.land/release/${decodeURI(version)}/${filename}`, filename, - version: sha, + version: version, }; } @@ -66,7 +76,9 @@ export async function downloadDeno( throw new Error(`Unexpected empty body`); } - console.log(`Downloading JSR binary...`); + console.log( + `Downloading JSR ${info.canary ? "canary" : "release"} binary...`, + ); await withProgressBar( async (tick) => { diff --git a/test/commands.test.ts b/test/commands.test.ts index e016863..33b5a46 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -197,7 +197,7 @@ describe("install", () => { assert.match( pkgJson.dependencies["@std/encoding"], - /^npm:@jsr\/std__encoding@\d+\.\d+\.\d+.*$/, + /^npm:@jsr\/std__encoding@[^]?\d+\.\d+\.\d+.*$/, ); const npmRc = await readTextFile(path.join(dir, ".npmrc")); @@ -754,6 +754,28 @@ describe("publish", () => { }); }); }); + + it("use deno canary binary when DENO_BIN_CANARY when set", async () => { + await runInTempDir(async (dir) => { + await writeTextFile( + path.join(dir, "mod.ts"), + "export const value = 42;", + ); + + // TODO: Change this once deno supports jsr.json + await writeJson(path.join(dir, "deno.json"), { + name: "@deno/jsr-cli-test", + version: "1.0.0", + exports: { + ".": "./mod.ts", + }, + }); + + await runJsr(["publish", "--dry-run"], dir, { + DENO_BIN_CANARY: "true", + }); + }); + }); } });