Skip to content

Commit

Permalink
feat: replaced semver dependency with custom implementation (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk authored Jan 5, 2024
1 parent 7e46db9 commit f698256
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 29 deletions.
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 { 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),
};
}
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 { gt, eq } 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 = 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,
Expand Down
43 changes: 43 additions & 0 deletions packages/versions/src/lib/semver.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
46 changes: 46 additions & 0 deletions packages/versions/src/lib/semver.ts
Original file line number Diff line number Diff line change
@@ -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;
}
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.

0 comments on commit f698256

Please sign in to comment.