From be3db2f63443060d03d4aaea1eca0f3b1f9bf96c Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 2 May 2024 14:46:28 +0200 Subject: [PATCH] fix: "show" cmd error for packages without stable versions --- package-lock.json | 12 ++++++++++-- package.json | 3 ++- src/commands.ts | 18 ++++++++++++++++-- test/commands.test.ts | 12 ++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index c681a8a..fbdef15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,11 +7,11 @@ "": { "name": "jsr", "version": "0.12.3", - "hasInstallScript": true, "license": "MIT", "dependencies": { "kolorist": "^1.8.0", - "node-stream-zip": "^1.15.0" + "node-stream-zip": "^1.15.0", + "semiver": "^1.1.0" }, "bin": { "jsr": "dist/bin.js" @@ -1118,6 +1118,14 @@ } ] }, + "node_modules/semiver": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/semiver/-/semiver-1.1.0.tgz", + "integrity": "sha512-QNI2ChmuioGC1/xjyYwyZYADILWyW6AmS1UH6gDj/SFUUUS4MBAWs/7mxnkRPc/F4iHezDP+O8t0dO8WHiEOdg==", + "engines": { + "node": ">=6" + } + }, "node_modules/serialize-javascript": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.0.tgz", diff --git a/package.json b/package.json index 85c961b..e0bd9ff 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ }, "dependencies": { "kolorist": "^1.8.0", - "node-stream-zip": "^1.15.0" + "node-stream-zip": "^1.15.0", + "semiver": "^1.1.0" } } diff --git a/src/commands.ts b/src/commands.ts index 761ca9f..d51171c 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -12,6 +12,7 @@ import { import { Bun, getPkgManager, PkgManagerName, YarnBerry } from "./pkg_manager"; import { downloadDeno, getDenoDownloadUrl } from "./download"; import { getNpmPackageInfo, getPackageMeta } from "./api"; +import semiver from "semiver"; const NPMRC_FILE = ".npmrc"; const BUNFIG_FILE = "bunfig.toml"; @@ -201,10 +202,23 @@ export async function showPackageInfo(raw: string) { const meta = await getPackageMeta(pkg); if (pkg.version === null) { - if (meta.latest === undefined) { + let latest = meta.latest; + if (latest === undefined) { throw new Error(`Missing latest version for ${pkg}`); + } else if (latest === null) { + // When no stable version is published: `latest === null`. We need to + // manually find the latest pre-release version + const versions = Object.keys(meta.versions); + + if (versions.length === 0) { + throw new Error(`Could not find published version for ${pkg}`); + } + + versions.sort(semiver); + pkg.version = versions[0]; + } else { + pkg.version = latest; } - pkg.version = meta.latest!; } const versionCount = Object.keys(meta.versions).length; diff --git a/test/commands.test.ts b/test/commands.test.ts index b2e5c2c..6c58702 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -788,4 +788,16 @@ describe("show", () => { process.cwd(), ); }); + + it("should show package information for pre-release only packages", async () => { + const output = await runJsr( + ["show", "@fresh/update"], + process.cwd(), + undefined, + true, + ); + const txt = kl.stripColors(output); + assert.ok(txt.includes("latest: -")); + assert.ok(txt.includes("npm tarball:")); + }); });