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

chore: remove some choices from create-fuels CLI #2444

Merged
merged 27 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
46049e8
remove some choices
Dhaiwat10 Jun 4, 2024
bb6bd42
enable pr release
Dhaiwat10 Jun 4, 2024
86dc280
delete unnecessary files, make npm default
Dhaiwat10 Jun 12, 2024
597810a
Merge branch 'master' into dp/create-fuels-remove-extra-choices
Dhaiwat10 Jun 12, 2024
33c51d6
fix merge conflicts
Dhaiwat10 Jun 12, 2024
95b1615
remove old exports
Dhaiwat10 Jun 12, 2024
e377d6b
fix lint errors
Dhaiwat10 Jun 12, 2024
297dbbe
add changeset
Dhaiwat10 Jun 12, 2024
5e1770f
fix changeset
Dhaiwat10 Jun 12, 2024
347dfee
fix test
Dhaiwat10 Jun 12, 2024
7ed719d
fix lint errors
Dhaiwat10 Jun 12, 2024
2916ba3
fix lint error
Dhaiwat10 Jun 12, 2024
60f7fd8
Merge branch 'master' into dp/create-fuels-remove-extra-choices
Dhaiwat10 Jun 13, 2024
d956767
remove outdated test
Dhaiwat10 Jun 13, 2024
e78406a
Merge branch 'dp/create-fuels-remove-extra-choices' of https://github…
Dhaiwat10 Jun 13, 2024
d75c0cb
fix failing test, remove unnecessary awaits
Dhaiwat10 Jun 13, 2024
e330021
remove asyncs
Dhaiwat10 Jun 13, 2024
a34e74e
Merge branch 'master' into dp/create-fuels-remove-extra-choices
Dhaiwat10 Jun 17, 2024
8f42989
Merge branch 'master' into dp/create-fuels-remove-extra-choices
Dhaiwat10 Jun 18, 2024
1af40ca
remove from docs
Dhaiwat10 Jun 18, 2024
c90a345
remove env vars, fix up tests
Dhaiwat10 Jun 18, 2024
e5a8da5
disable pr release
Dhaiwat10 Jun 18, 2024
e13eef3
Merge branch 'master' into dp/create-fuels-remove-extra-choices
petertonysmith94 Jun 19, 2024
2280d6c
Merge branch 'master' into dp/create-fuels-remove-extra-choices
maschad Jun 19, 2024
0b149d2
Merge branch 'master' into dp/create-fuels-remove-extra-choices
petertonysmith94 Jun 20, 2024
8a23dbb
Merge branch 'master' into dp/create-fuels-remove-extra-choices
petertonysmith94 Jun 20, 2024
e183684
Merge branch 'master' into dp/create-fuels-remove-extra-choices
petertonysmith94 Jun 20, 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/quiet-toes-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": patch
---

chore: remove some choices from `create-fuels` CLI
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ If you run your frontend now, you should be able to interact with your contract
To deploy your frontend to the cloud, you can use any service like [Vercel](https://vercel.com/). Make sure that you setup your environment variables correctly and that your contract ID is correct. Your environment variables should look something like this:

```md
NEXT_PUBLIC_HAS_CONTRACT=true
NEXT_PUBLIC_DAPP_ENVIRONMENT=testnet
NEXT_PUBLIC_TESTNET_CONTRACT_ID=0x8342d413de2a678245d9ee39f020795800c7e6a4ac5ff7daae275f533dc05e08

Expand Down
16 changes: 0 additions & 16 deletions apps/docs/src/guide/creating-a-fuel-dapp/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ bunx --bun create-fuels@{{fuels}} [project-name] [options]

:::

## `-c, --contract`

Notifies the tool to include a Sway contract program in your project.

## `-p, --predicate`

Notifies the tool to include a Sway predicate program in your project.

## `-s, --script`

Notifies the tool to include a Sway script program in your project.

## `--pnpm`

Notifies the tool to use pnpm as the package manager to install the necessary dependencies.
Expand All @@ -47,10 +35,6 @@ Notifies the tool to use npm as the package manager to install the necessary dep

Notifies the tool to use bun as the package manager to install the necessary dependencies.

## `-cs, -cp, -sp, -cps`

Shorthand to include a combination of contract, script and predicate programs.

## `--verbose`

Enables verbose logging. Useful when debugging issues with the tool.
Expand Down
3 changes: 0 additions & 3 deletions packages/create-fuels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ To learn how to deploy your Fuel dApp to the testnet, you can follow our [Deploy
- To make changes to the template locally, you will need to have a `.env.local` file in the `templates/nextjs` directory with the following contents:

```bash
NEXT_PUBLIC_HAS_CONTRACT=true
NEXT_PUBLIC_HAS_PREDICATE=true
NEXT_PUBLIC_HAS_SCRIPT=true
NEXT_PUBLIC_FUEL_NETWORK_URL=http://127.0.0.1:4000/v1/graphql
```

Expand Down
73 changes: 8 additions & 65 deletions packages/create-fuels/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import toml from '@iarna/toml';
import chalk from 'chalk';
import { execSync } from 'child_process';
import type { Command } from 'commander';
import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { cp, mkdir, rename } from 'fs/promises';
import ora from 'ora';
import { join } from 'path';
Expand All @@ -12,35 +12,23 @@ import { tryInstallFuelUp } from './lib';
import { getPackageManager } from './lib/getPackageManager';
import { getPackageVersion } from './lib/getPackageVersion';
import type { ProgramOptions } from './lib/setupProgram';
import { promptForProgramsToInclude, promptForProjectPath } from './prompts';
import { promptForProjectPath } from './prompts';
import { error, log } from './utils/logger';

export { setupProgram } from './lib/setupProgram';

export type ProgramsToInclude = Pick<ProgramOptions, 'contract' | 'predicate' | 'script'>;

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

parsed.workspace.members = parsed.workspace.members.filter((m) => programsToInclude[m]);

return toml.stringify(parsed);
};

function writeEnvFile(envFilePath: string, programsToInclude: ProgramsToInclude) {
/*
* Should be like:
* NEXT_PUBLIC_HAS_CONTRACT=true
* NEXT_PUBLIC_HAS_PREDICATE=false
* NEXT_PUBLIC_HAS_SCRIPT=true
*/
let newFileContents = Object.entries(programsToInclude)
.map(([program, include]) => `NEXT_PUBLIC_HAS_${program.toUpperCase()}=${include}`)
.join('\n');
function writeEnvFile(envFilePath: string) {
let newFileContents = '';

newFileContents += `\nNEXT_PUBLIC_FUEL_NODE_PORT=4000`;
newFileContents += `\nNEXT_PUBLIC_DAPP_ENVIRONMENT=local`;
Expand All @@ -52,20 +40,18 @@ export const runScaffoldCli = async ({
program,
args = process.argv,
shouldInstallDeps = false,
forceDisablePrompts = false,
}: {
program: Command;
args: string[];
shouldInstallDeps?: boolean;
forceDisablePrompts?: boolean;
}) => {
program.parse(args);

let projectPath = program.args[0] ?? (await promptForProjectPath());

const opts = program.opts<ProgramOptions>();
const verboseEnabled = opts.verbose ?? false;
const packageManager = await getPackageManager(opts);
const packageManager = getPackageManager(opts);

if (!process.env.VITEST) {
await tryInstallFuelUp(verboseEnabled);
Expand Down Expand Up @@ -93,35 +79,6 @@ export const runScaffoldCli = async ({
projectPath = await promptForProjectPath();
}

const cliProgramsToInclude = {
contract: opts.contract,
predicate: opts.predicate,
script: opts.script,
};
const hasAnyCliProgramsToInclude = Object.values(cliProgramsToInclude).some((v) => v);

let programsToInclude: ProgramsToInclude;
if (hasAnyCliProgramsToInclude) {
programsToInclude = cliProgramsToInclude;
} else {
programsToInclude = await promptForProgramsToInclude({
forceDisablePrompts,
});
}

while (!programsToInclude.contract && !programsToInclude.predicate && !programsToInclude.script) {
error('You must include at least one Sway program.');

// Exit the program if we are testing to prevent hanging
if (process.env.VITEST) {
throw new Error();
}

programsToInclude = await promptForProgramsToInclude({
forceDisablePrompts,
});
}

const fileCopySpinner = ora({
text: 'Copying template files..',
color: 'green',
Expand All @@ -135,25 +92,11 @@ export const runScaffoldCli = async ({
});
await rename(join(projectPath, 'gitignore'), join(projectPath, '.gitignore'));
await rename(join(projectPath, 'env'), join(projectPath, '.env.local'));
writeEnvFile(join(projectPath, '.env.local'), programsToInclude);

// 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 });
rmSync(join(projectPath, 'src/pages/predicate.tsx'), { recursive: true });
}
if (!programsToInclude.script) {
rmSync(join(projectPath, 'sway-programs/script'), { recursive: true });
rmSync(join(projectPath, 'src/pages/script.tsx'), { recursive: true });
}
writeEnvFile(join(projectPath, '.env.local'));

// 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);
const newForcTomlContents = processWorkspaceToml(forcTomlContents);
writeFileSync(forcTomlPath, newForcTomlContents);

// Rewrite the package.json file
Expand Down
48 changes: 13 additions & 35 deletions packages/create-fuels/src/lib/getPackageManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { mockLogger } from '../../test/utils/mockLogger';
import * as promptsMod from '../prompts';

import type { PackageManager } from './getPackageManager';
import { availablePackageManagers, getPackageManager, packageMangers } from './getPackageManager';

const mockAllDeps = (opts: { packageManager?: PackageManager } = {}) => {
const mockAllDeps = () => {
const { warn } = mockLogger();
const promptForPackageManager = vi
.spyOn(promptsMod, 'promptForPackageManager')
.mockResolvedValue(opts.packageManager);

return {
warn,
promptForPackageManager,
};
};

Expand All @@ -35,20 +30,20 @@ const runScenarios: [PackageManager, string][] = [
describe('getPackageManager', () => {
it.each(availablePackageManagers)(
`should get the correct package manager for %s`,
async (packageManager: PackageManager) => {
(packageManager: PackageManager) => {
const expectedPackageManager = packageMangers[packageManager];
const opts = { [packageManager]: true };

const result = await getPackageManager(opts);
const result = getPackageManager(opts);

expect(result).toEqual(expectedPackageManager);
}
);

it.each(installScenarios)(
'should have the correct install commands',
async (packageManager, expectedInstallCommand) => {
const command = await getPackageManager({ [packageManager]: true });
(packageManager, expectedInstallCommand) => {
const command = getPackageManager({ [packageManager]: true });

const install = command.install;

Expand All @@ -58,50 +53,33 @@ describe('getPackageManager', () => {

it.each(runScenarios)(
'should have the correct run commands',
async (packageManager, expectedRunCommand) => {
const command = await getPackageManager({ [packageManager]: true });
(packageManager, expectedRunCommand) => {
const command = getPackageManager({ [packageManager]: true });

const run = command.run(runCommand);

expect(run).toEqual(expectedRunCommand);
}
);

it('should warn the user if more than one package manager selected', async () => {
const { warn, promptForPackageManager } = mockAllDeps();
it('should warn the user if more than one package manager selected', () => {
const { warn } = mockAllDeps();
const opts = { pnpm: true, npm: true };

await getPackageManager(opts);
getPackageManager(opts);

expect(promptForPackageManager).toBeCalled();
expect(warn).toBeCalledWith('More than one package manager was selected.');
});

it('should allow inputting of a package manager via prompt', async () => {
it('should default to npm if no package manager is selected', () => {
const packageManager = 'npm';
const expectedPackageManager = packageMangers[packageManager];
const { warn, promptForPackageManager } = mockAllDeps({
packageManager,
});
const { warn } = mockAllDeps();
const opts = {};

const result = await getPackageManager(opts);
const result = getPackageManager(opts);

expect(warn).not.toBeCalled();
expect(promptForPackageManager).toBeCalled();
expect(result).toEqual(expectedPackageManager);
});

it('should default to pnpm if no package manager is selected', async () => {
const packageManager = 'pnpm';
const expectedPackageManager = packageMangers[packageManager];
const { warn, promptForPackageManager } = mockAllDeps();
const opts = {};

const result = await getPackageManager(opts);

expect(warn).not.toBeCalled();
expect(promptForPackageManager).toBeCalled();
expect(result).toEqual(expectedPackageManager);
});
});
8 changes: 2 additions & 6 deletions packages/create-fuels/src/lib/getPackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { promptForPackageManager } from '../prompts';
import { warn } from '../utils/logger';

import type { ProgramOptions } from './setupProgram';
Expand Down Expand Up @@ -26,7 +25,7 @@ export const packageMangers = {
},
} as const;

export const getPackageManager = async (opts: ProgramOptions) => {
export const getPackageManager = (opts: ProgramOptions) => {
const packageMangerOpts = {
pnpm: opts.pnpm,
npm: opts.npm,
Expand All @@ -41,12 +40,9 @@ export const getPackageManager = async (opts: ProgramOptions) => {
if (cliChosenPackageManagerSelected.length > 1) {
warn('More than one package manager was selected.');
}
if (cliChosenPackageManagerSelected.length !== 1) {
packageManager = await promptForPackageManager();
}

if (!packageManager) {
packageManager = 'pnpm';
packageManager = 'npm'; // default to npm if the user has not specified a package manager (eg. --pnpm, --bun)
}
return packageMangers[packageManager];
};
20 changes: 5 additions & 15 deletions packages/create-fuels/src/lib/setupProgram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,18 @@ import { setupProgram } from './setupProgram';
describe('setupProgram', () => {
test('setupProgram takes in args properly', () => {
const program = setupProgram();
program.parse(['', '', 'test-project-name', '-c', '-p', '-s', '--pnpm', '--npm']);
program.parse(['', '', 'test-project-name', '--pnpm', '--npm']);
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
expect(program.args[0]).toBe('test-project-name');
expect(program.opts().contract).toBe(true);
expect(program.opts().predicate).toBe(true);
expect(program.opts().script).toBe(true);
expect(program.opts().pnpm).toBe(true);
expect(program.opts().npm).toBe(true);
});

test('setupProgram takes in combined args properly', () => {
const program = setupProgram();
program.parse(['', '', '-cps']);
expect(program.opts().contract).toBe(true);
expect(program.opts().predicate).toBe(true);
expect(program.opts().script).toBe(true);
expect(program.opts().bun).toBe(undefined);
});

test('setupProgram - no args', () => {
const program = setupProgram();
program.parse([]);
expect(program.opts().contract).toBe(undefined);
expect(program.opts().predicate).toBe(undefined);
expect(program.opts().script).toBe(undefined);
expect(program.opts().pnpm).toBe(undefined);
expect(program.opts().npm).toBe(undefined);
expect(program.opts().bun).toBe(undefined);
});
});
9 changes: 3 additions & 6 deletions packages/create-fuels/src/lib/setupProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ export const setupProgram = () => {
const program = new Command(packageJson.name)
.version(packageJson.version)
.arguments('[projectDirectory]')
.option('-c, --contract', 'Include contract program')
.option('-p, --predicate', 'Include predicate program')
.option('-s, --script', 'Include script program')
.option('--pnpm', 'Use pnpm as the package manager')
.option('--npm', 'Use npm as the package manager')
.option('--bun', 'Use bun as the package manager')
.option('--pnpm', 'Use pnpm to install dependencies')
.option('--npm', 'Use npm to install dependencies')
.option('--bun', 'Use bun to install dependencies')
arboleya marked this conversation as resolved.
Show resolved Hide resolved
.option('--verbose', 'Enable verbose logging')
.addHelpCommand()
.showHelpAfterError(true);
Expand Down
2 changes: 0 additions & 2 deletions packages/create-fuels/src/prompts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './promptFuelUpInstall';
export * from './promptForProjectPath';
export * from './promptForPackageManager';
export * from './promptForProgramsToInclude';
Loading
Loading