Skip to content

Improve get latest compatible version to search for major.minor.*any*… #2800

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions lib/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ export class NpmInstallationManager implements INpmInstallationManager {
}

public async getLatestCompatibleVersion(packageName: string): Promise<string> {
const configVersion = this.$staticConfig.version;
const isPreReleaseVersion = (<any>semver).prerelease(configVersion) !== null;
let cliVersionRange = `~${semver.major(configVersion)}.${(<any>semver).minor(configVersion)}.0`;
if (isPreReleaseVersion) {
// if the user has some 0-19 pre-release version, include pre-release versions in the search query.
cliVersionRange = `~${configVersion}`;
}

let cliVersionRange = `~${this.$staticConfig.version}`;
let latestVersion = await this.getLatestVersion(packageName);
if (semver.satisfies(latestVersion, cliVersionRange)) {
return latestVersion;
}
let data = await this.$npm.view(packageName, { "versions": true });

return semver.maxSatisfying(data, cliVersionRange) || latestVersion;
let maxSatisfying = semver.maxSatisfying(data, cliVersionRange);
return maxSatisfying || latestVersion;
}

public async install(packageName: string, projectDir: string, opts?: INpmInstallOptions): Promise<any> {
Expand Down
6 changes: 6 additions & 0 deletions test/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ describe("Npm installation manager tests", () => {
packageLatestVersion: "1.4.0",
cliVersion: "1.6.0-2016-10-01-182",
expectedResult: "1.4.0"
},
"When CLI Version has patch version larger than an existing package, should return max compliant package from the same major.minor version": {
versions: ["1.0.0", "1.0.1", "1.4.0", "2.5.0", "2.5.1", "2.5.2"],
packageLatestVersion: "3.0.0",
cliVersion: "2.5.4",
expectedResult: "2.5.2"
}
};

Expand Down