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

feat: add more program options to create-fuels CLI #1691

Merged
merged 32 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f480c71
wip
Dhaiwat10 Jan 26, 2024
b64e4cb
add prompt and enable pr release
Dhaiwat10 Jan 28, 2024
7d8c417
fix forc formatting
Dhaiwat10 Jan 28, 2024
984a8e5
add changeset
Dhaiwat10 Jan 28, 2024
0130a4b
disable instructions
Dhaiwat10 Jan 28, 2024
5240d06
fix test
Dhaiwat10 Jan 28, 2024
00bd912
disable pr release
Dhaiwat10 Jan 28, 2024
ad65808
add testing for all possible permutations
Dhaiwat10 Jan 28, 2024
65ed41c
remove unused dep
Dhaiwat10 Jan 29, 2024
ff947b8
update lockfile
Dhaiwat10 Jan 29, 2024
a6d1774
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Jan 29, 2024
b52258c
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Jan 31, 2024
fb85523
fix: typo
nedsalk Feb 5, 2024
a7c0569
Update .changeset/unlucky-eggs-design.md
Dhaiwat10 Feb 5, 2024
8fa87b1
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Feb 5, 2024
80f9cc8
refactor processWorkspaceToml
Dhaiwat10 Feb 6, 2024
7d67572
refactor cli code
Dhaiwat10 Feb 6, 2024
1b8d701
modify template to include UIs for scripts and predicates
Dhaiwat10 Feb 7, 2024
fdc8e0e
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Feb 7, 2024
a27cb27
Update pr-release.yaml
Dhaiwat10 Feb 7, 2024
3926b53
remove fuel-ui since its deprecated
Dhaiwat10 Feb 7, 2024
83ea512
fixes
Dhaiwat10 Feb 7, 2024
aee3423
fix
Dhaiwat10 Feb 7, 2024
21e871e
another fix
Dhaiwat10 Feb 7, 2024
a862cde
hopefully this fixes the test :3
Dhaiwat10 Feb 7, 2024
2deee06
disable pr release
Dhaiwat10 Feb 7, 2024
7d2c131
improve error logging
Dhaiwat10 Feb 7, 2024
b7c11bc
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Feb 7, 2024
797e2ac
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Feb 9, 2024
fec22dd
remove unnecssary txParams
Dhaiwat10 Feb 9, 2024
207bd0d
Merge branch 'master' into dp/scaffold-cli-program-options
arboleya Feb 14, 2024
61699a0
Merge branch 'master' into dp/scaffold-cli-program-options
Dhaiwat10 Feb 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-eggs-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": minor
---

Add more program optoins to the `create-fuels` CLI. Users can now get a multiselect choice to choose out of contract, script & predicate.
1 change: 1 addition & 0 deletions packages/create-fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prepublishOnly": "cp -r ../../templates . && tsx ./scripts/rewriteTemplatePackageJson.ts"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"chalk": "4",
"commander": "^9.4.1",
"fuels": "workspace:*",
Expand Down
77 changes: 75 additions & 2 deletions packages/create-fuels/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-console */
import toml from '@iarna/toml';
import chalk from 'chalk';
import { execSync } from 'child_process';
import { Command } from 'commander';
import { existsSync } from 'fs';
import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { cp, mkdir, rename } from 'fs/promises';
import { join } from 'path';
import prompts from 'prompts';
Expand All @@ -13,13 +14,49 @@ const log = (...data: unknown[]) => {
process.stdout.write(`${data.join(' ')}\n`);
};

type ProgramsToInclude = {
contract: boolean;
predicate: boolean;
script: boolean;
};

const processWorkspaceToml = (fileContents: string, programsToInclude: ProgramsToInclude) => {
const parsed = toml.parse(fileContents) as {
workspace: {
members: string[];
};
};

parsed.workspace.members = parsed.workspace.members.filter((member) => {
if (member === 'predicate' && !programsToInclude.predicate) {
return false;
}
if (member === 'contract' && !programsToInclude.contract) {
return false;
}
if (member === 'script' && !programsToInclude.script) {
return false;
}
return true;
});

return toml.stringify(parsed);
};

export const runScaffoldCli = async (
explicitProjectPath?: string,
explicitPackageManger?: string,
shouldInstallDeps = true
shouldInstallDeps = true,
explicitProgramsToInclude?: ProgramsToInclude
) => {
let projectPath = explicitProjectPath || '';
let packageManager = explicitPackageManger || '';
let programsToInclude: ProgramsToInclude = explicitProgramsToInclude || {
contract: true,
predicate: false,
script: false,
};

const program = new Command(packageJson.name).version(packageJson.version);

if (!explicitProjectPath) {
Expand Down Expand Up @@ -61,11 +98,47 @@ export const runScaffoldCli = async (
packageManager = packageManagerInput.packageManager;
}

if (!explicitProgramsToInclude) {
const programsToIncludeInput = await prompts({
type: 'multiselect',
name: 'programsToInclude',
message: 'Which Sway programs do you want?',
choices: [
{ title: 'Contract', value: 'contract', selected: true },
{ title: 'Predicate', value: 'predicate' },
{ title: 'Script', value: 'script' },
],
instructions: false,
});
programsToInclude = {
contract: programsToIncludeInput.programsToInclude.includes('contract'),
predicate: programsToIncludeInput.programsToInclude.includes('predicate'),
script: programsToIncludeInput.programsToInclude.includes('script'),
};
}

await mkdir(projectPath);

await cp(join(__dirname, '../templates/nextjs'), projectPath, { recursive: true });
await rename(join(projectPath, 'gitignore'), join(projectPath, '.gitignore'));

// delete the programs that are not to be included
if (!programsToInclude.contract) {
rmSync(join(projectPath, 'sway-programs/contract'), { recursive: true });
}
if (!programsToInclude.predicate) {
rmSync(join(projectPath, 'sway-programs/predicate'), { recursive: true });
}
if (!programsToInclude.script) {
rmSync(join(projectPath, 'sway-programs/script'), { recursive: true });
}

// remove the programs that are not included from the Forc.toml members field and rewrite the file
const forcTomlPath = join(projectPath, 'sway-programs', 'Forc.toml');
const forcTomlContents = readFileSync(forcTomlPath, 'utf-8');
const newForcTomlContents = processWorkspaceToml(forcTomlContents, programsToInclude);
writeFileSync(forcTomlPath, newForcTomlContents);

if (shouldInstallDeps) {
process.chdir(projectPath);
execSync(`${packageManager} install`, { stdio: 'inherit' });
Expand Down
67 changes: 40 additions & 27 deletions packages/create-fuels/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,47 @@ import { join } from 'path';

import { runScaffoldCli } from '../src/cli';

const possibleProgramsToInclude = [
{ contract: true, predicate: false, script: false },
{ contract: false, predicate: true, script: false },
{ contract: false, predicate: false, script: true },
{ contract: true, predicate: true, script: false },
{ contract: true, predicate: false, script: true },
{ contract: false, predicate: true, script: true },
{ contract: true, predicate: true, script: true },
];

/**
* @group node
*/
test('create-fuels extracts the template to the specified directory', async () => {
// move the templates folder from the root of the project to the root of the create-fuels package temporarily.
// this is needed because of the way the create-fuels package is setup.
// it expects the templates folder to be in the root of the create-fuels package. we move it there in the prepublishOnly script
await cp(join(__dirname, '../../../templates'), join(__dirname, '../templates'), {
recursive: true,
});

await runScaffoldCli('test-project', 'pnpm', false);

// check if the template was extracted to the test-project directory.
// compare the templates/nextjs folder with the test-project folder recursively
const originalTemplateFiles = await fs.readdir(join(__dirname, '../templates/nextjs'));

// remove the gitignore file from the array because it is not copied to the test-project folder
const gitIgnoreIndex = originalTemplateFiles.indexOf('gitignore');
if (gitIgnoreIndex > -1) {
originalTemplateFiles.splice(gitIgnoreIndex, 1);
test.each(possibleProgramsToInclude)(
'create-fuels extracts the template to the specified directory',
async (programsToInclude) => {
// move the templates folder from the root of the project to the root of the create-fuels package temporarily.
// this is needed because of the way the create-fuels package is setup.
// it expects the templates folder to be in the root of the create-fuels package. we move it there in the prepublishOnly script
await cp(join(__dirname, '../../../templates'), join(__dirname, '../templates'), {
recursive: true,
});

await runScaffoldCli('test-project', 'pnpm', false, programsToInclude);

// check if the template was extracted to the test-project directory.
// compare the templates/nextjs folder with the test-project folder recursively
const originalTemplateFiles = await fs.readdir(join(__dirname, '../templates/nextjs'));

// remove the gitignore file from the array because it is not copied to the test-project folder
const gitIgnoreIndex = originalTemplateFiles.indexOf('gitignore');
if (gitIgnoreIndex > -1) {
originalTemplateFiles.splice(gitIgnoreIndex, 1);
}

const testProjectFiles = await fs.readdir('test-project');

expect(originalTemplateFiles).toEqual(testProjectFiles);

// cleanup - delete the test project and the templates folder
await fs.rm(join(__dirname, '../templates'), { recursive: true });
await fs.rm('test-project', { recursive: true });
}

const testProjectFiles = await fs.readdir('test-project');

expect(originalTemplateFiles).toEqual(testProjectFiles);

// cleanup - delete the test project and the templates folder
await fs.rm(join(__dirname, '../templates'), { recursive: true });
await fs.rm('test-project', { recursive: true });
});
);
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion templates/nextjs/fuels.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createConfig } from 'fuels';

export default createConfig({
contracts: ['sway-contracts'],
workspace: './sway-programs',
output: './src/sway-api',
});
2 changes: 0 additions & 2 deletions templates/nextjs/sway-contracts/.gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions templates/nextjs/sway-programs/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["contract", "predicate", "script"]
2 changes: 2 additions & 0 deletions templates/nextjs/sway-programs/contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
2 changes: 2 additions & 0 deletions templates/nextjs/sway-programs/predicate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions templates/nextjs/sway-programs/predicate/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "test-predicate"

[dependencies]
5 changes: 5 additions & 0 deletions templates/nextjs/sway-programs/predicate/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
predicate;

fn main() -> bool {
return true;
}
2 changes: 2 additions & 0 deletions templates/nextjs/sway-programs/script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions templates/nextjs/sway-programs/script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "test-script"

[dependencies]
5 changes: 5 additions & 0 deletions templates/nextjs/sway-programs/script/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script;

fn main() -> bool {
return true;
}
Loading