From 7d0c8e0735fe7ec162416adea8117609a1ca811b Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 1 Feb 2024 07:49:23 -0700 Subject: [PATCH] fix: allow semver ranges (#719) * fix: allow semver ranges * chore: clean up --- package.json | 1 + src/shared/installationVerification.ts | 9 +++++++-- src/shared/npmCommand.ts | 9 ++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index fbba896a..324dc914 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "npm": "10.2.3", "npm-run-path": "^4.0.1", "proxy-agent": "^6.3.1", + "semver": "^7.5.4", "shelljs": "^0.8.4" }, "devDependencies": { diff --git a/src/shared/installationVerification.ts b/src/shared/installationVerification.ts index 77d1c710..5a5275a0 100644 --- a/src/shared/installationVerification.ts +++ b/src/shared/installationVerification.ts @@ -17,6 +17,7 @@ import got from 'got'; import { ProxyAgent } from 'proxy-agent'; import { ux } from '@oclif/core'; import { prompts } from '@salesforce/sf-plugins-core'; +import { maxSatisfying } from 'semver'; import { NpmModule, NpmMeta } from './npmCommand.js'; import { NpmName } from './NpmName.js'; import { setErrorName } from './errors.js'; @@ -348,7 +349,9 @@ export class InstallationVerification implements Verifier { } // Assume the tag is version tag. - let versionNumber = npmMetadata.versions.find((version) => version === this.pluginNpmName?.tag); + let versionNumber = + maxSatisfying(npmMetadata.versions, this.pluginNpmName.tag) ?? + npmMetadata.versions.find((version) => version === this.pluginNpmName?.tag); logger.debug(`retrieveNpmMeta | versionObject: ${JSON.stringify(versionNumber)}`); @@ -363,7 +366,9 @@ export class InstallationVerification implements Verifier { // if we got a dist tag hit look up the version object if (tagVersionStr && tagVersionStr.length > 0 && tagVersionStr.includes('.')) { - versionNumber = npmMetadata.versions.find((version) => version === tagVersionStr); + versionNumber = + maxSatisfying(npmMetadata.versions, tagVersionStr) ?? + npmMetadata.versions.find((version) => version === tagVersionStr); logger.debug(`retrieveNpmMeta | versionObject: ${versionNumber}`); } else { const err = new SfError( diff --git a/src/shared/npmCommand.ts b/src/shared/npmCommand.ts index fb59832f..d75cad34 100644 --- a/src/shared/npmCommand.ts +++ b/src/shared/npmCommand.ts @@ -173,7 +173,14 @@ export class NpmModule { } try { - return JSON.parse(showCmd.stdout) as NpmShowResults; + // `npm show` returns an array of results when the version is a range + const raw = JSON.parse(showCmd.stdout) as NpmShowResults | NpmShowResults[]; + if (Array.isArray(raw)) { + // Return the last result in the array since that will be the highest version + // NOTE: .at() possibly returns undefined so instead directly index the array for the last element + return raw[raw.length - 1]; + } + return raw; } catch (error) { if (error instanceof Error) { throw setErrorName(new SfError(error.message, 'ShellParseError'), 'ShellParseError');