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

feat: replaced semver dependency with custom implementation #1594

Merged
merged 10 commits into from
Jan 5, 2024
6 changes: 2 additions & 4 deletions packages/versions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
17 changes: 4 additions & 13 deletions packages/versions/src/lib/checkFuelCoreVersionCompatibility.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import semver from 'semver';

import { getBuiltinVersions } from './getBuiltinVersions';
import { Semver } 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: Semver.majorEq(networkVersion, supportedVersion),
isMinorSupported: Semver.minorEq(networkVersion, supportedVersion),
isPatchSupported: Semver.patchEq(networkVersion, supportedVersion),
};
}
11 changes: 5 additions & 6 deletions packages/versions/src/lib/compareSystemVersions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import semver from 'semver';

import { getBuiltinVersions } from './getBuiltinVersions';
import { Semver } from './semver';

export interface ICompareVersionsParams {
systemForcVersion: string;
Expand All @@ -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 = Semver.gt(systemForcVersion, versions.FORC);
const systemFuelCoreIsGt = Semver.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 = Semver.eq(systemForcVersion, versions.FORC);
const systemFuelCoreIsEq = Semver.eq(systemFuelCoreVersion, versions.FUEL_CORE);

return {
systemForcIsGt,
Expand Down
51 changes: 51 additions & 0 deletions packages/versions/src/lib/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export class Semver {
private major: number;
private minor: number;
private patch: number;

private constructor(version: string) {
const [major, minor, patch] = version.split('.').map((v) => parseInt(v, 10));
this.major = major;
this.minor = minor;
this.patch = patch;
}

private static compareVersions(version1: string, version2: string): number {
const semver1 = new Semver(version1);
const semver2 = new Semver(version2);

if (semver1.major !== semver2.major) {
return semver1.major - semver2.major;
}
if (semver1.minor !== semver2.minor) {
return semver1.minor - semver2.minor;
}
return semver1.patch - semver2.patch;
}

public static gt(version1: string, version2: string): boolean {
return this.compareVersions(version1, version2) > 0;
}

public static eq(version1: string, version2: string): boolean {
return this.compareVersions(version1, version2) === 0;
}

public static majorEq(version1: string, version2: string): boolean {
const semver1 = new Semver(version1);
const semver2 = new Semver(version2);
return semver1.major === semver2.major;
}

public static minorEq(version1: string, version2: string): boolean {
const semver1 = new Semver(version1);
const semver2 = new Semver(version2);
return semver1.minor === semver2.minor;
}

public static patchEq(version1: string, version2: string): boolean {
const semver1 = new Semver(version1);
const semver2 = new Semver(version2);
return semver1.patch === semver2.patch;
}
}
arboleya marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading