From be53f89fc5970e43d262d742c532130e91ad4b27 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Jan 2025 06:45:09 -0300 Subject: [PATCH 1/2] Revert "feat: auto-detect package manager in `create fuels` (#3503)" This reverts commit e8a8c7168a459f7572a9c70ed0e50086a6e4ca28. --- .changeset/lazy-ears-tap.md | 5 -- .../src/guide/creating-a-fuel-dapp/index.md | 6 +-- .../src/guide/creating-a-fuel-dapp/options.md | 18 +++++-- packages/create-fuels/src/cli.ts | 2 +- .../src/lib/getPackageManager.test.ts | 49 ++++++++++--------- .../create-fuels/src/lib/getPackageManager.ts | 46 +++++++---------- .../create-fuels/src/lib/setupProgram.test.ts | 14 +++++- packages/create-fuels/src/lib/setupProgram.ts | 6 +++ packages/create-fuels/test/cli.test.ts | 5 +- .../create-fuels/test/utils/generateArgs.ts | 1 + 10 files changed, 82 insertions(+), 70 deletions(-) delete mode 100644 .changeset/lazy-ears-tap.md diff --git a/.changeset/lazy-ears-tap.md b/.changeset/lazy-ears-tap.md deleted file mode 100644 index a25355967f5..00000000000 --- a/.changeset/lazy-ears-tap.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"create-fuels": patch ---- - -feat: auto-detect package manager in `create fuels` diff --git a/apps/docs/src/guide/creating-a-fuel-dapp/index.md b/apps/docs/src/guide/creating-a-fuel-dapp/index.md index 1f5e6954ee6..ac9b3eb234f 100644 --- a/apps/docs/src/guide/creating-a-fuel-dapp/index.md +++ b/apps/docs/src/guide/creating-a-fuel-dapp/index.md @@ -20,15 +20,15 @@ The first step is to run the command: ::: code-group ```sh-vue [npm] -npm create fuels@{{fuels}} +npm create fuels@{{fuels}} -- --npm ``` ```sh-vue [pnpm] -pnpm create fuels@{{fuels}} +pnpm create fuels@{{fuels}} --pnpm ``` ```sh-vue [bun] -bun create fuels@{{fuels}} +bunx --bun create-fuels@{{fuels}} --bun ``` ::: diff --git a/apps/docs/src/guide/creating-a-fuel-dapp/options.md b/apps/docs/src/guide/creating-a-fuel-dapp/options.md index 0e016e8b6ac..8719900be02 100644 --- a/apps/docs/src/guide/creating-a-fuel-dapp/options.md +++ b/apps/docs/src/guide/creating-a-fuel-dapp/options.md @@ -10,15 +10,15 @@ The `npm create fuels` command has several command-line options that you can use ::: code-group ```sh-vue [pnpm] -pnpm create fuels@{{fuels}} [project-name] [options] +pnpm create fuels@{{fuels}} --pnpm [project-name] [options] ``` ```sh-vue [npm] -npm create fuels@{{fuels}} -- [project-name] [options] +npm create fuels@{{fuels}} -- --npm [project-name] [options] ``` ```sh-vue [bun] -bun create fuels@{{fuels}} [project-name] [options] +bunx --bun create-fuels@{{fuels}} --bun [project-name] [options] ``` ::: @@ -27,6 +27,18 @@ bun create fuels@{{fuels}} [project-name] [options] Specifies the template to use for your project. The available templates are: `vite` and `nextjs`. The default template is `vite`. +## `--pnpm` + +Notifies the tool to use pnpm as the package manager to install the necessary dependencies. + +## `--npm` + +Notifies the tool to use npm as the package manager to install the necessary dependencies. + +## `--bun` + +Notifies the tool to use bun as the package manager to install the necessary dependencies. + ## `--verbose` Enables verbose logging. Useful when debugging issues with the tool. diff --git a/packages/create-fuels/src/cli.ts b/packages/create-fuels/src/cli.ts index 2b9078f83c6..6b33723e20a 100644 --- a/packages/create-fuels/src/cli.ts +++ b/packages/create-fuels/src/cli.ts @@ -63,7 +63,7 @@ export const runScaffoldCli = async ({ let projectPath = program.args[0] ?? (await promptForProjectPath()); const verboseEnabled = opts.verbose ?? false; - const packageManager = getPackageManager(); + const packageManager = getPackageManager(opts); if (!process.env.VITEST) { await tryInstallFuelUp(verboseEnabled); diff --git a/packages/create-fuels/src/lib/getPackageManager.test.ts b/packages/create-fuels/src/lib/getPackageManager.test.ts index 5eab1e42f71..9fb4b0fb780 100644 --- a/packages/create-fuels/src/lib/getPackageManager.test.ts +++ b/packages/create-fuels/src/lib/getPackageManager.test.ts @@ -3,6 +3,14 @@ import { mockLogger } from '../../test/utils/mockLogger'; import type { PackageManager } from './getPackageManager'; import { availablePackageManagers, getPackageManager, packageMangers } from './getPackageManager'; +const mockAllDeps = () => { + const { warn } = mockLogger(); + + return { + warn, + }; +}; + const installScenarios: [PackageManager, string][] = [ ['pnpm', 'pnpm install'], ['npm', 'npm install'], @@ -16,30 +24,17 @@ const runScenarios: [PackageManager, string][] = [ ['bun', 'bun run fuels:dev'], ]; -const mockAllDeps = () => { - const { warn } = mockLogger(); - - return { - warn, - }; -}; - /** * @group node */ describe('getPackageManager', () => { - beforeEach(() => { - delete process.env.npm_config_user_agent; - }); - it.each(availablePackageManagers)( `should get the correct package manager for %s`, (packageManager: PackageManager) => { const expectedPackageManager = packageMangers[packageManager]; + const opts = { [packageManager]: true }; - process.env.npm_config_user_agent = packageManager; - - const result = getPackageManager(); + const result = getPackageManager(opts); expect(result).toEqual(expectedPackageManager); } @@ -48,9 +43,7 @@ describe('getPackageManager', () => { it.each(installScenarios)( 'should have the correct install commands', (packageManager, expectedInstallCommand) => { - process.env.npm_config_user_agent = packageManager; - - const command = getPackageManager(); + const command = getPackageManager({ [packageManager]: true }); const install = command.install; @@ -61,9 +54,7 @@ describe('getPackageManager', () => { it.each(runScenarios)( 'should have the correct run commands', (packageManager, expectedRunCommand) => { - process.env.npm_config_user_agent = packageManager; - - const command = getPackageManager(); + const command = getPackageManager({ [packageManager]: true }); const run = command.run(runCommand); @@ -71,14 +62,24 @@ describe('getPackageManager', () => { } ); - it('should default to npm', () => { + it('should warn the user if more than one package manager selected', () => { + const { warn } = mockAllDeps(); + const opts = { pnpm: true, npm: true }; + + getPackageManager(opts); + + expect(warn).toBeCalledWith('More than one package manager was selected.'); + }); + + it('should default to npm if no package manager is selected', () => { const packageManager = 'npm'; const expectedPackageManager = packageMangers[packageManager]; const { warn } = mockAllDeps(); + const opts = {}; - const result = getPackageManager(); + const result = getPackageManager(opts); + expect(warn).not.toBeCalled(); expect(result).toEqual(expectedPackageManager); - expect(warn).toHaveBeenCalledWith(`This package manager is not supported. Using npm instead.`); }); }); diff --git a/packages/create-fuels/src/lib/getPackageManager.ts b/packages/create-fuels/src/lib/getPackageManager.ts index fcbc60fc84d..f1982f13b84 100644 --- a/packages/create-fuels/src/lib/getPackageManager.ts +++ b/packages/create-fuels/src/lib/getPackageManager.ts @@ -1,5 +1,7 @@ import { warn } from '../utils/logger'; +import type { ProgramOptions } from './setupProgram'; + export const availablePackageManagers = ['pnpm', 'npm', 'bun'] as const; export type PackageManager = (typeof availablePackageManagers)[number]; @@ -8,53 +10,39 @@ const runnableApplicator = (command: string = '') => `${commandPrefix} ${command}`; -export const packageMangers: Record< - PackageManager, - { - install: string; - run: (command: string) => string; - name: PackageManager; - } -> = { +export const packageMangers = { pnpm: { install: 'pnpm install', run: runnableApplicator('pnpm'), - name: 'pnpm', }, npm: { install: 'npm install', run: runnableApplicator('npm run'), - name: 'npm', }, bun: { install: 'bun install', run: runnableApplicator('bun run'), - name: 'bun', }, } as const; -export function getUserPkgManager(): PackageManager { - const userAgent = process.env.npm_config_user_agent || ''; +export const getPackageManager = (opts: ProgramOptions) => { + const packageMangerOpts = { + pnpm: opts.pnpm, + npm: opts.npm, + bun: opts.bun, + }; - if (userAgent.startsWith(packageMangers.pnpm.name)) { - return packageMangers.pnpm.name; - } + const cliChosenPackageManagerSelected = Object.entries(packageMangerOpts) + .filter(([, v]) => v) + .map(([k]) => k) as PackageManager[]; - if (userAgent.startsWith(packageMangers.bun.name)) { - return packageMangers.bun.name; + let packageManager: PackageManager | undefined = cliChosenPackageManagerSelected[0]; + if (cliChosenPackageManagerSelected.length > 1) { + warn('More than one package manager was selected.'); } - if (userAgent.startsWith(packageMangers.npm.name)) { - return packageMangers.npm.name; + if (!packageManager) { + packageManager = 'npm'; // default to npm if the user has not specified a package manager (eg. --pnpm, --bun) } - - warn(`This package manager is not supported. Using npm instead.`); - - return packageMangers.npm.name; -} - -export const getPackageManager = () => { - const packageManager = getUserPkgManager(); - return packageMangers[packageManager]; }; diff --git a/packages/create-fuels/src/lib/setupProgram.test.ts b/packages/create-fuels/src/lib/setupProgram.test.ts index f63913dc072..642afdce40c 100644 --- a/packages/create-fuels/src/lib/setupProgram.test.ts +++ b/packages/create-fuels/src/lib/setupProgram.test.ts @@ -6,8 +6,20 @@ import { setupProgram } from './setupProgram'; describe('setupProgram', () => { test('setupProgram takes in args properly', () => { const program = setupProgram(); - program.parse(['', '', 'test-project-name', '--template', 'nextjs']); + program.parse([ + '', + '', + 'test-project-name', + '--template', + 'nextjs', + '--pnpm', + '--npm', + '--bun', + ]); expect(program.args[0]).toBe('test-project-name'); + expect(program.opts().pnpm).toBe(true); + expect(program.opts().npm).toBe(true); + expect(program.opts().bun).toBe(true); expect(program.opts().install).toBe(true); expect(program.opts().template).toBe('nextjs'); }); diff --git a/packages/create-fuels/src/lib/setupProgram.ts b/packages/create-fuels/src/lib/setupProgram.ts index 009ba4ce257..7576240c803 100644 --- a/packages/create-fuels/src/lib/setupProgram.ts +++ b/packages/create-fuels/src/lib/setupProgram.ts @@ -10,6 +10,9 @@ export interface ProgramOptions { contract?: boolean; predicate?: boolean; script?: boolean; + pnpm?: boolean; + npm?: boolean; + bun?: boolean; verbose?: boolean; install?: boolean; template?: Template; @@ -19,6 +22,9 @@ export const setupProgram = () => { const program = new Command(packageJson.name) .version(packageJson.version) .arguments('[projectDirectory]') + .option('--pnpm', 'Use pnpm to install dependencies') + .option('--npm', 'Use npm to install dependencies') + .option('--bun', 'Use bun to install dependencies') .option('--verbose', 'Enable verbose logging') .option('--no-install', 'Do not install dependencies') .option('--template