Skip to content

Commit c57860a

Browse files
authored
feat: adding new forcBuildFlags property to fuels config (FuelLabs#1788)
1 parent 1918470 commit c57860a

File tree

12 files changed

+100
-17
lines changed

12 files changed

+100
-17
lines changed

.changeset/hot-onions-burn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fuels": patch
3+
---
4+
5+
Adding new `forcBuildFlags` property to `fuels` config

apps/demo-fuels/fuels.config.full.ts

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export default createConfig({
4747
fuelCorePort: 4000,
4848
// #endregion fuelCorePort
4949

50+
// #region forcBuildFlags
51+
// Default: []
52+
forcBuildFlags: ['--release'],
53+
// #endregion forcBuildFlags
54+
5055
// #region deployConfig-fn
5156
deployConfig: async (options: ContractDeployOptions) => {
5257
// ability to fetch data remotely

apps/docs/src/guide/cli/commands.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ npx fuels build --deploy
7777
Using the `--deploy` flag will additionally:
7878

7979
1. Auto-start a short-lived `fuel-core` node if _needed_ ([docs](./config-file.md#autostartfuelcore))
80-
2. Run `deploy` on that node
80+
1. Run `deploy` on that node
8181

8282
> _This is useful when working with contracts because a contract's ID is generated only on deployment._
8383

apps/docs/src/guide/cli/config-file.md

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ Port to use when starting a local `fuel-core` node.
9090

9191
<<< @../../../demo-fuels/fuels.config.full.ts#fuelCorePort{ts:line-numbers}
9292

93+
## `forcBuildFlags`
94+
95+
> - _Used by [`fuels build`](./commands.md#fuels-build) and [`fuels deploy`](./commands.md#fuels-deploy)_.
96+
97+
Sway programs are compiled in `debug` mode by default.
98+
99+
Here you can customize all build flags, e.g. to build programs in `release` mode.
100+
101+
<<< @../../../demo-fuels/fuels.config.full.ts#forcBuildFlags{ts:line-numbers}
102+
103+
Check also:
104+
105+
- [Forc docs](https://docs.fuel.network/docs/forc/commands/forc_build/#forc-build)
106+
93107
## `deployConfig`
94108

95109
You can supply a ready-to-go deploy configuration object:

packages/fuels/src/cli/commands/build/buildSwayProgram.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const buildSwayProgram = async (config: FuelsConfig, path: string) => {
1313
const builtInForcPath = findBinPath('fuels-forc', __dirname);
1414

1515
const command = config.useBuiltinForc ? builtInForcPath : 'forc';
16-
const forc = spawn(command, ['build', '-p', path], { stdio: 'pipe' });
16+
const args = ['build', '-p', path].concat(config.forcBuildFlags);
17+
const forc = spawn(command, args, { stdio: 'pipe' });
1718

1819
if (loggingConfig.isLoggingEnabled) {
1920
forc.stderr?.pipe(process.stderr);

packages/fuels/src/cli/commands/build/generateTypes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function generateTypesForProgramType(
1515
) {
1616
debug('Generating types..');
1717

18-
const filepaths = await getABIPaths(paths);
18+
const filepaths = await getABIPaths(paths, config);
1919
const pluralizedDirName = `${String(programType).toLocaleLowerCase()}s`;
2020

2121
runTypegen({

packages/fuels/src/cli/commands/deploy/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export async function deploy(config: FuelsConfig) {
2424

2525
for (let i = 0; i < contractsLen; i++) {
2626
const contractPath = config.contracts[i];
27-
const binaryPath = getBinaryPath(contractPath);
28-
const abiPath = getABIPath(contractPath);
29-
const storageSlotsPath = getStorageSlotsPath(contractPath);
27+
const binaryPath = getBinaryPath(contractPath, config);
28+
const abiPath = getABIPath(contractPath, config);
29+
const storageSlotsPath = getStorageSlotsPath(contractPath, config);
3030
const projectName = getContractName(contractPath);
3131
const contractName = getContractCamelCase(contractPath);
3232
const deployConfig = await getDeployConfig(config.deployConfig, {

packages/fuels/src/cli/config/forcUtils.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import camelCase from 'lodash.camelcase';
33
import { join } from 'path';
44
import toml from 'toml';
55

6+
import type { FuelsConfig } from '../types';
7+
68
export type ForcToml = {
79
project: {
810
authors?: string[];
@@ -70,21 +72,21 @@ export function getContractCamelCase(contractPath: string) {
7072
return camelCase(projectName);
7173
}
7274

73-
export function getBinaryPath(contractPath: string) {
75+
export function getBinaryPath(contractPath: string, { buildMode }: FuelsConfig) {
7476
const projectName = getContractName(contractPath);
75-
return join(contractPath, `/out/debug/${projectName}.bin`);
77+
return join(contractPath, `/out/${buildMode}/${projectName}.bin`);
7678
}
7779

78-
export function getABIPath(contractPath: string) {
80+
export function getABIPath(contractPath: string, { buildMode }: FuelsConfig) {
7981
const projectName = getContractName(contractPath);
80-
return join(contractPath, `/out/debug/${projectName}-abi.json`);
82+
return join(contractPath, `/out/${buildMode}/${projectName}-abi.json`);
8183
}
8284

83-
export function getABIPaths(paths: string[]) {
84-
return Promise.all(paths.map((path) => getABIPath(path)));
85+
export function getABIPaths(paths: string[], config: FuelsConfig) {
86+
return Promise.all(paths.map((path) => getABIPath(path, config)));
8587
}
8688

87-
export const getStorageSlotsPath = (contractPath: string) => {
89+
export const getStorageSlotsPath = (contractPath: string, { buildMode }: FuelsConfig) => {
8890
const projectName = getContractName(contractPath);
89-
return join(contractPath, `/out/debug/${projectName}-storage_slots.json`);
91+
return join(contractPath, `/out/${buildMode}/${projectName}-storage_slots.json`);
9092
};

packages/fuels/src/cli/config/loadConfig.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export async function loadConfig(cwd: string): Promise<FuelsConfig> {
4444
const useBuiltinForc = userConfig.useBuiltinForc ?? shouldUseBuiltinForc();
4545
const useBuiltinFuelCore = userConfig.useBuiltinFuelCore ?? shouldUseBuiltinFuelCore();
4646

47-
// Start clone-object while initializiung optional props
47+
const { forcBuildFlags = [] } = userConfig;
48+
const releaseFlag = forcBuildFlags.find((f) => f === '--release');
49+
const buildMode = releaseFlag ? 'release' : 'debug';
50+
51+
// Start clone-object while initializing optional props
4852
const config: FuelsConfig = {
4953
contracts: [],
5054
scripts: [],
@@ -59,6 +63,8 @@ export async function loadConfig(cwd: string): Promise<FuelsConfig> {
5963
useBuiltinForc,
6064
useBuiltinFuelCore,
6165
configPath,
66+
forcBuildFlags,
67+
buildMode,
6268
};
6369

6470
// Resolve the output path on loaded config

packages/fuels/src/cli/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export type UserFuelsConfig = {
8585
*/
8686
fuelCorePort?: number;
8787

88+
/**
89+
* Aditional forc build flags to be used when compiling contracts.
90+
* Default: []
91+
* Example:
92+
* forcBuildFlags: ['--release'];
93+
*/
94+
forcBuildFlags?: string[];
95+
8896
/**
8997
* Function callback, will be called after a successful run
9098
* @param event - The event that triggered this execution
@@ -112,8 +120,10 @@ export type FuelsConfig = UserFuelsConfig &
112120
| 'useBuiltinFuelCore'
113121
| 'autoStartFuelCore'
114122
| 'providerUrl'
123+
| 'forcBuildFlags'
115124
>
116125
> & {
117126
basePath: string;
118127
configPath: string;
128+
buildMode: 'debug' | 'release';
119129
};

packages/fuels/test/features/build.test.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from 'fs';
1+
import { existsSync, readFileSync, writeFileSync } from 'fs';
22
import { join } from 'path';
33

44
import * as deployMod from '../../src/cli/commands/deploy/index';
@@ -74,7 +74,7 @@ describe(
7474

7575
await runInit({
7676
root: paths.root,
77-
workspace: paths.workspaceDir,
77+
contracts: paths.contractsDir,
7878
output: paths.outputDir,
7979
});
8080

@@ -110,6 +110,44 @@ describe(
110110
expect(deploy).toHaveBeenCalledTimes(1);
111111
expect(killChildProcess).toHaveBeenCalledTimes(1);
112112
});
113+
114+
it("should run `build` with `forcBuildFlags: ['--release']`", async () => {
115+
const { autoStartFuelCore, killChildProcess, deploy } = mockAll();
116+
117+
await runInit({
118+
root: paths.root,
119+
workspace: paths.workspaceDir,
120+
output: paths.outputDir,
121+
});
122+
123+
// inject `forcBuildFlags: ['--release']` in config file
124+
const configFilepath = join(paths.root, 'fuels.config.ts');
125+
const configContents = readFileSync(configFilepath, 'utf-8');
126+
127+
const search = " output: './output',";
128+
const replace = [search, " forcBuildFlags: ['--release'],"].join('\n');
129+
const configContentsNew = configContents.replace(search, replace);
130+
131+
writeFileSync(configFilepath, configContentsNew);
132+
133+
// moving on
134+
await runBuild({ root: paths.root });
135+
136+
const files = [
137+
'contracts/FooBarAbi.hex.ts',
138+
'contracts/FooBarAbi.d.ts',
139+
'contracts/factories/FooBarAbi__factory.ts',
140+
'contracts/index.ts',
141+
'index.ts',
142+
].map((f) => join(paths.outputDir, f));
143+
144+
files.forEach((file) => expect(existsSync(file)).toBeTruthy());
145+
146+
expect(autoStartFuelCore).toHaveBeenCalledTimes(0);
147+
expect(deploy).toHaveBeenCalledTimes(0);
148+
expect(killChildProcess).toHaveBeenCalledTimes(0);
149+
});
113150
},
151+
114152
{ timeout: 180000 }
115153
);

packages/fuels/test/fixtures/fuels.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ export const fuelsConfig: FuelsConfig = {
2525
fuelCorePort: 4000,
2626
providerUrl: FUEL_NETWORK_URL,
2727
configPath: __filename,
28+
forcBuildFlags: [],
29+
buildMode: 'debug',
2830
};

0 commit comments

Comments
 (0)