From d14e381c3c12adb0edf4bf23a3207bec02e07960 Mon Sep 17 00:00:00 2001 From: Dhai Date: Thu, 26 Dec 2024 08:07:37 +0530 Subject: [PATCH 01/10] feat: auto-detect package manager in `create fuels` --- .github/workflows/pr-release.yaml | 2 +- .../create-fuels/src/lib/getPackageManager.ts | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index 6eeca1f3f04..593d7fce145 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -8,7 +8,7 @@ jobs: name: "Release PR to npm" runs-on: ubuntu-latest # comment out if:false to enable release PR to npm - if: false + # if: false permissions: write-all steps: - name: Checkout diff --git a/packages/create-fuels/src/lib/getPackageManager.ts b/packages/create-fuels/src/lib/getPackageManager.ts index f1982f13b84..0f67cf348f0 100644 --- a/packages/create-fuels/src/lib/getPackageManager.ts +++ b/packages/create-fuels/src/lib/getPackageManager.ts @@ -25,6 +25,20 @@ export const packageMangers = { }, } as const; +export function getUserPkgManager(): PackageManager { + const userAgent = process.env.npm_config_user_agent || ''; + + if (userAgent.startsWith('pnpm')) { + return 'pnpm'; + } + + if (userAgent.startsWith('bun')) { + return 'bun'; + } + + return 'npm'; +} + export const getPackageManager = (opts: ProgramOptions) => { const packageMangerOpts = { pnpm: opts.pnpm, @@ -42,7 +56,14 @@ export const getPackageManager = (opts: ProgramOptions) => { } if (!packageManager) { - packageManager = 'npm'; // default to npm if the user has not specified a package manager (eg. --pnpm, --bun) + packageManager = getUserPkgManager(); } + + if (!packageManager) { + packageManager = 'npm'; + } + + console.log('packageManager', packageManager); + return packageMangers[packageManager]; }; From 2e7f31d8371efb5282eeaa26f9eb422038d6f99d Mon Sep 17 00:00:00 2001 From: Dhai Date: Thu, 26 Dec 2024 08:27:15 +0530 Subject: [PATCH 02/10] fix tests and docs --- .../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 | 43 +++++++------------ .../create-fuels/src/lib/getPackageManager.ts | 31 +------------ .../create-fuels/src/lib/setupProgram.test.ts | 14 +----- packages/create-fuels/src/lib/setupProgram.ts | 6 --- 7 files changed, 25 insertions(+), 95 deletions(-) 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 ac9b3eb234f..d6cb351ca59 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 +npm create fuels@{{fuels}} ``` ```sh-vue [pnpm] -pnpm create fuels@{{fuels}} --pnpm +pnpm create fuels@{{fuels}} ``` ```sh-vue [bun] -bunx --bun create-fuels@{{fuels}} --bun +bunx --bun create-fuels@{{fuels}} ``` ::: 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 8719900be02..8e949cadc94 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}} --pnpm [project-name] [options] +pnpm create fuels@{{fuels}} [project-name] [options] ``` ```sh-vue [npm] -npm create fuels@{{fuels}} -- --npm [project-name] [options] +npm create fuels@{{fuels}} -- [project-name] [options] ``` ```sh-vue [bun] -bunx --bun create-fuels@{{fuels}} --bun [project-name] [options] +bunx --bun create-fuels@{{fuels}} [project-name] [options] ``` ::: @@ -27,18 +27,6 @@ bunx --bun create-fuels@{{fuels}} --bun [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 6b33723e20a..2b9078f83c6 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(opts); + const packageManager = getPackageManager(); 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 9fb4b0fb780..ba43b24b847 100644 --- a/packages/create-fuels/src/lib/getPackageManager.test.ts +++ b/packages/create-fuels/src/lib/getPackageManager.test.ts @@ -1,16 +1,6 @@ -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'], @@ -28,13 +18,18 @@ const runScenarios: [PackageManager, string][] = [ * @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 }; - const result = getPackageManager(opts); + process.env.npm_config_user_agent = packageManager; + + const result = getPackageManager(); expect(result).toEqual(expectedPackageManager); } @@ -43,7 +38,9 @@ describe('getPackageManager', () => { it.each(installScenarios)( 'should have the correct install commands', (packageManager, expectedInstallCommand) => { - const command = getPackageManager({ [packageManager]: true }); + process.env.npm_config_user_agent = packageManager; + + const command = getPackageManager(); const install = command.install; @@ -54,7 +51,9 @@ describe('getPackageManager', () => { it.each(runScenarios)( 'should have the correct run commands', (packageManager, expectedRunCommand) => { - const command = getPackageManager({ [packageManager]: true }); + process.env.npm_config_user_agent = packageManager; + + const command = getPackageManager(); const run = command.run(runCommand); @@ -62,24 +61,12 @@ describe('getPackageManager', () => { } ); - 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', () => { + it('should default to npm', () => { const packageManager = 'npm'; const expectedPackageManager = packageMangers[packageManager]; - const { warn } = mockAllDeps(); - const opts = {}; - const result = getPackageManager(opts); + const result = getPackageManager(); - expect(warn).not.toBeCalled(); expect(result).toEqual(expectedPackageManager); }); }); diff --git a/packages/create-fuels/src/lib/getPackageManager.ts b/packages/create-fuels/src/lib/getPackageManager.ts index 0f67cf348f0..654b338172e 100644 --- a/packages/create-fuels/src/lib/getPackageManager.ts +++ b/packages/create-fuels/src/lib/getPackageManager.ts @@ -1,7 +1,3 @@ -import { warn } from '../utils/logger'; - -import type { ProgramOptions } from './setupProgram'; - export const availablePackageManagers = ['pnpm', 'npm', 'bun'] as const; export type PackageManager = (typeof availablePackageManagers)[number]; @@ -39,31 +35,8 @@ export function getUserPkgManager(): PackageManager { return 'npm'; } -export const getPackageManager = (opts: ProgramOptions) => { - const packageMangerOpts = { - pnpm: opts.pnpm, - npm: opts.npm, - bun: opts.bun, - }; - - const cliChosenPackageManagerSelected = Object.entries(packageMangerOpts) - .filter(([, v]) => v) - .map(([k]) => k) as PackageManager[]; - - let packageManager: PackageManager | undefined = cliChosenPackageManagerSelected[0]; - if (cliChosenPackageManagerSelected.length > 1) { - warn('More than one package manager was selected.'); - } - - if (!packageManager) { - packageManager = getUserPkgManager(); - } - - if (!packageManager) { - packageManager = 'npm'; - } - - console.log('packageManager', packageManager); +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 642afdce40c..f63913dc072 100644 --- a/packages/create-fuels/src/lib/setupProgram.test.ts +++ b/packages/create-fuels/src/lib/setupProgram.test.ts @@ -6,20 +6,8 @@ import { setupProgram } from './setupProgram'; describe('setupProgram', () => { test('setupProgram takes in args properly', () => { const program = setupProgram(); - program.parse([ - '', - '', - 'test-project-name', - '--template', - 'nextjs', - '--pnpm', - '--npm', - '--bun', - ]); + program.parse(['', '', 'test-project-name', '--template', 'nextjs']); 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 7576240c803..009ba4ce257 100644 --- a/packages/create-fuels/src/lib/setupProgram.ts +++ b/packages/create-fuels/src/lib/setupProgram.ts @@ -10,9 +10,6 @@ export interface ProgramOptions { contract?: boolean; predicate?: boolean; script?: boolean; - pnpm?: boolean; - npm?: boolean; - bun?: boolean; verbose?: boolean; install?: boolean; template?: Template; @@ -22,9 +19,6 @@ 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