From a5947fd80d781f621a4dec01767d735ed1a652b7 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 5 Sep 2024 11:47:35 +0530 Subject: [PATCH 01/21] test `reportUserVersion` --- .github/workflows/pr-release.yaml | 2 +- packages/utils/src/index.ts | 1 + packages/utils/src/utils/reportUserVersion.ts | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 packages/utils/src/utils/reportUserVersion.ts diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index 6eeca1f3f04..593d7fce145 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -8,7 +8,7 @@ jobs: name: "Release PR to npm" runs-on: ubuntu-latest # comment out if:false to enable release PR to npm - if: false + # if: false permissions: write-all steps: - name: Checkout diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 4f21266f23c..e54826131a4 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -14,6 +14,7 @@ export * from './utils/dataSlice'; export * from './utils/toUtf8Bytes'; export * from './utils/toUtf8String'; export * from './utils/bytecode'; +export * from './utils/reportUserVersion'; /** * Used to verify that a switch statement exhausts all variants. diff --git a/packages/utils/src/utils/reportUserVersion.ts b/packages/utils/src/utils/reportUserVersion.ts new file mode 100644 index 00000000000..bf9de3cba5d --- /dev/null +++ b/packages/utils/src/utils/reportUserVersion.ts @@ -0,0 +1,20 @@ +import fs from 'fs'; +import path from 'path'; + +export function findUserPackageJson() { + if (!require.main) { + return null; + } + + let currentDir = path.dirname(require.main.filename); + + while (currentDir !== path.parse(currentDir).root) { + const packageJsonPath = path.join(currentDir, 'package.json'); + if (fs.existsSync(packageJsonPath)) { + return packageJsonPath; + } + currentDir = path.dirname(currentDir); // Move up one directory level + } + + return null; +} From d6618040bc7beb5389bed55b4794a7f03e6db39d Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 13:41:33 +0530 Subject: [PATCH 02/21] move util to `versions` package --- packages/utils/src/index.ts | 1 - packages/versions/src/index.ts | 1 + .../src/lib/getFuelsVersion.ts} | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename packages/{utils/src/utils/reportUserVersion.ts => versions/src/lib/getFuelsVersion.ts} (84%) diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e54826131a4..4f21266f23c 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -14,7 +14,6 @@ export * from './utils/dataSlice'; export * from './utils/toUtf8Bytes'; export * from './utils/toUtf8String'; export * from './utils/bytecode'; -export * from './utils/reportUserVersion'; /** * Used to verify that a switch statement exhausts all variants. diff --git a/packages/versions/src/index.ts b/packages/versions/src/index.ts index 8b918f41f73..491918195ad 100644 --- a/packages/versions/src/index.ts +++ b/packages/versions/src/index.ts @@ -41,3 +41,4 @@ export const versions = getBuiltinVersions(); export * from './lib/types'; export * from './lib/checkFuelCoreVersionCompatibility'; +export * from './lib/getFuelsVersion'; diff --git a/packages/utils/src/utils/reportUserVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts similarity index 84% rename from packages/utils/src/utils/reportUserVersion.ts rename to packages/versions/src/lib/getFuelsVersion.ts index bf9de3cba5d..84ae4724518 100644 --- a/packages/utils/src/utils/reportUserVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -13,7 +13,7 @@ export function findUserPackageJson() { if (fs.existsSync(packageJsonPath)) { return packageJsonPath; } - currentDir = path.dirname(currentDir); // Move up one directory level + currentDir = path.dirname(currentDir); } return null; From 78307a6722ae07145531ed9d5731bdbfdcdc1fe5 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 13:56:50 +0530 Subject: [PATCH 03/21] export via cli and not index --- packages/versions/src/cli.ts | 5 +++++ packages/versions/src/index.ts | 1 - packages/versions/src/lib/getFuelsVersion.ts | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index 5834ad11b5c..41410e7b459 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -5,6 +5,7 @@ import { colorizeUserVersion } from './lib/colorizeUserVersion'; import { compareSystemVersions } from './lib/compareSystemVersions'; import { fuelUpLink } from './lib/fuelUpLink'; import { getBuiltinVersions } from './lib/getBuiltinVersions'; +import { getFuelsVersion } from './lib/getFuelsVersion'; import { getSystemVersions } from './lib/getSystemVersions'; export * from './lib/compareSystemVersions'; @@ -18,6 +19,10 @@ export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; export function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; + const fuelsVersion = getFuelsVersion(); + + info(`\nFuels version: ${fuelsVersion}`); + const supportedVersions = getBuiltinVersions(); const cliTable = new CliTable({ diff --git a/packages/versions/src/index.ts b/packages/versions/src/index.ts index 491918195ad..8b918f41f73 100644 --- a/packages/versions/src/index.ts +++ b/packages/versions/src/index.ts @@ -41,4 +41,3 @@ export const versions = getBuiltinVersions(); export * from './lib/types'; export * from './lib/checkFuelCoreVersionCompatibility'; -export * from './lib/getFuelsVersion'; diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts index 84ae4724518..dee718d5337 100644 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; -export function findUserPackageJson() { +export function getFuelsVersion() { if (!require.main) { return null; } From be39edcf55972fa9d7221b1045cb275034c12657 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 18:26:08 +0530 Subject: [PATCH 04/21] log fuels version --- packages/versions/src/lib/getFuelsVersion.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts index dee718d5337..18cb2963e86 100644 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; -export function getFuelsVersion() { +const findUserPackageJson = () => { if (!require.main) { return null; } @@ -17,4 +17,13 @@ export function getFuelsVersion() { } return null; +}; + +export function getFuelsVersion() { + const packageJsonPath = findUserPackageJson(); + if (!packageJsonPath) { + return null; + } + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); + return packageJson.version; } From bc6aff1908d257721dfe80cae005f8a031b72c89 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 18:47:10 +0530 Subject: [PATCH 05/21] fetch latest version --- packages/versions/src/bin.ts | 6 +++++- packages/versions/src/cli.ts | 4 ++-- packages/versions/src/lib/getFuelsVersion.ts | 11 ++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/versions/src/bin.ts b/packages/versions/src/bin.ts index fb45251447d..2e4607ad3c5 100644 --- a/packages/versions/src/bin.ts +++ b/packages/versions/src/bin.ts @@ -1,3 +1,7 @@ import { runVersions } from './cli'; -runVersions(); +runVersions() + .then(() => process.exit(0)) + .catch(() => { + process.exit(1); + }); diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index 41410e7b459..a2e6f24bbfb 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -16,10 +16,10 @@ export * from './lib/getBuiltinVersions'; export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; -export function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { +export async function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; - const fuelsVersion = getFuelsVersion(); + const fuelsVersion = await getFuelsVersion(); info(`\nFuels version: ${fuelsVersion}`); diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts index 18cb2963e86..0f912be97c3 100644 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -19,7 +19,16 @@ const findUserPackageJson = () => { return null; }; -export function getFuelsVersion() { +const getLatestFuelsVersion = async () => { + // get the latest version from the npm registry + const response = await fetch('https://registry.npmjs.org/fuels/latest'); + const data = await response.json(); + return data.version; +}; + +export async function getFuelsVersion() { + console.log(await getLatestFuelsVersion()); + const packageJsonPath = findUserPackageJson(); if (!packageJsonPath) { return null; From 26a6bb1093928662c7750d8854aa85fd00f3f8eb Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 19:02:39 +0530 Subject: [PATCH 06/21] log out comparison --- packages/versions/src/cli.test.ts | 16 ++++++++-------- packages/versions/src/cli.ts | 15 ++++++++++++--- packages/versions/src/lib/getFuelsVersion.ts | 14 ++++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index a077b875380..464ea82d945 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -84,7 +84,7 @@ describe('cli.js', () => { /* Tests */ - test('should inform about newer versions', () => { + test('should inform about newer versions', async () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '1.1.1', @@ -99,7 +99,7 @@ describe('cli.js', () => { }); // executing - runVersions(); + await runVersions(); // validating expect(info).toHaveBeenCalledTimes(2); @@ -107,7 +107,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(0); }); - test('should inform about exact versions', () => { + test('should inform about exact versions', async () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '1.0.0', @@ -122,7 +122,7 @@ describe('cli.js', () => { }); // executing - runVersions(); + await runVersions(); // validating expect(info).toHaveBeenCalledTimes(2); @@ -130,7 +130,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(0); }); - test('should warn about older versions', () => { + test('should warn about older versions', async () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '0.0.1', @@ -145,7 +145,7 @@ describe('cli.js', () => { }); // executing - runVersions(); + await runVersions(); // validating expect(info).toHaveBeenCalledTimes(0); @@ -153,7 +153,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(3); }); - test('should warn about fuelup exception', () => { + test('should warn about fuelup exception', async () => { // mocks const systemVersionsError = new Error('fuelup exception'); @@ -170,7 +170,7 @@ describe('cli.js', () => { }); // executing - runVersions(); + await runVersions(); // validating expect(info).toHaveBeenCalledTimes(0); diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index a2e6f24bbfb..48560d7a385 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -5,8 +5,9 @@ import { colorizeUserVersion } from './lib/colorizeUserVersion'; import { compareSystemVersions } from './lib/compareSystemVersions'; import { fuelUpLink } from './lib/fuelUpLink'; import { getBuiltinVersions } from './lib/getBuiltinVersions'; -import { getFuelsVersion } from './lib/getFuelsVersion'; +import { getFuelsVersion, getLatestFuelsVersion } from './lib/getFuelsVersion'; import { getSystemVersions } from './lib/getSystemVersions'; +import { gt } from './lib/semver'; export * from './lib/compareSystemVersions'; export * from './lib/fuelUpLink'; @@ -19,9 +20,17 @@ export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; export async function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; - const fuelsVersion = await getFuelsVersion(); + const userFuelsVersion = getFuelsVersion(); + const latestFuelsVersion = await getLatestFuelsVersion(); + const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); - info(`\nFuels version: ${fuelsVersion}`); + if (isFuelsVersionOutdated) { + info( + `\nThere is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}` + ); + } else { + info(`\nYour fuels version is up to date: ${userFuelsVersion}`); + } const supportedVersions = getBuiltinVersions(); diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts index 0f912be97c3..ea3de2cc20c 100644 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -1,6 +1,8 @@ import fs from 'fs'; import path from 'path'; +import { gt } from './semver'; + const findUserPackageJson = () => { if (!require.main) { return null; @@ -19,16 +21,14 @@ const findUserPackageJson = () => { return null; }; -const getLatestFuelsVersion = async () => { +export const getLatestFuelsVersion = async () => { // get the latest version from the npm registry const response = await fetch('https://registry.npmjs.org/fuels/latest'); const data = await response.json(); return data.version; }; -export async function getFuelsVersion() { - console.log(await getLatestFuelsVersion()); - +export function getFuelsVersion() { const packageJsonPath = findUserPackageJson(); if (!packageJsonPath) { return null; @@ -36,3 +36,9 @@ export async function getFuelsVersion() { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); return packageJson.version; } + +export const checkIfFuelsVersionIsLatest = async () => { + const fuelsVersion = getFuelsVersion(); + const latestFuelsVersion = await getLatestFuelsVersion(); + return gt(latestFuelsVersion, fuelsVersion); +}; From 96b55c1c641c2334dda2d88dd61e3be9b610fea3 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 19:03:06 +0530 Subject: [PATCH 07/21] add changeset --- .changeset/lovely-seahorses-move.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lovely-seahorses-move.md diff --git a/.changeset/lovely-seahorses-move.md b/.changeset/lovely-seahorses-move.md new file mode 100644 index 00000000000..10bd9e12e26 --- /dev/null +++ b/.changeset/lovely-seahorses-move.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/versions": patch +--- + +chore: inform users if their `fuels` version is outdated From 59520130c941369fba2c43cc0c769ee50d5781f9 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 21:56:13 +0530 Subject: [PATCH 08/21] fix tests --- packages/versions/src/cli.test.ts | 26 +++++++++++++++++--- packages/versions/src/cli.ts | 4 +-- packages/versions/src/lib/getFuelsVersion.ts | 11 +-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index 464ea82d945..3b330925a26 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -2,6 +2,7 @@ import { eitherOr, runVersions } from './cli'; import * as colorizeUserVersionMod from './lib/colorizeUserVersion'; import * as compareSystemVersionsMod from './lib/compareSystemVersions'; import * as getBuiltinVersionsMod from './lib/getBuiltinVersions'; +import * as getFuelsVersionMod from './lib/getFuelsVersion'; import * as getSystemVersionsMod from './lib/getSystemVersions'; /** @@ -30,6 +31,8 @@ describe('cli.js', () => { systemForcVersion: string; systemFuelCoreVersion: string; systemVersionsError: Error | null; + userFuelsVersion: string; + latestFuelsVersion: string; }) { const { systemForcVersion, @@ -41,6 +44,8 @@ describe('cli.js', () => { systemForcIsEq, systemForcIsLt, systemVersionsError, + userFuelsVersion, + latestFuelsVersion, } = params; const error = vi.spyOn(console, 'error').mockImplementation(() => []); @@ -74,6 +79,11 @@ describe('cli.js', () => { FUELS: '1.0.0', })); + vi.spyOn(getFuelsVersionMod, 'getUserFuelsVersion').mockReturnValue(userFuelsVersion); + vi.spyOn(getFuelsVersionMod, 'getLatestFuelsVersion').mockReturnValue( + Promise.resolve(latestFuelsVersion) + ); + return { error, info, @@ -96,13 +106,15 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError: null, + userFuelsVersion: '1.0.0', + latestFuelsVersion: '1.0.1', }); // executing await runVersions(); // validating - expect(info).toHaveBeenCalledTimes(2); + expect(info).toHaveBeenCalledTimes(3); expect(exit).toHaveBeenCalledWith(0); expect(error).toHaveBeenCalledTimes(0); }); @@ -119,13 +131,15 @@ describe('cli.js', () => { systemForcIsEq: true, systemForcIsLt: false, systemVersionsError: null, + userFuelsVersion: '1.0.0', + latestFuelsVersion: '1.0.0', }); // executing await runVersions(); // validating - expect(info).toHaveBeenCalledTimes(2); + expect(info).toHaveBeenCalledTimes(3); expect(exit).toHaveBeenCalledWith(0); expect(error).toHaveBeenCalledTimes(0); }); @@ -142,13 +156,15 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: true, systemVersionsError: null, + userFuelsVersion: '1.0.0', + latestFuelsVersion: '1.0.1', }); // executing await runVersions(); // validating - expect(info).toHaveBeenCalledTimes(0); + expect(info).toHaveBeenCalledTimes(1); expect(exit).toHaveBeenCalledWith(1); expect(error).toHaveBeenCalledTimes(3); }); @@ -167,13 +183,15 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError, + userFuelsVersion: '1.0.0', + latestFuelsVersion: '1.0.1', }); // executing await runVersions(); // validating - expect(info).toHaveBeenCalledTimes(0); + expect(info).toHaveBeenCalledTimes(1); expect(exit).toHaveBeenCalledWith(1); expect(error).toHaveBeenCalledTimes(4); diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index 48560d7a385..dd1eafe2692 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -5,7 +5,7 @@ import { colorizeUserVersion } from './lib/colorizeUserVersion'; import { compareSystemVersions } from './lib/compareSystemVersions'; import { fuelUpLink } from './lib/fuelUpLink'; import { getBuiltinVersions } from './lib/getBuiltinVersions'; -import { getFuelsVersion, getLatestFuelsVersion } from './lib/getFuelsVersion'; +import { getUserFuelsVersion, getLatestFuelsVersion } from './lib/getFuelsVersion'; import { getSystemVersions } from './lib/getSystemVersions'; import { gt } from './lib/semver'; @@ -20,7 +20,7 @@ export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; export async function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; - const userFuelsVersion = getFuelsVersion(); + const userFuelsVersion = getUserFuelsVersion(); const latestFuelsVersion = await getLatestFuelsVersion(); const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts index ea3de2cc20c..41eef9f1ad4 100644 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ b/packages/versions/src/lib/getFuelsVersion.ts @@ -1,8 +1,6 @@ import fs from 'fs'; import path from 'path'; -import { gt } from './semver'; - const findUserPackageJson = () => { if (!require.main) { return null; @@ -22,13 +20,12 @@ const findUserPackageJson = () => { }; export const getLatestFuelsVersion = async () => { - // get the latest version from the npm registry const response = await fetch('https://registry.npmjs.org/fuels/latest'); const data = await response.json(); return data.version; }; -export function getFuelsVersion() { +export function getUserFuelsVersion() { const packageJsonPath = findUserPackageJson(); if (!packageJsonPath) { return null; @@ -36,9 +33,3 @@ export function getFuelsVersion() { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); return packageJson.version; } - -export const checkIfFuelsVersionIsLatest = async () => { - const fuelsVersion = getFuelsVersion(); - const latestFuelsVersion = await getLatestFuelsVersion(); - return gt(latestFuelsVersion, fuelsVersion); -}; From efbdf797304dae0068bd2d449fb60d15497d4320 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 22:01:23 +0530 Subject: [PATCH 09/21] disable pr release --- .github/workflows/pr-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index 593d7fce145..6eeca1f3f04 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -8,7 +8,7 @@ jobs: name: "Release PR to npm" runs-on: ubuntu-latest # comment out if:false to enable release PR to npm - # if: false + if: false permissions: write-all steps: - name: Checkout From 872326584295cd6f3b57ef8fdf432b0dc3928643 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 11 Sep 2024 22:07:11 +0530 Subject: [PATCH 10/21] add tests --- packages/versions/src/cli.test.ts | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index 3b330925a26..370d53aa91a 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -205,4 +205,50 @@ describe('cli.js', () => { expect(eitherOr('a', 'b')).toEqual('a'); expect(eitherOr(null, 'b')).toEqual('b'); }); + + test('should notify about newer npm version', async () => { + // mocks + const { info } = mockAllDeps({ + systemForcVersion: '1.1.1', + systemFuelCoreVersion: '1.1.1', + systemFuelCoreIsGt: true, + systemFuelCoreIsEq: false, + systemFuelCoreIsLt: false, + systemForcIsGt: true, + systemForcIsEq: false, + systemForcIsLt: false, + systemVersionsError: null, + userFuelsVersion: '1.0.0', + latestFuelsVersion: '1.0.1', + }); + + // executing + await runVersions(); + + expect(info).toHaveBeenCalledWith( + '\nThere is a newer version of fuels available: 1.0.1. Your version is: 1.0.0' + ); + }); + + test('should log proper message when npm version is the same as the latest', async () => { + // mocks + const { info } = mockAllDeps({ + systemForcVersion: '1.1.1', + systemFuelCoreVersion: '1.1.1', + systemFuelCoreIsGt: false, + systemFuelCoreIsEq: true, + systemFuelCoreIsLt: false, + systemForcIsGt: false, + systemForcIsEq: true, + systemForcIsLt: false, + systemVersionsError: null, + userFuelsVersion: '1.1.1', + latestFuelsVersion: '1.1.1', + }); + + // executing + await runVersions(); + + expect(info).toHaveBeenCalledWith('\nYour fuels version is up to date: 1.1.1'); + }); }); From ee603f2632fcf7e90e92cd3b70836884f18d54ca Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 12 Sep 2024 10:49:02 +0530 Subject: [PATCH 11/21] re-use `getBuiltinVersions` --- packages/versions/src/cli.test.ts | 11 +----- packages/versions/src/cli.ts | 4 +-- packages/versions/src/lib/getFuelsVersion.ts | 35 ------------------- .../versions/src/lib/getLatestFuelsVersion.ts | 5 +++ 4 files changed, 8 insertions(+), 47 deletions(-) delete mode 100644 packages/versions/src/lib/getFuelsVersion.ts create mode 100644 packages/versions/src/lib/getLatestFuelsVersion.ts diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index 370d53aa91a..4aebfadd269 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -2,7 +2,7 @@ import { eitherOr, runVersions } from './cli'; import * as colorizeUserVersionMod from './lib/colorizeUserVersion'; import * as compareSystemVersionsMod from './lib/compareSystemVersions'; import * as getBuiltinVersionsMod from './lib/getBuiltinVersions'; -import * as getFuelsVersionMod from './lib/getFuelsVersion'; +import * as getFuelsVersionMod from './lib/getLatestFuelsVersion'; import * as getSystemVersionsMod from './lib/getSystemVersions'; /** @@ -31,7 +31,6 @@ describe('cli.js', () => { systemForcVersion: string; systemFuelCoreVersion: string; systemVersionsError: Error | null; - userFuelsVersion: string; latestFuelsVersion: string; }) { const { @@ -44,7 +43,6 @@ describe('cli.js', () => { systemForcIsEq, systemForcIsLt, systemVersionsError, - userFuelsVersion, latestFuelsVersion, } = params; @@ -79,7 +77,6 @@ describe('cli.js', () => { FUELS: '1.0.0', })); - vi.spyOn(getFuelsVersionMod, 'getUserFuelsVersion').mockReturnValue(userFuelsVersion); vi.spyOn(getFuelsVersionMod, 'getLatestFuelsVersion').mockReturnValue( Promise.resolve(latestFuelsVersion) ); @@ -106,7 +103,6 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError: null, - userFuelsVersion: '1.0.0', latestFuelsVersion: '1.0.1', }); @@ -131,7 +127,6 @@ describe('cli.js', () => { systemForcIsEq: true, systemForcIsLt: false, systemVersionsError: null, - userFuelsVersion: '1.0.0', latestFuelsVersion: '1.0.0', }); @@ -156,7 +151,6 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: true, systemVersionsError: null, - userFuelsVersion: '1.0.0', latestFuelsVersion: '1.0.1', }); @@ -183,7 +177,6 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError, - userFuelsVersion: '1.0.0', latestFuelsVersion: '1.0.1', }); @@ -218,7 +211,6 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError: null, - userFuelsVersion: '1.0.0', latestFuelsVersion: '1.0.1', }); @@ -242,7 +234,6 @@ describe('cli.js', () => { systemForcIsEq: true, systemForcIsLt: false, systemVersionsError: null, - userFuelsVersion: '1.1.1', latestFuelsVersion: '1.1.1', }); diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index dd1eafe2692..6c73fcad03f 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -5,7 +5,7 @@ import { colorizeUserVersion } from './lib/colorizeUserVersion'; import { compareSystemVersions } from './lib/compareSystemVersions'; import { fuelUpLink } from './lib/fuelUpLink'; import { getBuiltinVersions } from './lib/getBuiltinVersions'; -import { getUserFuelsVersion, getLatestFuelsVersion } from './lib/getFuelsVersion'; +import { getLatestFuelsVersion } from './lib/getLatestFuelsVersion'; import { getSystemVersions } from './lib/getSystemVersions'; import { gt } from './lib/semver'; @@ -20,7 +20,7 @@ export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; export async function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; - const userFuelsVersion = getUserFuelsVersion(); + const { FUELS: userFuelsVersion } = getBuiltinVersions(); const latestFuelsVersion = await getLatestFuelsVersion(); const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); diff --git a/packages/versions/src/lib/getFuelsVersion.ts b/packages/versions/src/lib/getFuelsVersion.ts deleted file mode 100644 index 41eef9f1ad4..00000000000 --- a/packages/versions/src/lib/getFuelsVersion.ts +++ /dev/null @@ -1,35 +0,0 @@ -import fs from 'fs'; -import path from 'path'; - -const findUserPackageJson = () => { - if (!require.main) { - return null; - } - - let currentDir = path.dirname(require.main.filename); - - while (currentDir !== path.parse(currentDir).root) { - const packageJsonPath = path.join(currentDir, 'package.json'); - if (fs.existsSync(packageJsonPath)) { - return packageJsonPath; - } - currentDir = path.dirname(currentDir); - } - - return null; -}; - -export const getLatestFuelsVersion = async () => { - const response = await fetch('https://registry.npmjs.org/fuels/latest'); - const data = await response.json(); - return data.version; -}; - -export function getUserFuelsVersion() { - const packageJsonPath = findUserPackageJson(); - if (!packageJsonPath) { - return null; - } - const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); - return packageJson.version; -} diff --git a/packages/versions/src/lib/getLatestFuelsVersion.ts b/packages/versions/src/lib/getLatestFuelsVersion.ts new file mode 100644 index 00000000000..70996d9f96a --- /dev/null +++ b/packages/versions/src/lib/getLatestFuelsVersion.ts @@ -0,0 +1,5 @@ +export const getLatestFuelsVersion = async () => { + const response = await fetch('https://registry.npmjs.org/fuels/latest'); + const data = await response.json(); + return data.version; +}; From 6d14901720a7ec2fd50595600f690a1a6074e0c0 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 13:09:15 +0530 Subject: [PATCH 12/21] move to `fuels` CLI from `versions` --- .../cli/utils/checkForAndDisplayUpdates.ts | 34 +++++++++++++++++ packages/fuels/src/run.ts | 4 +- packages/versions/src/cli.test.ts | 37 ++++++------------- packages/versions/src/cli.ts | 16 +------- packages/versions/src/index.ts | 1 + .../versions/src/lib/getLatestFuelsVersion.ts | 5 --- 6 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts delete mode 100644 packages/versions/src/lib/getLatestFuelsVersion.ts diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts new file mode 100644 index 00000000000..b8fc757e60d --- /dev/null +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts @@ -0,0 +1,34 @@ +import { versions, gt } from '@fuel-ts/versions'; + +import { warn, log } from './logger'; + +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 () => { + const { FUELS: userFuelsVersion } = versions; + + const latestFuelsVersion = await Promise.race([ + new Promise((resolve) => { + setTimeout(resolve, 3000); + }), + getLatestFuelsVersion(), + ]); + + if (!latestFuelsVersion) { + return; + } + + const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); + + if (isFuelsVersionOutdated) { + warn( + `\n⚠️ There is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}\n` + ); + } else { + log(`\n✅ Your fuels version is up to date: ${userFuelsVersion}\n`); + } +}; diff --git a/packages/fuels/src/run.ts b/packages/fuels/src/run.ts index e6bf972037d..2fadc25ad0f 100644 --- a/packages/fuels/src/run.ts +++ b/packages/fuels/src/run.ts @@ -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)]); }; diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index 4aebfadd269..5959794d651 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -2,7 +2,6 @@ import { eitherOr, runVersions } from './cli'; import * as colorizeUserVersionMod from './lib/colorizeUserVersion'; import * as compareSystemVersionsMod from './lib/compareSystemVersions'; import * as getBuiltinVersionsMod from './lib/getBuiltinVersions'; -import * as getFuelsVersionMod from './lib/getLatestFuelsVersion'; import * as getSystemVersionsMod from './lib/getSystemVersions'; /** @@ -31,7 +30,6 @@ describe('cli.js', () => { systemForcVersion: string; systemFuelCoreVersion: string; systemVersionsError: Error | null; - latestFuelsVersion: string; }) { const { systemForcVersion, @@ -43,7 +41,6 @@ describe('cli.js', () => { systemForcIsEq, systemForcIsLt, systemVersionsError, - latestFuelsVersion, } = params; const error = vi.spyOn(console, 'error').mockImplementation(() => []); @@ -77,10 +74,6 @@ describe('cli.js', () => { FUELS: '1.0.0', })); - vi.spyOn(getFuelsVersionMod, 'getLatestFuelsVersion').mockReturnValue( - Promise.resolve(latestFuelsVersion) - ); - return { error, info, @@ -91,7 +84,7 @@ describe('cli.js', () => { /* Tests */ - test('should inform about newer versions', async () => { + test('should inform about newer versions', () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '1.1.1', @@ -103,11 +96,10 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError: null, - latestFuelsVersion: '1.0.1', }); // executing - await runVersions(); + runVersions(); // validating expect(info).toHaveBeenCalledTimes(3); @@ -115,7 +107,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(0); }); - test('should inform about exact versions', async () => { + test('should inform about exact versions', () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '1.0.0', @@ -127,11 +119,10 @@ describe('cli.js', () => { systemForcIsEq: true, systemForcIsLt: false, systemVersionsError: null, - latestFuelsVersion: '1.0.0', }); // executing - await runVersions(); + runVersions(); // validating expect(info).toHaveBeenCalledTimes(3); @@ -139,7 +130,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(0); }); - test('should warn about older versions', async () => { + test('should warn about older versions', () => { // mocks const { error, info, exit } = mockAllDeps({ systemForcVersion: '0.0.1', @@ -151,11 +142,10 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: true, systemVersionsError: null, - latestFuelsVersion: '1.0.1', }); // executing - await runVersions(); + runVersions(); // validating expect(info).toHaveBeenCalledTimes(1); @@ -163,7 +153,7 @@ describe('cli.js', () => { expect(error).toHaveBeenCalledTimes(3); }); - test('should warn about fuelup exception', async () => { + test('should warn about fuelup exception', () => { // mocks const systemVersionsError = new Error('fuelup exception'); @@ -177,11 +167,10 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError, - latestFuelsVersion: '1.0.1', }); // executing - await runVersions(); + runVersions(); // validating expect(info).toHaveBeenCalledTimes(1); @@ -199,7 +188,7 @@ describe('cli.js', () => { expect(eitherOr(null, 'b')).toEqual('b'); }); - test('should notify about newer npm version', async () => { + test('should notify about newer npm version', () => { // mocks const { info } = mockAllDeps({ systemForcVersion: '1.1.1', @@ -211,18 +200,17 @@ describe('cli.js', () => { systemForcIsEq: false, systemForcIsLt: false, systemVersionsError: null, - latestFuelsVersion: '1.0.1', }); // executing - await runVersions(); + runVersions(); expect(info).toHaveBeenCalledWith( '\nThere is a newer version of fuels available: 1.0.1. Your version is: 1.0.0' ); }); - test('should log proper message when npm version is the same as the latest', async () => { + test('should log proper message when npm version is the same as the latest', () => { // mocks const { info } = mockAllDeps({ systemForcVersion: '1.1.1', @@ -234,11 +222,10 @@ describe('cli.js', () => { systemForcIsEq: true, systemForcIsLt: false, systemVersionsError: null, - latestFuelsVersion: '1.1.1', }); // executing - await runVersions(); + runVersions(); expect(info).toHaveBeenCalledWith('\nYour fuels version is up to date: 1.1.1'); }); diff --git a/packages/versions/src/cli.ts b/packages/versions/src/cli.ts index 6c73fcad03f..5834ad11b5c 100644 --- a/packages/versions/src/cli.ts +++ b/packages/versions/src/cli.ts @@ -5,9 +5,7 @@ import { colorizeUserVersion } from './lib/colorizeUserVersion'; import { compareSystemVersions } from './lib/compareSystemVersions'; import { fuelUpLink } from './lib/fuelUpLink'; import { getBuiltinVersions } from './lib/getBuiltinVersions'; -import { getLatestFuelsVersion } from './lib/getLatestFuelsVersion'; import { getSystemVersions } from './lib/getSystemVersions'; -import { gt } from './lib/semver'; export * from './lib/compareSystemVersions'; export * from './lib/fuelUpLink'; @@ -17,21 +15,9 @@ export * from './lib/getBuiltinVersions'; export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2; -export async function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { +export function runVersions(params: { forcPath?: string; fuelCorePath?: string } = {}) { const { error, info } = console; - const { FUELS: userFuelsVersion } = getBuiltinVersions(); - const latestFuelsVersion = await getLatestFuelsVersion(); - const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); - - if (isFuelsVersionOutdated) { - info( - `\nThere is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}` - ); - } else { - info(`\nYour fuels version is up to date: ${userFuelsVersion}`); - } - const supportedVersions = getBuiltinVersions(); const cliTable = new CliTable({ diff --git a/packages/versions/src/index.ts b/packages/versions/src/index.ts index 8b918f41f73..0c566912914 100644 --- a/packages/versions/src/index.ts +++ b/packages/versions/src/index.ts @@ -41,3 +41,4 @@ export const versions = getBuiltinVersions(); export * from './lib/types'; export * from './lib/checkFuelCoreVersionCompatibility'; +export * from './lib/semver'; diff --git a/packages/versions/src/lib/getLatestFuelsVersion.ts b/packages/versions/src/lib/getLatestFuelsVersion.ts deleted file mode 100644 index 70996d9f96a..00000000000 --- a/packages/versions/src/lib/getLatestFuelsVersion.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const getLatestFuelsVersion = async () => { - const response = await fetch('https://registry.npmjs.org/fuels/latest'); - const data = await response.json(); - return data.version; -}; From 735d75d7e11d7af7e950f8fa5c1dc590c400b7b9 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 13:10:33 +0530 Subject: [PATCH 13/21] revert test changes --- packages/versions/src/bin.ts | 6 +----- packages/versions/src/cli.test.ts | 8 ++++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/versions/src/bin.ts b/packages/versions/src/bin.ts index 2e4607ad3c5..fb45251447d 100644 --- a/packages/versions/src/bin.ts +++ b/packages/versions/src/bin.ts @@ -1,7 +1,3 @@ import { runVersions } from './cli'; -runVersions() - .then(() => process.exit(0)) - .catch(() => { - process.exit(1); - }); +runVersions(); diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index 5959794d651..f6faa26e70f 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -102,7 +102,7 @@ describe('cli.js', () => { runVersions(); // validating - expect(info).toHaveBeenCalledTimes(3); + expect(info).toHaveBeenCalledTimes(2); expect(exit).toHaveBeenCalledWith(0); expect(error).toHaveBeenCalledTimes(0); }); @@ -125,7 +125,7 @@ describe('cli.js', () => { runVersions(); // validating - expect(info).toHaveBeenCalledTimes(3); + expect(info).toHaveBeenCalledTimes(2); expect(exit).toHaveBeenCalledWith(0); expect(error).toHaveBeenCalledTimes(0); }); @@ -148,7 +148,7 @@ describe('cli.js', () => { runVersions(); // validating - expect(info).toHaveBeenCalledTimes(1); + expect(info).toHaveBeenCalledTimes(0); expect(exit).toHaveBeenCalledWith(1); expect(error).toHaveBeenCalledTimes(3); }); @@ -173,7 +173,7 @@ describe('cli.js', () => { runVersions(); // validating - expect(info).toHaveBeenCalledTimes(1); + expect(info).toHaveBeenCalledTimes(0); expect(exit).toHaveBeenCalledWith(1); expect(error).toHaveBeenCalledTimes(4); From b92908e777ec83cebfd381f4ae5270f136e00e27 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 13:11:21 +0530 Subject: [PATCH 14/21] revert tests --- packages/versions/src/cli.test.ts | 42 ------------------------------- 1 file changed, 42 deletions(-) diff --git a/packages/versions/src/cli.test.ts b/packages/versions/src/cli.test.ts index f6faa26e70f..a077b875380 100644 --- a/packages/versions/src/cli.test.ts +++ b/packages/versions/src/cli.test.ts @@ -187,46 +187,4 @@ describe('cli.js', () => { expect(eitherOr('a', 'b')).toEqual('a'); expect(eitherOr(null, 'b')).toEqual('b'); }); - - test('should notify about newer npm version', () => { - // mocks - const { info } = mockAllDeps({ - systemForcVersion: '1.1.1', - systemFuelCoreVersion: '1.1.1', - systemFuelCoreIsGt: true, - systemFuelCoreIsEq: false, - systemFuelCoreIsLt: false, - systemForcIsGt: true, - systemForcIsEq: false, - systemForcIsLt: false, - systemVersionsError: null, - }); - - // executing - runVersions(); - - expect(info).toHaveBeenCalledWith( - '\nThere is a newer version of fuels available: 1.0.1. Your version is: 1.0.0' - ); - }); - - test('should log proper message when npm version is the same as the latest', () => { - // mocks - const { info } = mockAllDeps({ - systemForcVersion: '1.1.1', - systemFuelCoreVersion: '1.1.1', - systemFuelCoreIsGt: false, - systemFuelCoreIsEq: true, - systemFuelCoreIsLt: false, - systemForcIsGt: false, - systemForcIsEq: true, - systemForcIsLt: false, - systemVersionsError: null, - }); - - // executing - runVersions(); - - expect(info).toHaveBeenCalledWith('\nYour fuels version is up to date: 1.1.1'); - }); }); From 7c6bd3e26927ebdb36658b0a08e1e6539071b62b Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 15:04:36 +0530 Subject: [PATCH 15/21] add tests --- .../utils/checkForAndDisplayUpdates.test.ts | 51 +++++++++++++++++++ .../cli/utils/checkForAndDisplayUpdates.ts | 46 ++++++++++------- 2 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts new file mode 100644 index 00000000000..307f64ae4ac --- /dev/null +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -0,0 +1,51 @@ +import * as versionsMod from '@fuel-ts/versions'; + +import * as checkForAndDisplayUpdatesMod from './checkForAndDisplayUpdates'; +import * as loggerMod from './logger'; + +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')) + ); + await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow(); + }); + + 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'); + }); +}); diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts index b8fc757e60d..9eb4bd4c833 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts @@ -1,34 +1,42 @@ -import { versions, gt } from '@fuel-ts/versions'; +import { versions, gt, eq } from '@fuel-ts/versions'; import { warn, log } from './logger'; -const getLatestFuelsVersion = async () => { +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 () => { - const { FUELS: userFuelsVersion } = versions; + try { + const { FUELS: userFuelsVersion } = versions; - const latestFuelsVersion = await Promise.race([ - new Promise((resolve) => { - setTimeout(resolve, 3000); - }), - getLatestFuelsVersion(), - ]); + const latestFuelsVersion = await Promise.race([ + new Promise((resolve) => { + setTimeout(resolve, 3000); + }), + getLatestFuelsVersion(), + ]); - if (!latestFuelsVersion) { - return; - } + if (!latestFuelsVersion) { + return; + } + + const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); + const isFuelsVersionUpToDate = eq(latestFuelsVersion, userFuelsVersion); - const isFuelsVersionOutdated = gt(latestFuelsVersion, userFuelsVersion); + if (isFuelsVersionOutdated) { + warn( + `\n⚠️ There is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}\n` + ); + return; + } - if (isFuelsVersionOutdated) { - warn( - `\n⚠️ There is a newer version of fuels available: ${latestFuelsVersion}. Your version is: ${userFuelsVersion}\n` - ); - } else { - log(`\n✅ Your fuels version is up to date: ${userFuelsVersion}\n`); + if (isFuelsVersionUpToDate) { + log(`\n✅ Your fuels version is up to date: ${userFuelsVersion}\n`); + } + } catch { + /* Fail gracefully */ } }; From 0cf5780bc82002c9ae35393956f6d4fb5967bdee Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 15:20:46 +0530 Subject: [PATCH 16/21] add testing group --- packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index 307f64ae4ac..b28bb9ee70e 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -3,6 +3,9 @@ import * as versionsMod from '@fuel-ts/versions'; import * as checkForAndDisplayUpdatesMod from './checkForAndDisplayUpdates'; import * as loggerMod from './logger'; +/** + * @group node + */ describe('checkForAndDisplayUpdates', () => { beforeEach(() => { vi.resetAllMocks(); From 83ab7870c5b462e15b46c5349d87c21a21319566 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 15:22:59 +0530 Subject: [PATCH 17/21] add changeset --- .changeset/{lovely-seahorses-move.md => eleven-jeans-matter.md} | 1 + 1 file changed, 1 insertion(+) rename .changeset/{lovely-seahorses-move.md => eleven-jeans-matter.md} (86%) diff --git a/.changeset/lovely-seahorses-move.md b/.changeset/eleven-jeans-matter.md similarity index 86% rename from .changeset/lovely-seahorses-move.md rename to .changeset/eleven-jeans-matter.md index 10bd9e12e26..d422de2b9bd 100644 --- a/.changeset/lovely-seahorses-move.md +++ b/.changeset/eleven-jeans-matter.md @@ -1,5 +1,6 @@ --- "@fuel-ts/versions": patch +"fuels": patch --- chore: inform users if their `fuels` version is outdated From e6799428bb0fe422d5bfbc36de1b5a31462d09b0 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 13 Sep 2024 15:40:22 +0530 Subject: [PATCH 18/21] add a test case --- .../src/cli/utils/checkForAndDisplayUpdates.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index b28bb9ee70e..18aed11ea2f 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -51,4 +51,14 @@ describe('checkForAndDisplayUpdates', () => { 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); + }) + ); + await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow(); + }); }); From 5df5b7868361f4e8d5b3f6f44d7f5052650988ec Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Mon, 16 Sep 2024 12:33:12 +0530 Subject: [PATCH 19/21] print a msg if fetch fails --- packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts | 2 ++ packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index 18aed11ea2f..d9510759eea 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -35,7 +35,9 @@ describe('checkForAndDisplayUpdates', () => { 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 () => { diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts index 9eb4bd4c833..d0718cfd59a 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts @@ -37,6 +37,6 @@ export const checkForAndDisplayUpdates = async () => { log(`\n✅ Your fuels version is up to date: ${userFuelsVersion}\n`); } } catch { - /* Fail gracefully */ + log(`\n Unable to fetch latest fuels version. Skipping...\n`); } }; From cd5434ec1971678648792f1e44fb4b99c5ff5e35 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Mon, 16 Sep 2024 13:57:00 +0530 Subject: [PATCH 20/21] add error msg in one more place --- packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts | 2 ++ packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index d9510759eea..0bf879526d4 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -62,5 +62,7 @@ describe('checkForAndDisplayUpdates', () => { }) ); await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow(); + const log = vi.spyOn(loggerMod, 'log'); + expect(log).toHaveBeenCalledWith('\n Unable to fetch latest fuels version. Skipping...\n'); }); }); diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts index d0718cfd59a..32294045b2e 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts @@ -20,6 +20,7 @@ export const checkForAndDisplayUpdates = async () => { ]); if (!latestFuelsVersion) { + log(`\n Unable to fetch latest fuels version. Skipping...\n`); return; } From 32d24dc514a6bab57928b0097a5b03d2694e500e Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Mon, 16 Sep 2024 14:08:41 +0530 Subject: [PATCH 21/21] fix test --- packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index 0bf879526d4..107027e9d59 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -61,8 +61,8 @@ describe('checkForAndDisplayUpdates', () => { setTimeout(resolve, 5000); }) ); - await expect(checkForAndDisplayUpdatesMod.checkForAndDisplayUpdates()).resolves.not.toThrow(); 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'); }); });