Skip to content

Commit

Permalink
fix: paths and globals in fuels init (#3510)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arboleya authored Jan 2, 2025
1 parent 4c5da1d commit 11fcd7b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-toys-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

fix: paths and globals in `fuels init`
17 changes: 11 additions & 6 deletions packages/fuels/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@ export const configureCli = () => {

let command: Command;

const desc = `Relative path/globals to `;
const arg = `<path|global>`;

(command = program.command(Commands.init))
.description('Create a sample `fuel.config.ts` file')
.addOption(pathOption)
.option('-w, --workspace <path>', '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 <path>', 'Relative dir path for Typescript generation output')
.option('--forc-path <path>', 'Path to the `forc` binary')
.option('--fuel-core-path <path>', 'Path to the `fuel-core` binary')
Expand Down
49 changes: 49 additions & 0 deletions packages/fuels/test/features/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions packages/fuels/test/utils/runCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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),
Expand Down

0 comments on commit 11fcd7b

Please sign in to comment.