Skip to content

#1390 Coerce a semver when calculating updatables #1391

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

Merged
merged 1 commit into from
Sep 5, 2022
Merged
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
24 changes: 20 additions & 4 deletions arduino-ide-extension/src/common/protocol/installable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ export namespace Installable {
export namespace Version {
/**
* Most recent version comes first, then the previous versions. (`1.8.1`, `1.6.3`, `1.6.2`, `1.6.1` and so on.)
*
* If `coerce` is `true` tries to convert any invalid semver strings to a valid semver based on [these](https://github.com/npm/node-semver#coercion) rules.
*/
export const COMPARATOR = (left: Version, right: Version): number => {
if (semver.valid(left) && semver.valid(right)) {
return semver.compare(left, right);
export const COMPARATOR = (
left: Version,
right: Version,
coerce = false
): number => {
const validLeft = semver.parse(left);
const validRight = semver.parse(right);
if (validLeft && validRight) {
return semver.compare(validLeft, validRight);
}
if (coerce) {
const coercedLeft = validLeft ?? semver.coerce(left);
const coercedRight = validRight ?? semver.coerce(right);
if (coercedLeft && coercedRight) {
return semver.compare(coercedLeft, coercedRight);
}
}
return naturalCompare(left, right);
};
Expand All @@ -56,7 +71,8 @@ export namespace Installable {
}
const result = Installable.Version.COMPARATOR(
latestVersion,
installedVersion
installedVersion,
true
);
return result > 0;
};
Expand Down