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

fix: command fuels version not working #1883

Merged
merged 29 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7509323
Fixing system versions evaluation
arboleya Mar 15, 2024
23c7407
Refactoring broken tests
arboleya Mar 15, 2024
eacbc98
Setting defaults
arboleya Mar 15, 2024
84c5be7
Formatting
arboleya Mar 15, 2024
92364c4
Adding changeset
arboleya Mar 15, 2024
ff4f695
Returning error instead of throwing
arboleya Mar 15, 2024
5b30d70
DRY
arboleya Mar 16, 2024
6c13ae1
Formatting
arboleya Mar 16, 2024
5204747
Lintfix
arboleya Mar 16, 2024
cfb56b1
DRY
arboleya Mar 16, 2024
b1b5430
Fixing vitest ignore comment by appending @preserve to it
arboleya Mar 16, 2024
8724b3d
Removing useless validations
arboleya Mar 16, 2024
28f9bd4
Adding `eitherOr` method utility
arboleya Mar 16, 2024
c9100ff
Handling new error: fuelup exception
arboleya Mar 16, 2024
e42ea9c
Ignoring JS in final coverage
arboleya Mar 16, 2024
9d4450a
Ignoring bin files in coverage (usually not tested)
arboleya Mar 16, 2024
c88a583
Removing log call
arboleya Mar 16, 2024
be37d09
Adding line break
arboleya Mar 18, 2024
8b7ceb7
Adjusting regexp
arboleya Mar 18, 2024
cdc2b1b
Adjusting and DRYing conditional
arboleya Mar 18, 2024
bc19d32
Updating test description
arboleya Mar 18, 2024
52885ec
Tyop
arboleya Mar 18, 2024
b41d347
Renaming variable
arboleya Mar 18, 2024
e695498
Fixing test assertions
arboleya Mar 18, 2024
75df40e
Update packages/versions/src/cli.test.ts
arboleya Mar 19, 2024
738e1c2
DRYing regexps
arboleya Mar 19, 2024
178d5da
Updating changeset
arboleya Mar 19, 2024
b7792a8
Merge branch 'master' into aa/fix/versions
arboleya Mar 19, 2024
a76d599
Merge branch 'master' into aa/fix/versions
arboleya Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-seas-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/versions": patch
---

fix: command `fuels version` not working
12 changes: 6 additions & 6 deletions packages/versions/scripts/rewriteVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export const readVersionsFromFiles = () => {
const rootDir = join(__dirname, '../../..');
const packagesDir = join(rootDir, 'packages');

const firstLineReg = /^.+$/m;

// forc
const forcPath = join(packagesDir, 'forc', 'VERSION');
const forcContents = readFileSync(forcPath, 'utf8');
const forcVersion = forcContents.match(/^.+$/m)?.[0] || forcContents;
const forcVersion = readFileSync(forcPath, 'utf8').match(firstLineReg)?.[0];

// fuel-core
const fuelCorePath = join(packagesDir, 'fuel-core', 'VERSION');
const fuelCoreContents = readFileSync(fuelCorePath, 'utf8');
const fuelCoreVersion = fuelCoreContents.match(/^.+$/m)?.[0] || fuelCoreContents;
const fuelCoreVersion = readFileSync(fuelCorePath, 'utf8').match(firstLineReg)?.[0];

// fuels
const fuelsPath = join(packagesDir, 'fuels', 'package.json');
Expand Down Expand Up @@ -62,8 +62,8 @@ export const rewriteVersions = () => {
writeFileSync(filepath, contents);
};

/* istanbul ignore next */
// Do not auto-run script when inside vitest
/* istanbul ignore next -- @preserve */
if (!process.env.VITEST) {
// do not auto-run script when inside vitest
rewriteVersions();
}
41 changes: 40 additions & 1 deletion packages/versions/src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runVersions } from './cli';
import { eitherOr, runVersions } from './cli';
import * as colorizeUserVersionMod from './lib/colorizeUserVersion';
import * as compareSystemVersionsMod from './lib/compareSystemVersions';
import * as getBuiltinVersionsMod from './lib/getBuiltinVersions';
Expand Down Expand Up @@ -27,6 +27,7 @@ describe('cli.js', () => {
systemFuelCoreIsEq: boolean;
systemForcVersion: string;
systemFuelCoreVersion: string;
systemVersionsError: Error | null;
}) {
const {
systemForcVersion,
Expand All @@ -35,6 +36,7 @@ describe('cli.js', () => {
systemFuelCoreIsEq,
systemForcIsGt,
systemForcIsEq,
systemVersionsError,
} = params;

const error = vi.spyOn(console, 'error').mockImplementation(() => []);
Expand All @@ -53,6 +55,7 @@ describe('cli.js', () => {
}));

vi.spyOn(getSystemVersionsMod, 'getSystemVersions').mockImplementation(() => ({
error: systemVersionsError,
systemForcVersion,
systemFuelCoreVersion,
}));
Expand Down Expand Up @@ -82,6 +85,7 @@ describe('cli.js', () => {
systemFuelCoreIsEq: false,
systemForcIsGt: true,
systemForcIsEq: false,
systemVersionsError: null,
});

// executing
Expand All @@ -102,6 +106,7 @@ describe('cli.js', () => {
systemFuelCoreIsEq: true,
systemForcIsGt: false,
systemForcIsEq: true,
systemVersionsError: null,
});

// executing
Expand All @@ -122,6 +127,7 @@ describe('cli.js', () => {
systemFuelCoreIsEq: false,
systemForcIsGt: false,
systemForcIsEq: false,
systemVersionsError: null,
});

// executing
Expand All @@ -132,4 +138,37 @@ describe('cli.js', () => {
expect(exit).toHaveBeenCalledWith(1);
expect(error).toHaveBeenCalledTimes(3);
});

test('should warn about fuelup exception', () => {
// mocks
const systemVersionsError = new Error('fuelup exception');

const { error, info, exit } = mockAllDeps({
systemForcVersion: '0.0.1',
systemFuelCoreVersion: '0.0.1',
systemFuelCoreIsGt: false,
systemFuelCoreIsEq: false,
systemForcIsGt: false,
systemForcIsEq: false,
systemVersionsError,
});

// executing
runVersions();

// validating
expect(info).toHaveBeenCalledTimes(0);
expect(exit).toHaveBeenCalledWith(1);
expect(error).toHaveBeenCalledTimes(4);

expect(error.mock.calls[1][0]).toMatch(/make sure you/i);
expect(error.mock.calls[2][0]).toMatch(/>> Error: /i);
expect(error.mock.calls[2][1]).toMatch(/fuelup exception/i);
expect(error.mock.calls[3][0]).toMatch(/fuellabs\/fuelup/);
});

it('should use fallback values', () => {
expect(eitherOr('a', 'b')).toEqual('a');
expect(eitherOr(null, 'b')).toEqual('b');
});
});
38 changes: 26 additions & 12 deletions packages/versions/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export * from './lib/compareSystemVersions';
export * from './lib/fuelUpLink';
export * from './lib/getSystemVersions';

export const eitherOr = (val1: string | null, val2: string) => val1 ?? val2;

export function runVersions() {
const { error, info } = console;

Expand All @@ -20,21 +22,21 @@ export function runVersions() {
head: ['', bold('Supported'), bold(`Yours / System`)],
});

const { systemForcVersion, systemFuelCoreVersion } = getSystemVersions();
const { error: systemError, systemForcVersion, systemFuelCoreVersion } = getSystemVersions();

const comparisons = compareSystemVersions({
systemForcVersion,
systemFuelCoreVersion,
systemForcVersion: eitherOr(systemForcVersion, '0'),
systemFuelCoreVersion: eitherOr(systemFuelCoreVersion, '0'),
});

const userForcColorized = colorizeUserVersion({
version: systemForcVersion,
version: eitherOr(systemForcVersion, '—'),
isGt: comparisons.systemForcIsGt,
isOk: comparisons.systemForcIsEq,
});

const userFuelCoreColorized = colorizeUserVersion({
version: systemFuelCoreVersion,
version: eitherOr(systemFuelCoreVersion, '—'),
isGt: comparisons.systemFuelCoreIsGt,
isOk: comparisons.systemFuelCoreIsEq,
});
Expand All @@ -45,18 +47,30 @@ export function runVersions() {
const someIsGt = comparisons.systemForcIsGt || comparisons.systemFuelCoreIsGt;
const bothAreExact = comparisons.systemForcIsEq && comparisons.systemFuelCoreIsEq;

let exitCode: number;

if (someIsGt) {
info(`Your system's components are newer than the ones supported!`);
exitCode = 0;
info(cliTable.toString());
process.exit(0);
info(`\nYour system's components are newer than the ones supported!`);
} else if (bothAreExact) {
info(`You have all the right versions! ⚡`);
exitCode = 0;
info(cliTable.toString());
process.exit(0);
info(`\nYou have all the right versions! ⚡`);
} else if (systemError) {
exitCode = 1;
error(cliTable.toString());
error('\n - Make sure you have Forc and Fuel-Core installed');
error(' >> Error: ', systemError.message);
} else {
error(`You're using outdated versions — update them with:`);
error(` ${green(fuelUpLink)}`);
exitCode = 1;
error(cliTable.toString());
process.exit(1);
error(`\n - You're using outdated versions`);
}

if (exitCode === 1) {
error(` ${green(fuelUpLink)}`);
}

process.exit(exitCode);
}
36 changes: 24 additions & 12 deletions packages/versions/src/lib/getSystemVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('getSystemVersions.js', () => {
// mocking
const systemForcVersion = '1.0.0';
const systemFuelCoreVersion = '2.0.0';
const { error, execSync } = mockAllDeps({
const { execSync } = mockAllDeps({
systemForcVersion,
systemFuelCoreVersion,
});
Expand All @@ -60,33 +60,45 @@ describe('getSystemVersions.js', () => {
const versions = getSystemVersions();

// validating
expect(error).toHaveBeenCalledTimes(0);
expect(execSync).toHaveBeenCalledTimes(2);
expect(versions.systemForcVersion).toEqual(systemForcVersion);
expect(versions.systemFuelCoreVersion).toEqual(systemFuelCoreVersion);
});

test('should throw if Forc or Fuel-Core is not installed', () => {
test('should return error if Forc or Fuel-Core is not installed', () => {
// mocking
const systemForcVersion = '1.0.0';
const systemFuelCoreVersion = '2.0.0';
const { error } = mockAllDeps({

mockAllDeps({
systemForcVersion,
systemFuelCoreVersion,
shouldThrow: true,
});

// executing
let errorMsg: Error | undefined;
const { error: systemError } = getSystemVersions();

// validating
expect(systemError).toBeTruthy();
});

test('should throw for fuelup exception', () => {
// mocking
const systemForcVersion = 'fuelup exception';
const systemFuelCoreVersion = 'fuelup exception';
const { execSync } = mockAllDeps({
systemForcVersion,
systemFuelCoreVersion,
});

try {
getSystemVersions();
} catch (err) {
errorMsg = err as unknown as Error;
}
// executing
const versions = getSystemVersions();

// validating
expect(error).toHaveBeenCalledTimes(2);
expect(errorMsg).toBeTruthy();
expect(execSync).toHaveBeenCalledTimes(2);
expect(versions.error?.toString()).toEqual(`Error: ${systemForcVersion}`);
expect(versions.systemForcVersion).toEqual(null);
expect(versions.systemFuelCoreVersion).toEqual(null);
});
});
57 changes: 21 additions & 36 deletions packages/versions/src/lib/getSystemVersions.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
import { green } from 'chalk';
import { execSync } from 'child_process';

import { fuelUpLink } from './fuelUpLink';
const versionReg = /[0-9]+\.[0-9]+\.[0-9]/;

const stdio = 'ignore';

export function getSystemForc() {
let systemForcVersion: string | null = null;
export const getSystemVersion = (command: string) => {
let version: string | null = null;
let error: Error | null = null;

try {
const reg = /[^0-9.]+/g;
systemForcVersion = execSync('forc --version', { stdio }).toString().replace(reg, '');
const contents = execSync(command).toString();
if (versionReg.test(contents)) {
version = contents.match(versionReg)?.[0] as string;
} else {
throw new Error(contents);
}
} catch (err: unknown) {
error = err as Error;
}

return {
error,
systemForcVersion,
version,
};
};

export function getSystemForc() {
const { error, version: v } = getSystemVersion('forc --version');
return { error, systemForcVersion: v };
}

export function getSystemFuelCore() {
let systemFuelCoreVersion: string | null = null;
let error: Error | null = null;

try {
const reg = /[^0-9.]+/g;
systemFuelCoreVersion = execSync('fuel-core --version', { stdio }).toString().replace(reg, '');
} catch (err: unknown) {
error = err as Error;
}

return {
error,
systemFuelCoreVersion,
};
const { error, version: v } = getSystemVersion('fuel-core --version');
return { error, systemFuelCoreVersion: v };
}

export function getSystemVersions() {
const { error } = console;

const { error: errorForc, systemForcVersion } = getSystemForc();
const { error: errorCore, systemFuelCoreVersion } = getSystemFuelCore();

const err = errorForc ?? errorCore;

if (err) {
error('Make sure you have Forc and Fuel-Core installed.');
error(` ${green(fuelUpLink)}`);
throw err;
}
const error = errorForc ?? errorCore;

return {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
systemForcVersion: systemForcVersion!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
systemFuelCoreVersion: systemFuelCoreVersion!,
error,
systemForcVersion,
systemFuelCoreVersion,
};
}
2 changes: 2 additions & 0 deletions vite.base.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export default defineConfig({
reporter: ["json", "text", "html"],
include: ["packages", "internal", "apps"],
exclude: [
"**/*.js",
"**/node_modules/**",
"**/dist/**",
"**/test/**",
"**/*.test.ts",
"**/*.d.ts",
"**/bin.ts",
"packages/fuel-gauge/**",
"apps/demo-*",
"apps/docs",
Expand Down