Skip to content

Commit

Permalink
fix: allow semver ranges (#719)
Browse files Browse the repository at this point in the history
* fix: allow semver ranges

* chore: clean up
  • Loading branch information
mdonnalley authored Feb 1, 2024
1 parent 898a5c3 commit 7d0c8e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 7 additions & 2 deletions src/shared/installationVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)}`);

Expand All @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion src/shared/npmCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 7d0c8e0

Please sign in to comment.