Skip to content

Commit

Permalink
feat: adding utils package (#978)
Browse files Browse the repository at this point in the history
* Extracting Typegen utils (plus test utils) into a new `utils` package

* Fixing author name

* Adding changeset

* Renaming file and method

* Fixing remaining outdagted/broken refs

* Moving utilities around

* Moving test-utilities around

* Adding new output variable and renaming existing one

* Returning also the hexilified version of Forc projects’ `.bin` file

* Fixing broken file path in `package.json`

* Fixing another broken file path in `package.json`

* Fixing outdated refs

* Adjusting package configs

* Update packages/utils/src/test-utils/safeExec.test.ts

Co-authored-by: Dhaiwat <dhaiwatpandya@gmail.com>

* Undo unrelated change

* Renaming `normalize` method, reviewing docs, updating refs

* Undo unrelated changes

* Updating refs

---------

Co-authored-by: Dhaiwat <dhaiwatpandya@gmail.com>
  • Loading branch information
arboleya and Dhaiwat10 authored May 8, 2023
1 parent ddca7c8 commit b71ad9f
Show file tree
Hide file tree
Showing 30 changed files with 419 additions and 110 deletions.
7 changes: 7 additions & 0 deletions .changeset/curly-suns-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-typegen": patch
"fuels": patch
"@fuel-ts/utils": patch
---

Extracting Typegen utils (plus test utils) into a new package
1 change: 1 addition & 0 deletions packages/abi-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"license": "Apache-2.0",
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
"@fuel-ts/utils": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"commander": "^9.4.1",
"glob": "^8.0.3",
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/AbiTypeGen.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndCatch } from '../test/utils/executeAndCatch';
import { safeExec } from '@fuel-ts/utils/test';

import { getNewAbiTypegen } from '../test/utils/getNewAbiTypegen';

import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('AbiTypegen.ts', () => {

const programType = 'nope' as ProgramTypeEnum; // forced cast to cause error

const { error } = await executeAndCatch(() => {
const { error } = await safeExec(() => {
getNewAbiTypegen({ programType, includeBinFiles: true });
});

Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/abi/Abi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { safeExec } from '@fuel-ts/utils/test';

import { ForcProjectsEnum, getProjectResources } from '../../test/fixtures/forc-projects/index';
import { executeAndCatch } from '../../test/utils/executeAndCatch';
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType';
import * as parseFunctionsMod from '../utils/parseFunctions';
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('Abi.ts', () => {
test('should throw if contract name can not be obtained', async () => {
const fn = () => getMockedAbi({ inputPath: '' });

const { error, result } = await executeAndCatch(fn);
const { error, result } = await safeExec(fn);

const expectedErrorMsg = `Could not parse name from abi file: `;
expect(result).toBeFalsy();
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/abi/Abi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { normalizeString } from '@fuel-ts/utils';

import type { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { IFunction } from '../types/interfaces/IFunction';
import type { IRawAbi } from '../types/interfaces/IRawAbi';
import type { IType } from '../types/interfaces/IType';
import { normalizeName } from '../utils/normalize';
import { parseFunctions } from '../utils/parseFunctions';
import { parseTypes } from '../utils/parseTypes';

Expand Down Expand Up @@ -42,7 +43,7 @@ export class Abi {
throw new Error(`Could not parse name from abi file: ${filepath}`);
}

const name = `${normalizeName(abiName[1])}Abi`;
const name = `${normalizeString(abiName[1])}Abi`;

this.name = name;
this.programType = programType;
Expand Down
18 changes: 9 additions & 9 deletions packages/abi-typegen/src/runTypegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { safeExec } from '@fuel-ts/utils/test';
import { existsSync } from 'fs';
import { sync as globSync } from 'glob';
import { join } from 'path';
// eslint-disable-next-line import/no-extraneous-dependencies
import shelljs from 'shelljs';

import { getProjectResources, ForcProjectsEnum } from '../test/fixtures/forc-projects/index';
import { executeAndCatch } from '../test/utils/executeAndCatch';

import { runTypegen } from './runTypegen';
import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
Expand All @@ -18,7 +18,7 @@ describe('runTypegen.js', () => {
const cwd = process.cwd();
const inputs = [project.inputGlobal];
const output = project.tempDir;
const normalizedName = project.abiNormalizedName;
const normalizedName = project.normalizedName;
const programType = ProgramTypeEnum.CONTRACT;
const silent = true;

Expand All @@ -39,7 +39,7 @@ describe('runTypegen.js', () => {
silent,
});

const { error } = await executeAndCatch(fn);
const { error } = await safeExec(fn);

// validates execution was ok
expect(error).toBeFalsy();
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('runTypegen.js', () => {
const cwd = process.cwd();
const input = project.inputGlobal;
const output = project.tempDir;
const normalizedName = project.abiNormalizedName;
const normalizedName = project.normalizedName;

const programType = ProgramTypeEnum.CONTRACT;
const silent = true;
Expand All @@ -84,7 +84,7 @@ describe('runTypegen.js', () => {
silent,
});

const { error } = await executeAndCatch(fn);
const { error } = await safeExec(fn);

// validates execution was ok
expect(error).toBeFalsy();
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('runTypegen.js', () => {
const cwd = process.cwd();
const input = project.inputGlobal;
const output = project.tempDir;
const normalizedName = project.abiNormalizedName;
const normalizedName = project.normalizedName;
const programType = ProgramTypeEnum.SCRIPT;
const silent = true;

Expand All @@ -128,7 +128,7 @@ describe('runTypegen.js', () => {
silent,
});

const { error } = await executeAndCatch(fn);
const { error } = await safeExec(fn);

// validates execution was ok
expect(error).toBeFalsy();
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('runTypegen.js', () => {
});
};

const { error } = await executeAndCatch(fn);
const { error } = await safeExec(fn);

// restore bin to original place
shelljs.mv(tempBinPath, project.binPath);
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('runTypegen.js', () => {
silent,
});

const { error } = await executeAndCatch(fn);
const { error } = await safeExec(fn);

// validates execution was ok
expect(error?.message).toEqual(
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/templates/predicate/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { safeExec } from '@fuel-ts/utils/test';

import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index';
import factoryTemplate from '../../../test/fixtures/templates/predicate/factory.hbs';
import { executeAndCatch } from '../../../test/utils/executeAndCatch';
import { mockVersions } from '../../../test/utils/mockVersions';
import { Abi } from '../../abi/Abi';
import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum';
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('factory.ts', () => {
programType: ProgramTypeEnum.PREDICATE,
});

const { error } = await executeAndCatch(() => {
const { error } = await safeExec(() => {
renderFactoryTemplate({ abi });
});

Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/templates/script/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { safeExec } from '@fuel-ts/utils/test';

import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index';
import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs';
import { executeAndCatch } from '../../../test/utils/executeAndCatch';
import { mockVersions } from '../../../test/utils/mockVersions';
import { Abi } from '../../abi/Abi';
import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum';
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('factory.ts', () => {
programType: ProgramTypeEnum.SCRIPT,
});

const { error } = await executeAndCatch(() => {
const { error } = await safeExec(() => {
renderFactoryTemplate({ abi });
});

Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/utils/findType.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndCatch } from '../../test/utils/executeAndCatch';
import { safeExec } from '@fuel-ts/utils/test';

import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType';
import type { IType } from '../types/interfaces/IType';

Expand Down Expand Up @@ -32,7 +33,7 @@ describe('findType.ts', () => {
const types: IType[] = []; // empty array here, will error

const fn = () => findType({ typeId, types });
const { error, result } = await executeAndCatch(fn);
const { error, result } = await safeExec(fn);

expect(error).toBeTruthy();
expect(result).toBeFalsy();
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/utils/makeType.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndCatch } from '../../test/utils/executeAndCatch';
import { safeExec } from '@fuel-ts/utils/test';

import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType';

import { makeType } from './makeType';
Expand Down Expand Up @@ -26,7 +27,7 @@ describe('makeType.ts', () => {
const expectedErrorMsg = `Type not supported: ${rawAbiType.type}`;

const fn = () => makeType({ rawAbiType });
const { error, result } = await executeAndCatch<Error>(fn);
const { error, result } = await safeExec<Error>(fn);

expect(result).toBeFalsy();
expect(error?.message).toEqual(expectedErrorMsg);
Expand Down
29 changes: 0 additions & 29 deletions packages/abi-typegen/src/utils/normalize.test.ts

This file was deleted.

51 changes: 3 additions & 48 deletions packages/abi-typegen/test/fixtures/forc-projects/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { readFileSync } from 'fs';
import { getForcProject } from '@fuel-ts/utils/test';
import { join } from 'path';
import rimraf from 'rimraf';

import type { IRawAbi } from '../../../src/index';
import { normalizeName } from '../../../src/utils/normalize';

export enum ForcProjectsEnum {
ARRAY_OF_ENUMS = 'array-of-enums',
Expand All @@ -25,48 +23,5 @@ export enum ForcProjectsEnum {
VECTOR_SIMPLE = 'vector-simple',
}

export const getProjectDebugDir = (project: ForcProjectsEnum) =>
join(__dirname, project, 'out', 'debug');

export const getProjectTempDir = (project: ForcProjectsEnum) =>
join(getProjectDebugDir(project), '__types__');

export const getProjectAbiPath = (project: ForcProjectsEnum) =>
join(getProjectDebugDir(project), `${project}-abi.json`);

export const getProjectBinPath = (project: ForcProjectsEnum) =>
join(getProjectDebugDir(project), `${project}.bin`);

export const getProjectAbiName = (project: ForcProjectsEnum) => `${project}-abi`;

export const getProjectNormalizedName = (project: ForcProjectsEnum) => normalizeName(project);

export const getProjectAbi = (project: ForcProjectsEnum) => {
const projectPath = getProjectAbiPath(project);
const abiContents: IRawAbi = JSON.parse(readFileSync(projectPath, 'utf-8'));
return abiContents;
};

export const getProjectResources = (project: ForcProjectsEnum) => {
const debugDir = getProjectDebugDir(project);
const tempDir = getProjectTempDir(project);
const binPath = getProjectBinPath(project);
const abiPath = getProjectAbiPath(project);
const abiName = getProjectAbiName(project);
const abiContents = getProjectAbi(project);
const abiNormalizedName = getProjectNormalizedName(project);
const inputGlobal = `${debugDir}/*-abi.json`;

rimraf.sync(tempDir);

return {
debugDir,
tempDir,
binPath,
abiPath,
abiName,
abiContents,
abiNormalizedName,
inputGlobal,
};
};
export const getProjectResources = (project: ForcProjectsEnum) =>
getForcProject<IRawAbi>(join(__dirname, project));
64 changes: 64 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# `@fuel-ts/utils`

**@fuel-ts/utils** is a sub-module for interacting with **Fuel**.

It's a collection of utilities and test utilities that may be useful in other places.

# Table of contents

- [Documentation](#documentation)
- [Usage](#usage)
- [Installation](#installation)
- [Full SDK Installation](#full-sdk-installation)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [License](#license)

## Documentation

See [Fuel-ts Documentation](https://fuellabs.github.io/fuels-ts/packages/fuel-ts-utils/)

## Installation

```sh
yarn add @fuel-ts/utils
# or
npm add @fuel-ts/utils
```

### Utilities

```ts
import { normalizeString } from "@fuel-ts/utils";

console.log(normalizeString("fuel-labs"));
// FuelLabs
```

### Test Utilities

```ts
import { safeExec } from "@fuel-ts/utils/test"; // note the `test` suffix

console.log(safeExec(() => 123));
// { error: null, result: 123 }

console.log(
safeExec(() => {
throw new Error("Some error");
})
);
// { error: (Error: 'Some error'), result: null }
```

## Contributing

In order to contribute to `@fuel-ts/utils`, please see the main [fuels-ts](https://github.com/FuelLabs/fuels-ts) monorepo.

## Changelog

The `@fuel-ts/utils` changelog can be found at [CHANGELOG](./CHANGELOG.md).

## License

The primary license for `@fuel-ts/utils` is `Apache 2.0`, see [LICENSE](./LICENSE).
Loading

0 comments on commit b71ad9f

Please sign in to comment.