Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch to deno release binary for publishing by default #100

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
],
])
}
`);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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" },
},
Expand Down
7 changes: 4 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [
Expand Down
26 changes: 19 additions & 7 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,30 +26,39 @@ export interface DownloadInfo {
url: string;
filename: string;
version: string;
canary: boolean;
}

export async function getDenoDownloadUrl(): Promise<DownloadInfo> {
export async function getDenoDownloadUrl(
canary: boolean,
): Promise<DownloadInfo> {
const key = `${process.platform} ${os.arch()}`;
if (!(key in FILENAMES)) {
throw new Error(`Unsupported platform: ${key}`);
}

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,
};
}

Expand All @@ -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) => {
Expand Down
24 changes: 23 additions & 1 deletion test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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+.*$/,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnpm changed their output again. For us it makes no difference if the ^ is there, because the lockfile will have an exact version anyways.

);

const npmRc = await readTextFile(path.join(dir, ".npmrc"));
Expand Down Expand Up @@ -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<DenoJson>(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",
});
});
});
}
});

Expand Down
Loading