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(semver): use semver util to check version upgrades #1084

Merged
merged 1 commit into from
Aug 11, 2023
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 @@ -122,6 +122,7 @@
"react-joyride": "^2.5.3",
"react-redux": "^8.0.5",
"react-router-last-location": "^2.0.1",
"semver": "^7.5.4",
"showdown": "^2.1.0"
},
"packageManager": "[email protected]"
Expand Down
45 changes: 5 additions & 40 deletions src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { ISortBy, SortByDirection } from '@patternfly/react-table';
import _ from 'lodash';
import { useHistory } from 'react-router-dom';
import { BehaviorSubject, Observable } from 'rxjs';
import semverGt from 'semver/functions/gt';
import semverValid from 'semver/functions/valid';
import { getFromLocalStorage } from './LocalStorage';

const SECOND_MILLIS = 1000;
Expand Down Expand Up @@ -242,46 +244,9 @@ export const getActiveTab = <T>(search: string, key: string, supportedTabs: T[],

export const clickOutside = () => document.body.click();

export interface SemVer {
major: number;
minor: number;
patch: number;
}

// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
export const semverRegex =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

export const getSemVer = (str: string): SemVer | undefined => {
const matched = str.match(semverRegex);
if (matched) {
const [_, major, minor, patch] = matched;
return {
major: Number(major),
minor: Number(minor),
patch: Number(patch),
};
}
return undefined;
};

const convert = (ver: SemVer) => ver.major * 100 + ver.minor * 10 + ver.patch;

export const compareSemVer = (ver1: SemVer, ver2: SemVer): number => {
const _ver1 = convert(ver1);
const _ver2 = convert(ver2);
return _ver1 > _ver2 ? 1 : _ver1 < _ver2 ? -1 : 0;
};

export const isAssetNew = (currVerStr: string) => {
const oldVer = getSemVer(getFromLocalStorage('ASSET_VERSION', '0.0.0'));
const currVer = getSemVer(currVerStr);

if (!currVer) {
throw new Error(`Invalid asset version: ${currVer}`);
}
// Invalid (old) version is ignored.
return !oldVer || compareSemVer(currVer, oldVer) > 0;
export const isAssetNew = (currVer: string) => {
const oldVer: string = getFromLocalStorage('ASSET_VERSION', '0.0.0');
return !semverValid(oldVer) || semverGt(currVer, oldVer);
};

export const utf8ToBase32 = (str: string): string => {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4124,6 +4124,7 @@ __metadata:
rimraf: ^4.1.2
rxjs: ^7.8.0
selenium-webdriver: ^4.10.0
semver: ^7.5.4
showdown: ^2.1.0
style-loader: ^3.3.1
svg-url-loader: ^8.0.0
Expand Down Expand Up @@ -12057,6 +12058,17 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.5.4":
version: 7.5.4
resolution: "semver@npm:7.5.4"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3
languageName: node
linkType: hard

"send@npm:0.18.0":
version: 0.18.0
resolution: "send@npm:0.18.0"
Expand Down
Loading