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: allow semver ranges #719

Merged
merged 2 commits into from
Feb 1, 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
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
Loading