[deps] Replace compare-versions with semver (#195287)

## Summary

We are reducing the number of dependencies by replacing the
`compare-versions` library with the already used `semver` library that
offer the same functionality.
This commit is contained in:
Marco Vettorello 2024-10-08 21:23:12 +02:00 committed by GitHub
parent 187afe78ce
commit 31f4f2c984
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 16 additions and 12 deletions

View file

@ -1075,7 +1075,6 @@
"classnames": "2.2.6",
"color": "^4.2.3",
"commander": "^4.1.1",
"compare-versions": "3.5.1",
"constate": "^3.3.2",
"copy-to-clipboard": "^3.0.8",
"core-js": "^3.37.1",

View file

@ -7,8 +7,9 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import versionCompare from 'compare-versions';
import valid from 'semver/functions/valid';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import ipaddr, { type IPv4, type IPv6 } from 'ipaddr.js';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import moment from 'moment';
@ -154,7 +155,9 @@ const versionComparison: CompareFn<string> = (v1, v2, direction) => {
if (bInvalid) {
return direction * -1;
}
return versionCompare(valueA, valueB);
const semVerValueA = semVerCoerce(valueA) ?? '';
const semVerValueB = semVerCoerce(valueB) ?? '';
return semVerCompare(semVerValueA, semVerValueB);
};
const openRange = { gte: -Infinity, lt: Infinity };

View file

@ -9,7 +9,8 @@
import _ from 'lodash';
import schemaParser from 'vega-schema-url-parser';
import versionCompare from 'compare-versions';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import hjson from 'hjson';
import { euiPaletteColorBlind } from '@elastic/eui';
import { euiThemeVars } from '@kbn/ui-theme';
@ -17,6 +18,7 @@ import { i18n } from '@kbn/i18n';
import { logger, Warn, None, version as vegaVersion } from 'vega';
import { compile, TopLevelSpec, version as vegaLiteVersion } from 'vega-lite';
import { EsQueryParser } from './es_query_parser';
import { Utils } from './utils';
import { EmsFileParser } from './ems_file_parser';
@ -558,8 +560,10 @@ The URL is an identifier only. Kibana and your browser will never access this UR
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLiteVersion : vegaVersion;
const schemaSemVer = semVerCoerce(schema.version) ?? '';
const libSemVersion = semVerCoerce(libVersion) ?? '';
if (versionCompare(schema.version, libVersion) > 0) {
if (semVerCompare(schemaSemVer, libSemVersion) > 0) {
this._onWarning(
i18n.translate(
'visTypeVega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage',

View file

@ -5,7 +5,6 @@
* 2.0.
*/
import React, { useState, useMemo, useEffect } from 'react';
import compareVersions from 'compare-versions';
import { EuiSpacer } from '@elastic/eui';
import { useParams, useHistory, generatePath } from 'react-router-dom';
import type {
@ -14,6 +13,8 @@ import type {
RuleStateAttributes,
} from '@kbn/cloud-security-posture-common/schema/rules/latest';
import { extractErrorMessage } from '@kbn/cloud-security-posture-common';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import { benchmarksNavigation } from '../../common/navigation/constants';
import { buildRuleKey } from '../../../common/utils/rules_states';
import { RulesTable } from './rules_table';
@ -197,7 +198,9 @@ export const RulesContainer = () => {
return a.localeCompare(b, 'en', { sensitivity: 'base' });
});
const cleanedRuleNumberList = [...new Set(ruleNumberList)].sort(compareVersions);
const cleanedRuleNumberList = [...new Set(ruleNumberList)].sort((a, b) =>
semVerCompare(semVerCoerce(a) ?? '', semVerCoerce(b) ?? '')
);
const rulesPageData = useMemo(
() => getRulesPageData(filteredRulesWithStates, status, error, rulesQuery),

View file

@ -14706,11 +14706,6 @@ commondir@^1.0.1:
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=
compare-versions@3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-3.5.1.tgz#26e1f5cf0d48a77eced5046b9f67b6b61075a393"
integrity sha512-9fGPIB7C6AyM18CJJBHt5EnCZDG3oiTJYy0NjfIAGjKpzv0tkxWko7TNQHF5ymqm7IH03tqmeuBxtvD+Izh6mg==
compare-versions@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-6.1.0.tgz#3f2131e3ae93577df111dba133e6db876ffe127a"