From 11fcd7b48246f49f4074f2cf0d689ce5b83fd399 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Jan 2025 06:16:07 -0300 Subject: [PATCH] fix: paths and globals in `fuels init` (#3510) * Parsing args as an array of paths/globals * Handling multiple paths in test utility * Testing multilple paths in `fuel init` command * Adding changeset * Tyop * Updating changeset --- .changeset/new-toys-approve.md | 5 +++ packages/fuels/src/cli.ts | 17 +++++--- packages/fuels/test/features/init.test.ts | 49 +++++++++++++++++++++++ packages/fuels/test/utils/runCommands.ts | 14 ++++--- 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 .changeset/new-toys-approve.md diff --git a/.changeset/new-toys-approve.md b/.changeset/new-toys-approve.md new file mode 100644 index 00000000000..02b6c5574f0 --- /dev/null +++ b/.changeset/new-toys-approve.md @@ -0,0 +1,5 @@ +--- +"fuels": patch +--- + +fix: paths and globals in `fuels init` diff --git a/packages/fuels/src/cli.ts b/packages/fuels/src/cli.ts index 522665cd7f8..eb02468b6e7 100644 --- a/packages/fuels/src/cli.ts +++ b/packages/fuels/src/cli.ts @@ -46,16 +46,21 @@ export const configureCli = () => { let command: Command; - const desc = `Relative path/globals to `; - const arg = ``; - (command = program.command(Commands.init)) .description('Create a sample `fuel.config.ts` file') .addOption(pathOption) .option('-w, --workspace ', 'Relative dir path to Forc workspace') - .addOption(new Option(`-c, --contracts ${arg}`, `${desc} Contracts`).conflicts('workspace')) - .addOption(new Option(`-s, --scripts ${arg}`, `${desc} Scripts`).conflicts('workspace')) - .addOption(new Option(`-p, --predicates ${arg}`, `${desc} Predicates`).conflicts('workspace')) + .addOption( + new Option(`-c, --contracts [paths...]`, `Relative paths to Contracts`).conflicts('workspace') + ) + .addOption( + new Option(`-s, --scripts [paths...]`, `Relative paths to Scripts`).conflicts('workspace') + ) + .addOption( + new Option(`-p, --predicates [paths...]`, `Relative paths to Predicates`).conflicts( + 'workspace' + ) + ) .requiredOption('-o, --output ', 'Relative dir path for Typescript generation output') .option('--forc-path ', 'Path to the `forc` binary') .option('--fuel-core-path ', 'Path to the `fuel-core` binary') diff --git a/packages/fuels/test/features/init.test.ts b/packages/fuels/test/features/init.test.ts index 4c7be45929f..f55d512d68b 100644 --- a/packages/fuels/test/features/init.test.ts +++ b/packages/fuels/test/features/init.test.ts @@ -46,6 +46,55 @@ describe('init', () => { expect(fuelsContents).not.toMatch(`fuelCorePath: 'fuels-core',`); }); + it('should run `init` command with --contracts', async () => { + await runInit({ + root: paths.root, + contracts: [paths.contractsBarDir, paths.contractsFooDir], + output: paths.outputDir, + }); + + const [relativeBarDir, relativeFooDir] = [ + paths.contractsBarDir.replace(paths.workspaceDir, 'workspace'), + paths.contractsFooDir.replace(paths.workspaceDir, 'workspace'), + ]; + + expect(existsSync(paths.fuelsConfigPath)).toBeTruthy(); + const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8'); + expect(fuelsContents).toMatch(/contracts:/); + expect(fuelsContents).toMatch(relativeBarDir); + expect(fuelsContents).toMatch(relativeFooDir); + }); + + it('should run `init` command with --predicates', async () => { + await runInit({ + root: paths.root, + predicates: paths.predicateDir, + output: paths.outputDir, + }); + + const relativePredicateDir = paths.predicateDir.replace(paths.workspaceDir, 'workspace'); + + expect(existsSync(paths.fuelsConfigPath)).toBeTruthy(); + const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8'); + expect(fuelsContents).toMatch(/predicates:/); + expect(fuelsContents).toMatch(relativePredicateDir); + }); + + it('should run `init` command with --scripts', async () => { + await runInit({ + root: paths.root, + scripts: paths.scriptsDir, + output: paths.outputDir, + }); + + const relativeScriptDir = paths.scriptsDir.replace(paths.workspaceDir, 'workspace'); + + expect(existsSync(paths.fuelsConfigPath)).toBeTruthy(); + const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8'); + expect(fuelsContents).toMatch(/scripts:/); + expect(fuelsContents).toMatch(relativeScriptDir); + }); + it('should run `init` command using custom binaries', async () => { await runInit({ root: paths.root, diff --git a/packages/fuels/test/utils/runCommands.ts b/packages/fuels/test/utils/runCommands.ts index 3558d04eb97..b1f10eab728 100644 --- a/packages/fuels/test/utils/runCommands.ts +++ b/packages/fuels/test/utils/runCommands.ts @@ -47,7 +47,7 @@ export function bootstrapProject(testFilepath: string) { const upgradableChunkedContractPath = join(contractsDir, 'upgradable-chunked'); const scriptsDir = join(workspaceDir, 'scripts'); - const predicateDir = join(workspaceDir, 'predicate'); + const predicateDir = join(workspaceDir, 'predicates'); const outputDir = join(root, 'output'); const outputContractsDir = join(outputDir, 'contracts'); @@ -93,9 +93,9 @@ export type BaseParams = { export type InitParams = BaseParams & { workspace?: string; - contracts?: string; - scripts?: string; - predicates?: string; + contracts?: string | string[]; + scripts?: string | string[]; + predicates?: string | string[]; output: string; forcPath?: string; fuelCorePath?: string; @@ -122,8 +122,10 @@ export async function runInit(params: InitParams) { privateKey, } = params; - const flag = (flags: (string | undefined)[], value?: string | boolean): string[] => - value ? (flags as string[]) : []; + const flag = ( + flags: (string | string[] | undefined)[], + value?: string | string[] | boolean + ): string[] => (value ? (flags.flat() as string[]) : []); const flags = [ flag(['--path', root], root),