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

chore: inform users if their fuels version is outdated #3108

Merged
merged 28 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a5947fd
test `reportUserVersion`
Dhaiwat10 Sep 5, 2024
ad4809a
Merge branch 'master' into dp/outdated-fuels-banner
Dhaiwat10 Sep 9, 2024
d661804
move util to `versions` package
Dhaiwat10 Sep 11, 2024
78307a6
export via cli and not index
Dhaiwat10 Sep 11, 2024
be39edc
log fuels version
Dhaiwat10 Sep 11, 2024
bc6aff1
fetch latest version
Dhaiwat10 Sep 11, 2024
26a6bb1
log out comparison
Dhaiwat10 Sep 11, 2024
96b55c1
add changeset
Dhaiwat10 Sep 11, 2024
14e8376
Merge branch 'master' into dp/outdated-fuels-banner
Dhaiwat10 Sep 11, 2024
5952013
fix tests
Dhaiwat10 Sep 11, 2024
23df8b1
Merge branch 'master' into dp/outdated-fuels-banner
Dhaiwat10 Sep 11, 2024
efbdf79
disable pr release
Dhaiwat10 Sep 11, 2024
4089c45
Merge branch 'dp/outdated-fuels-banner' of https://github.com/FuelLab…
Dhaiwat10 Sep 11, 2024
8723265
add tests
Dhaiwat10 Sep 11, 2024
ee603f2
re-use `getBuiltinVersions`
Dhaiwat10 Sep 12, 2024
6d14901
move to `fuels` CLI from `versions`
Dhaiwat10 Sep 13, 2024
735d75d
revert test changes
Dhaiwat10 Sep 13, 2024
b92908e
revert tests
Dhaiwat10 Sep 13, 2024
7c6bd3e
add tests
Dhaiwat10 Sep 13, 2024
b910a43
Merge branch 'master' into dp/outdated-fuels-banner
Dhaiwat10 Sep 13, 2024
0cf5780
add testing group
Dhaiwat10 Sep 13, 2024
83ab787
add changeset
Dhaiwat10 Sep 13, 2024
e679942
add a test case
Dhaiwat10 Sep 13, 2024
df5a925
Merge branch 'master' into dp/outdated-fuels-banner
maschad Sep 13, 2024
5df5b78
print a msg if fetch fails
Dhaiwat10 Sep 16, 2024
cd5434e
add error msg in one more place
Dhaiwat10 Sep 16, 2024
32d24dc
fix test
Dhaiwat10 Sep 16, 2024
1a33f96
Merge branch 'master' into dp/outdated-fuels-banner
Dhaiwat10 Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/eleven-jeans-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/versions": patch
"fuels": patch
---

chore: inform users if their `fuels` version is outdated
68 changes: 68 additions & 0 deletions packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as versionsMod from '@fuel-ts/versions';

import * as checkForAndDisplayUpdatesMod from './checkForAndDisplayUpdates';
import * as loggerMod from './logger';

/**
* @group node
*/
describe('checkForAndDisplayUpdates', () => {
beforeEach(() => {
vi.resetAllMocks();
});

afterEach(() => {
vi.restoreAllMocks();
});

const mockDeps = (params: { latestVersion: string; userVersion: string }) => {
const { latestVersion, userVersion } = params;
vi.spyOn(Promise, 'race').mockReturnValue(Promise.resolve(latestVersion));

vi.spyOn(versionsMod, 'versions', 'get').mockReturnValue({
FUELS: userVersion,
FORC: '1.0.0',
FUEL_CORE: '1.0.0',
});

const log = vi.spyOn(loggerMod, 'log');
const warn = vi.spyOn(loggerMod, 'warn');

return { log, warn };
};

test('should fail gracefully if the fetch fails', async () => {
vi.spyOn(global, 'fetch').mockImplementation(() =>
Promise.reject(new Error('Failed to fetch'))
);
const log = vi.spyOn(loggerMod, 'log');
await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow();
expect(log).toHaveBeenCalledWith('\n Unable to fetch latest fuels version. Skipping...\n');
});

test('should log a warning if the version is outdated', async () => {
const { warn } = mockDeps({ latestVersion: '1.0.1', userVersion: '1.0.0' });
await checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates();
expect(warn).toHaveBeenCalledWith(
'\n⚠️ There is a newer version of fuels available: 1.0.1. Your version is: 1.0.0\n'
);
});

test('should log a success message if the version is up to date', async () => {
const { log } = mockDeps({ latestVersion: '1.0.0', userVersion: '1.0.0' });
await checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates();
expect(log).toHaveBeenCalledWith('\n✅ Your fuels version is up to date: 1.0.0\n');
});

test('should handle fetch timing out', async () => {
vi.spyOn(global, 'fetch').mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(resolve, 5000);
})
);
const log = vi.spyOn(loggerMod, 'log');
await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow();
Dhaiwat10 marked this conversation as resolved.
Show resolved Hide resolved
expect(log).toHaveBeenCalledWith('\n Unable to fetch latest fuels version. Skipping...\n');
});
});
43 changes: 43 additions & 0 deletions packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { versions, gt, eq } from '@fuel-ts/versions';

import { warn, log } from './logger';

export const getLatestFuelsVersion = async () => {
const response = await fetch('https://registry.npmjs.org/fuels/latest');
const data = await response.json();
return data.version as string;
};

export const checkForAndDisplayUpdates = async () => {
try {
const { FUELS: userFuelsVersion } = versions;

const latestFuelsVersion = await Promise.race<string | undefined>([
new Promise((resolve) => {
setTimeout(resolve, 3000);
}),
getLatestFuelsVersion(),
]);
arboleya marked this conversation as resolved.
Show resolved Hide resolved

if (!latestFuelsVersion) {
log(`\n Unable to fetch latest fuels version. Skipping...\n`);
return;
Dhaiwat10 marked this conversation as resolved.
Show resolved Hide resolved
}

const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion);
const isFuelsVersionUpToDate = eq(latestFuelsVersion, userFuelsVersion);

if (isFuelsVersionOutdated) {
warn(
`\n⚠️ There is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}\n`
);
return;
}

if (isFuelsVersionUpToDate) {
log(`\n✅ Your fuels version is up to date: ${userFuelsVersion}\n`);
}
} catch {
log(`\n Unable to fetch latest fuels version. Skipping...\n`);
}
};
4 changes: 3 additions & 1 deletion packages/fuels/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { configureCli } from './cli';
import { checkForAndDisplayUpdates } from './cli/utils/checkForAndDisplayUpdates';
import { error } from './cli/utils/logger';

export const run = async (argv: string[]) => {
const program = configureCli();
return program.parseAsync(argv);
return Promise.all([await checkForAndDisplayUpdates().catch(error), program.parseAsync(argv)]);
arboleya marked this conversation as resolved.
Show resolved Hide resolved
};
1 change: 1 addition & 0 deletions packages/versions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export const versions = getBuiltinVersions();

export * from './lib/types';
export * from './lib/checkFuelCoreVersionCompatibility';
export * from './lib/semver';