Skip to content

Commit 0fedaa2

Browse files
authored
refactor: make versions package usable locally (#850)
1 parent 0f75954 commit 0fedaa2

File tree

13 files changed

+564
-382
lines changed

13 files changed

+564
-382
lines changed

.changeset/quick-fireants-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/versions": patch
3+
---
4+
5+
Refactoring `versions` packages so that it can be used locally, providing accurate versions for all relevant parts of the stack

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"@changesets/changelog-github": "^0.4.7",
4646
"@changesets/cli": "^2.25.0",
4747
"@ethersproject/bytes": "^5.7.0",
48-
"@jest/types": "29.4.3",
49-
"@types/jest": "^29.2.3",
48+
"@jest/types": "^29.5.0",
49+
"@types/jest": "^29.5.0",
5050
"@types/node": "^14.18.32",
5151
"@types/node-fetch": "^2.6.2",
5252
"@types/shelljs": "^0.8.11",
@@ -70,7 +70,7 @@
7070
"ethers": "^5.7.2",
7171
"forc-bin": "workspace:*",
7272
"husky": "^8.0.3",
73-
"jest": "^29.3.1",
73+
"jest": "^29.5.0",
7474
"jest-text-transformer": "^1.0.4",
7575
"markdownlint": "^0.23.1",
7676
"markdownlint-cli": "^0.27.1",

packages/abi-typegen/src/templates/common/common.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@ import { renderCommonTemplate } from './common';
55
describe('templates/common', () => {
66
test('should render common template', () => {
77
// mocking
8-
const { restore } = mockVersions();
8+
const { versions, restore } = mockVersions();
99

1010
// executing
11-
const expectedVersion = /(Fuels|Forc|Fuel-Core) version: 0.0.0/;
11+
const rendered = renderCommonTemplate();
1212

1313
// validating
1414
restore();
1515

16-
expect(renderCommonTemplate()).toMatch(expectedVersion);
16+
const { FORC, FUELS, FUEL_CORE } = versions;
17+
18+
const expectedFuelsVersion = new RegExp(`Fuels version: ${FUELS}`);
19+
const expectedForcVersion = new RegExp(`Forc version: ${FORC}`);
20+
const expectedFuelCoreVersion = new RegExp(`Fuel-Core version: ${FUEL_CORE}`);
21+
22+
expect(rendered).toMatch(expectedFuelsVersion);
23+
expect(rendered).toMatch(expectedForcVersion);
24+
expect(rendered).toMatch(expectedFuelCoreVersion);
1725
});
1826
});
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { versions } from '@fuel-ts/versions';
1+
import * as versionsMod from '@fuel-ts/versions';
22

33
export function mockVersions(
44
values: {
@@ -11,14 +11,12 @@ export function mockVersions(
1111
FUEL_CORE: '33.33.33',
1212
}
1313
) {
14-
const original = { ...versions };
15-
16-
Object.assign(versions, values);
14+
const mock = jest.replaceProperty(versionsMod, 'versions', values);
1715

1816
return {
1917
versions: values,
2018
restore() {
21-
Object.assign(versions, original);
19+
mock.restore();
2220
},
2321
};
2422
}

packages/versions/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@types/semver": "^7.3.13"
5858
},
5959
"scripts": {
60-
"build": "tsup --dts --env.BUILD_VERSION $BUILD_VERSION --env.FORC_VERSION $FORC_VERSION --env.FUEL_CORE_VERSION $FUEL_CORE_VERSION"
60+
"prebuild": "ts-node ./scripts/rewriteVersions.ts",
61+
"build": "tsup --dts"
6162
}
6263
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { readVersions, readVersionsFromEnv, readVersionsFromFiles } from './rewriteVersions';
2+
3+
describe('getSupportedVersions.js', () => {
4+
function modifyEnv() {
5+
const envBackup = { ...process.env };
6+
7+
const BUILD_VERSION = '9.9.9';
8+
const FORC_VERSION = '8.8.8';
9+
const FUEL_CORE_VERSION = '7.7.7';
10+
11+
process.env.BUILD_VERSION = BUILD_VERSION;
12+
process.env.FORC_VERSION = FORC_VERSION;
13+
process.env.FUEL_CORE_VERSION = FUEL_CORE_VERSION;
14+
15+
return {
16+
BUILD_VERSION,
17+
FORC_VERSION,
18+
FUEL_CORE_VERSION,
19+
restoreEnv() {
20+
process.env = envBackup;
21+
},
22+
};
23+
}
24+
25+
test('should read versions from files', async () => {
26+
const versionsFromFiles = readVersionsFromFiles();
27+
28+
expect(versionsFromFiles.FORC).toBeTruthy();
29+
expect(versionsFromFiles.FUEL_CORE).toBeTruthy();
30+
expect(versionsFromFiles.FUELS).toBeTruthy();
31+
});
32+
33+
test('should read versions from env', async () => {
34+
// mocking
35+
const { BUILD_VERSION, FORC_VERSION, FUEL_CORE_VERSION, restoreEnv } = modifyEnv();
36+
37+
// executing
38+
const versions = readVersionsFromEnv();
39+
40+
// restoring
41+
restoreEnv();
42+
43+
// validating
44+
expect(versions.FORC).toEqual(FORC_VERSION);
45+
expect(versions.FUEL_CORE).toEqual(FUEL_CORE_VERSION);
46+
expect(versions.FUELS).toEqual(BUILD_VERSION);
47+
});
48+
49+
test('should prioritize versions from env', async () => {
50+
// mocking
51+
const { BUILD_VERSION, FORC_VERSION, FUEL_CORE_VERSION, restoreEnv } = modifyEnv();
52+
53+
// executing
54+
const versions = readVersions();
55+
56+
// restoring
57+
restoreEnv();
58+
59+
// validating
60+
expect(versions.FORC).toEqual(FORC_VERSION);
61+
expect(versions.FUEL_CORE).toEqual(FUEL_CORE_VERSION);
62+
expect(versions.FUELS).toEqual(BUILD_VERSION);
63+
});
64+
65+
test('should fallback to versions from files', async () => {
66+
// executing
67+
const versions = readVersions();
68+
const versionsFromFiles = readVersionsFromFiles();
69+
70+
// validating
71+
expect(versions.FORC).toEqual(versionsFromFiles.FORC);
72+
expect(versions.FUEL_CORE).toEqual(versionsFromFiles.FUEL_CORE);
73+
expect(versions.FUELS).toEqual(versionsFromFiles.FUELS);
74+
});
75+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import { join } from 'path';
3+
4+
export const readVersionsFromFiles = () => {
5+
const rootDir = join(__dirname, '../../..');
6+
const packagesDir = join(rootDir, 'packages');
7+
const servicesDir = join(rootDir, 'services');
8+
9+
// forc-bin
10+
const forcPath = join(packagesDir, 'forc-bin', 'package.json');
11+
const forcPkgJson = JSON.parse(readFileSync(forcPath, 'utf8'));
12+
13+
// fuel-core
14+
const dockerFilePath = join(servicesDir, 'fuel-core', 'Dockerfile');
15+
const dockerFileContents = readFileSync(dockerFilePath, 'utf8');
16+
const regexFuelcore = /FROM ghcr\.io\/fuellabs\/fuel-core:v(\d+\.\d+\.\d+)/;
17+
const match = dockerFileContents.match(regexFuelcore);
18+
19+
// fuels
20+
const fuelsPath = join(packagesDir, 'fuels', 'package.json');
21+
const fuelsPkgJson = JSON.parse(readFileSync(fuelsPath, 'utf8'));
22+
23+
const versions = {
24+
FORC: forcPkgJson.config.forcVersion,
25+
FUELS: fuelsPkgJson.version,
26+
FUEL_CORE: match?.[1],
27+
};
28+
29+
return versions;
30+
};
31+
32+
export const readVersionsFromEnv = () => {
33+
const versions = {
34+
FORC: process.env.FORC_VERSION,
35+
FUEL_CORE: process.env.FUEL_CORE_VERSION,
36+
FUELS: process.env.BUILD_VERSION,
37+
};
38+
return versions;
39+
};
40+
41+
export const readVersions = () => {
42+
const fromFiles = readVersionsFromFiles();
43+
const fromEnv = readVersionsFromEnv();
44+
45+
const FUELS = fromEnv.FUELS || fromFiles.FUELS;
46+
const FORC = fromEnv.FORC || fromFiles.FORC;
47+
const FUEL_CORE = fromEnv.FUEL_CORE || fromFiles.FUEL_CORE;
48+
49+
return { FUELS, FORC, FUEL_CORE };
50+
};
51+
52+
export const rewriteVersions = () => {
53+
const { FUELS, FORC, FUEL_CORE } = readVersions();
54+
55+
const filepath = join(__dirname, '..', 'src', 'lib', 'getSupportedVersions.ts');
56+
57+
let contents = readFileSync(filepath, 'utf8');
58+
59+
contents = contents.replace(/FUELS: '[\d.]+'/, `FUELS: '${FUELS}'`);
60+
contents = contents.replace(/FORC: '[\d.]+'/, `FORC: '${FORC}'`);
61+
contents = contents.replace(/FUEL_CORE: '[\d.]+'/, `FUEL_CORE: '${FUEL_CORE}'`);
62+
63+
writeFileSync(filepath, contents);
64+
};
65+
66+
rewriteVersions();

packages/versions/src/index.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
/*
2-
Variables:
2+
1) Variables
3+
------------
34
`FUELS` — comes from `/packages/fuels/package.json`
45
`FUEL_CORE` — comes from `/services/fuel-core/Dockerfile`
56
`FORC` — comes from `/packages/forc-bin/package.json`
67
7-
The CI release routine reads the aforementioned files,
8-
and set all the variables prior to building the packages.
8+
3) Pre Build
9+
------------
10+
There's a `prebuild` script in:
11+
- packages/versions/package.json
912
10-
Take a look at the /.github/workflows/release` file.
13+
Before build, it will call this file:
14+
- packages/versions/scripts/replaceVersions.ts
1115
12-
The TSUP build then replaces all `process.env.<VAR_NAME>`
13-
entries bellow with their respective values from the env.
16+
Which will replace static versions at:
17+
- packages/versions/src/lib/getSupportedVersions.ts
1418
15-
If no env variables are set, the expression `!0` seems to be
16-
used by TSUP, which then results to `true`. For this reason,
17-
we check for it and use default values instead.
19+
If no env variables are set, it uses the current versions
20+
from the original locations mentioned in the 1st step.
21+
22+
3) CI
23+
------------
24+
As part of the CI release (1) routine, the changes made
25+
to the versions package by the `prebuild` routine will
26+
be committed by the same script (2) that handles the
27+
docs versioning.
28+
29+
- (1) <repoRoot>/.github/workflows/release.yaml
30+
- (2) <repoRoot>/changeset-version-with-docs.ts
31+
32+
4) Build
33+
------------
34+
By the time we get to the `build` step, everything is in
35+
place already and ready to be built and released.
1836
*/
1937

2038
import { getSupportedVersions } from './lib/getSupportedVersions';
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
1-
import { getSupportedVersions, thisVersionOrDefault } from './getSupportedVersions';
1+
import { readVersionsFromFiles } from '../../scripts/rewriteVersions';
2+
3+
import { getSupportedVersions } from './getSupportedVersions';
24

35
describe('getSupportedVersions.js', () => {
46
test('should return received version of default', async () => {
5-
expect(thisVersionOrDefault()).toEqual('0.0.0');
6-
expect(thisVersionOrDefault('true')).toEqual('0.0.0');
7-
expect(thisVersionOrDefault(undefined)).toEqual('0.0.0');
8-
expect(thisVersionOrDefault('1.1.1')).toEqual('1.1.1');
9-
});
10-
11-
test('should get versions just fine', async () => {
12-
// mocking
13-
const envBackup = { ...process.env };
14-
15-
const BUILD_VERSION = '9.9.9';
16-
const FORC_VERSION = '8.8.8';
17-
const FUEL_CORE_VERSION = '7.7.7';
18-
19-
process.env.BUILD_VERSION = BUILD_VERSION;
20-
process.env.FORC_VERSION = FORC_VERSION;
21-
process.env.FUEL_CORE_VERSION = FUEL_CORE_VERSION;
22-
23-
// executing
24-
const env = getSupportedVersions();
25-
26-
// restoring
27-
process.env = envBackup;
7+
const versions = getSupportedVersions();
8+
const versionsFromFiles = readVersionsFromFiles();
289

29-
// validating
30-
expect(env.FUELS).toEqual(BUILD_VERSION);
31-
expect(env.FORC).toEqual(FORC_VERSION);
32-
expect(env.FUEL_CORE).toEqual(FUEL_CORE_VERSION);
10+
expect(versions.FORC).toEqual(versionsFromFiles.FORC);
11+
expect(versions.FUEL_CORE).toEqual(versionsFromFiles.FUEL_CORE);
12+
expect(versions.FUELS).toEqual(versionsFromFiles.FUELS);
3313
});
3414
});
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
export function thisVersionOrDefault(version?: string | boolean) {
2-
if (version !== undefined) {
3-
const versionStr = version.toString();
4-
if (versionStr !== 'true') {
5-
return versionStr;
6-
}
7-
}
8-
return '0.0.0';
9-
}
10-
111
export function getSupportedVersions() {
122
return {
13-
FUELS: thisVersionOrDefault(process.env.BUILD_VERSION),
14-
FUEL_CORE: thisVersionOrDefault(process.env.FUEL_CORE_VERSION),
15-
FORC: thisVersionOrDefault(process.env.FORC_VERSION),
3+
FORC: '0.35.3',
4+
FUEL_CORE: '0.17.3',
5+
FUELS: '0.35.0',
166
};
177
}

0 commit comments

Comments
 (0)