From f698256b8a360fe7132200eaa10d57128f1045a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Fri, 5 Jan 2024 11:29:34 +0100 Subject: [PATCH] feat: replaced `semver` dependency with custom implementation (#1594) --- packages/versions/package.json | 6 +-- .../lib/checkFuelCoreVersionCompatibility.ts | 17 ++----- .../versions/src/lib/compareSystemVersions.ts | 11 ++--- packages/versions/src/lib/semver.test.ts | 43 +++++++++++++++++ packages/versions/src/lib/semver.ts | 46 +++++++++++++++++++ pnpm-lock.yaml | 6 --- 6 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 packages/versions/src/lib/semver.test.ts create mode 100644 packages/versions/src/lib/semver.ts diff --git a/packages/versions/package.json b/packages/versions/package.json index 2a7daa6240b..0d4708c5c6c 100644 --- a/packages/versions/package.json +++ b/packages/versions/package.json @@ -42,11 +42,9 @@ "license": "Apache-2.0", "dependencies": { "chalk": "4", - "cli-table": "^0.3.11", - "semver": "^7.3.8" + "cli-table": "^0.3.11" }, "devDependencies": { - "@types/cli-table": "^0.3.1", - "@types/semver": "^7.3.13" + "@types/cli-table": "^0.3.1" } } diff --git a/packages/versions/src/lib/checkFuelCoreVersionCompatibility.ts b/packages/versions/src/lib/checkFuelCoreVersionCompatibility.ts index 169644436c0..b9fa9c4d0c2 100644 --- a/packages/versions/src/lib/checkFuelCoreVersionCompatibility.ts +++ b/packages/versions/src/lib/checkFuelCoreVersionCompatibility.ts @@ -1,22 +1,13 @@ -import semver from 'semver'; - import { getBuiltinVersions } from './getBuiltinVersions'; +import { majorEq, minorEq, patchEq } from './semver'; export function checkFuelCoreVersionCompatibility(networkVersion: string) { const { FUEL_CORE: supportedVersion } = getBuiltinVersions(); - const networkMajor = semver.major(networkVersion); - const networkMinor = semver.minor(networkVersion); - const networkPatch = semver.patch(networkVersion); - - const supportedMajor = semver.major(supportedVersion); - const supportedMinor = semver.minor(supportedVersion); - const supportedPatch = semver.patch(supportedVersion); - return { supportedVersion, - isMajorSupported: networkMajor === supportedMajor, - isMinorSupported: networkMinor === supportedMinor, - isPatchSupported: networkPatch === supportedPatch, + isMajorSupported: majorEq(networkVersion, supportedVersion), + isMinorSupported: minorEq(networkVersion, supportedVersion), + isPatchSupported: patchEq(networkVersion, supportedVersion), }; } diff --git a/packages/versions/src/lib/compareSystemVersions.ts b/packages/versions/src/lib/compareSystemVersions.ts index 2cc7dd566cb..12f47794091 100644 --- a/packages/versions/src/lib/compareSystemVersions.ts +++ b/packages/versions/src/lib/compareSystemVersions.ts @@ -1,6 +1,5 @@ -import semver from 'semver'; - import { getBuiltinVersions } from './getBuiltinVersions'; +import { gt, eq } from './semver'; export interface ICompareVersionsParams { systemForcVersion: string; @@ -13,12 +12,12 @@ export function compareSystemVersions(params: ICompareVersionsParams) { const versions = getBuiltinVersions(); // are user's versions GREATER than the ones supported by the SDK? - const systemForcIsGt = semver.gt(systemForcVersion, versions.FORC); - const systemFuelCoreIsGt = semver.gt(systemFuelCoreVersion, versions.FUEL_CORE); + const systemForcIsGt = gt(systemForcVersion, versions.FORC); + const systemFuelCoreIsGt = gt(systemFuelCoreVersion, versions.FUEL_CORE); // are user's versions EXACTLY the ones supported by the SDK? - const systemForcIsEq = semver.eq(systemForcVersion, versions.FORC); - const systemFuelCoreIsEq = semver.eq(systemFuelCoreVersion, versions.FUEL_CORE); + const systemForcIsEq = eq(systemForcVersion, versions.FORC); + const systemFuelCoreIsEq = eq(systemFuelCoreVersion, versions.FUEL_CORE); return { systemForcIsGt, diff --git a/packages/versions/src/lib/semver.test.ts b/packages/versions/src/lib/semver.test.ts new file mode 100644 index 00000000000..e1ea836b54f --- /dev/null +++ b/packages/versions/src/lib/semver.test.ts @@ -0,0 +1,43 @@ +import { eq, gt, majorEq, minorEq, patchEq } from './semver'; + +/** + * @group node + */ +describe('semver', () => { + test('majorEq', () => { + expect(majorEq('1.2.3', '1.2.3')).toBe(true); + expect(majorEq('1.2.3', '1.2.4')).toBe(true); + expect(majorEq('1.2.3', '1.3.3')).toBe(true); + expect(majorEq('1.2.3', '2.2.3')).toBe(false); + }); + test('minorEq', () => { + expect(minorEq('1.2.3', '1.2.3')).toBe(true); + expect(minorEq('1.2.3', '1.2.4')).toBe(true); + expect(minorEq('1.2.3', '2.2.3')).toBe(true); + expect(minorEq('1.2.3', '1.3.3')).toBe(false); + }); + test('patchEq', () => { + expect(patchEq('1.2.3', '1.2.3')).toBe(true); + expect(patchEq('1.2.3', '1.3.3')).toBe(true); + expect(patchEq('1.2.3', '2.2.3')).toBe(true); + expect(patchEq('1.2.3', '1.2.4')).toBe(false); + }); + test('gt', () => { + expect(gt('1.2.3', '0.2.3')).toBe(true); + expect(gt('1.2.3', '1.1.3')).toBe(true); + expect(gt('1.2.3', '1.2.2')).toBe(true); + expect(gt('1.2.3', '1.2.3')).toBe(false); + expect(gt('1.2.3', '2.2.3')).toBe(false); + expect(gt('1.2.3', '1.3.3')).toBe(false); + expect(gt('1.2.3', '1.2.4')).toBe(false); + }); + test('eq', () => { + expect(eq('1.2.3', '1.2.3')).toBe(true); + expect(eq('1.2.3', '0.2.3')).toBe(false); + expect(eq('1.2.3', '2.2.3')).toBe(false); + expect(eq('1.2.3', '1.1.3')).toBe(false); + expect(eq('1.2.3', '1.3.3')).toBe(false); + expect(eq('1.2.3', '1.2.2')).toBe(false); + expect(eq('1.2.3', '1.2.4')).toBe(false); + }); +}); diff --git a/packages/versions/src/lib/semver.ts b/packages/versions/src/lib/semver.ts new file mode 100644 index 00000000000..a479e2670e4 --- /dev/null +++ b/packages/versions/src/lib/semver.ts @@ -0,0 +1,46 @@ +function parseVersion(version: string): { major: number; minor: number; patch: number } { + const [major, minor, patch] = version.split('.').map((v) => parseInt(v, 10)); + return { major, minor, patch }; +} + +function versionDiffs( + version1: string, + version2: string +): { major: number; minor: number; patch: number; fullVersionDiff: number } { + const semver1 = parseVersion(version1); + const semver2 = parseVersion(version2); + const major = semver1.major - semver2.major; + const minor = semver1.minor - semver2.minor; + const patch = semver1.patch - semver2.patch; + return { + major, + minor, + patch, + fullVersionDiff: major || minor || patch, + }; +} + +export function gt(version1: string, version2: string): boolean { + const { fullVersionDiff } = versionDiffs(version1, version2); + return fullVersionDiff > 0; +} + +export function eq(version1: string, version2: string): boolean { + const { fullVersionDiff } = versionDiffs(version1, version2); + return fullVersionDiff === 0; +} + +export function majorEq(version1: string, version2: string): boolean { + const { major } = versionDiffs(version1, version2); + return major === 0; +} + +export function minorEq(version1: string, version2: string): boolean { + const { minor } = versionDiffs(version1, version2); + return minor === 0; +} + +export function patchEq(version1: string, version2: string): boolean { + const { patch } = versionDiffs(version1, version2); + return patch === 0; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43aea01f8ba..37343747a9a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1160,16 +1160,10 @@ importers: cli-table: specifier: ^0.3.11 version: 0.3.11 - semver: - specifier: ^7.3.8 - version: 7.3.8 devDependencies: '@types/cli-table': specifier: ^0.3.1 version: 0.3.1 - '@types/semver': - specifier: ^7.3.13 - version: 7.3.13 packages/wallet: dependencies: