From e106db445db4a5259f3cfc69d193744962937fa8 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 27 Dec 2024 15:07:07 +0100 Subject: [PATCH] fix!: use bun as package manager when bun.lock is found --- .../build-info/e2e/browser-compatibility.e2e.ts | 2 +- .../node/__snapshots__/get-build-info.test.ts.snap | 12 +++++++++--- .../build-info/src/node/get-build-info.test.ts | 4 +++- .../detect-package-manager.test.ts | 10 ++++++++++ .../src/package-managers/detect-package-manager.ts | 14 +++++++------- packages/build-info/tests/bin.test.ts | 4 +++- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/build-info/e2e/browser-compatibility.e2e.ts b/packages/build-info/e2e/browser-compatibility.e2e.ts index c99f980dc6..c43d396f81 100644 --- a/packages/build-info/e2e/browser-compatibility.e2e.ts +++ b/packages/build-info/e2e/browser-compatibility.e2e.ts @@ -78,7 +78,7 @@ test('Should detect the package manager', async ({ page }) => { expect(await page.evaluate(() => window.detectPackageManager(new window.project(window.fs, '/')))).toMatchObject({ forceEnvironment: 'NETLIFY_USE_PNPM', installCommand: 'pnpm install', - lockFile: 'pnpm-lock.yaml', + lockFiles: ['pnpm-lock.yaml'], name: 'pnpm', }) }) diff --git a/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap b/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap index 52f1fcf63e..02862f3b95 100644 --- a/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap +++ b/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap @@ -34,7 +34,9 @@ exports[`should retrieve the build info for providing a rootDir 1`] = ` "packageManager": { "forceEnvironment": "NETLIFY_USE_PNPM", "installCommand": "pnpm install", - "lockFile": "pnpm-lock.yaml", + "lockFiles": [ + "pnpm-lock.yaml", + ], "name": "pnpm", "runCommand": "pnpm run", "version": SemVer { @@ -165,7 +167,9 @@ exports[`should retrieve the build info for providing a rootDir and a nested pro "packageManager": { "forceEnvironment": "NETLIFY_USE_PNPM", "installCommand": "pnpm install", - "lockFile": "pnpm-lock.yaml", + "lockFiles": [ + "pnpm-lock.yaml", + ], "name": "pnpm", "runCommand": "pnpm run", "version": SemVer { @@ -239,7 +243,9 @@ exports[`should retrieve the build info for providing a rootDir and the same pro "packageManager": { "forceEnvironment": "NETLIFY_USE_PNPM", "installCommand": "pnpm install", - "lockFile": "pnpm-lock.yaml", + "lockFiles": [ + "pnpm-lock.yaml", + ], "name": "pnpm", "runCommand": "pnpm run", "version": SemVer { diff --git a/packages/build-info/src/node/get-build-info.test.ts b/packages/build-info/src/node/get-build-info.test.ts index 69f945d3f6..7a2ba7b167 100644 --- a/packages/build-info/src/node/get-build-info.test.ts +++ b/packages/build-info/src/node/get-build-info.test.ts @@ -53,7 +53,9 @@ test('should not crash on invalid projects', async (ctx) => { expect(packageManager).toMatchInlineSnapshot(` { "installCommand": "npm install", - "lockFile": "package-lock.json", + "lockFiles": [ + "package-lock.json", + ], "name": "npm", "runCommand": "npm run", } diff --git a/packages/build-info/src/package-managers/detect-package-manager.test.ts b/packages/build-info/src/package-managers/detect-package-manager.test.ts index 5f223c976f..56eca38d94 100644 --- a/packages/build-info/src/package-managers/detect-package-manager.test.ts +++ b/packages/build-info/src/package-managers/detect-package-manager.test.ts @@ -110,6 +110,16 @@ test('should use bun if there is a bun.lockb in the root', async ({ fs }) => { expect(pkgManager?.name).toBe('bun') }) +test('should use bun if there is a bun.lock in the root', async ({ fs }) => { + const cwd = mockFileSystem({ + 'package.json': '{}', + 'bun.lock': '', + }) + const project = new Project(fs, cwd) + const pkgManager = await detectPackageManager(project) + expect(pkgManager?.name).toBe('bun') +}) + test('should use the `packageManager` property to detect yarn', async ({ fs }) => { const cwd = mockFileSystem({ 'package.json': JSON.stringify({ packageManager: 'yarn@3.2.1' }), diff --git a/packages/build-info/src/package-managers/detect-package-manager.ts b/packages/build-info/src/package-managers/detect-package-manager.ts index b63b13aed5..c4d958bb10 100644 --- a/packages/build-info/src/package-managers/detect-package-manager.ts +++ b/packages/build-info/src/package-managers/detect-package-manager.ts @@ -17,8 +17,8 @@ export type PkgManagerFields = { installCommand: string /** The package managers run command prefix */ runCommand: string - /** The lock file a package manager is using */ - lockFile: string + /** The lock files a package manager is using */ + lockFiles: string[] /** Environment variable that can be used to force the usage of a package manager even though there is no lock file or a different lock file */ forceEnvironment?: string /** Flags that should be used for running the installation command */ @@ -34,27 +34,27 @@ export const AVAILABLE_PACKAGE_MANAGERS: Record = name: PkgManager.YARN, installCommand: 'yarn install', runCommand: 'yarn run', - lockFile: 'yarn.lock', + lockFiles: ['yarn.lock'], forceEnvironment: 'NETLIFY_USE_YARN', }, [PkgManager.PNPM]: { name: PkgManager.PNPM, installCommand: 'pnpm install', runCommand: 'pnpm run', - lockFile: 'pnpm-lock.yaml', + lockFiles: ['pnpm-lock.yaml'], forceEnvironment: 'NETLIFY_USE_PNPM', }, [PkgManager.NPM]: { name: PkgManager.NPM, installCommand: 'npm install', runCommand: 'npm run', - lockFile: 'package-lock.json', + lockFiles: ['package-lock.json'], }, [PkgManager.BUN]: { name: PkgManager.BUN, installCommand: 'bun install', runCommand: 'bun run', - lockFile: 'bun.lockb', + lockFiles: ['bun.lockb', 'bun.lock'], }, } @@ -63,7 +63,7 @@ export const AVAILABLE_PACKAGE_MANAGERS: Record = * this is to reduce the complexity in loops */ const lockFileMap = Object.values(AVAILABLE_PACKAGE_MANAGERS).reduce( - (cur, pkgManager) => ({ ...cur, [pkgManager.lockFile]: pkgManager }), + (cur, pkgManager) => pkgManager.lockFiles.reduce((cur, lockFile) => ({ ...cur, [lockFile]: pkgManager }), cur), {} as Record, ) diff --git a/packages/build-info/tests/bin.test.ts b/packages/build-info/tests/bin.test.ts index 40a3b4bbbe..a733b89003 100644 --- a/packages/build-info/tests/bin.test.ts +++ b/packages/build-info/tests/bin.test.ts @@ -55,7 +55,9 @@ test('CLI does not print js-workspaces if given a project without it', async (ct \\"name\\": \\"pnpm\\", \\"installCommand\\": \\"pnpm install\\", \\"runCommand\\": \\"pnpm run\\", - \\"lockFile\\": \\"pnpm-lock.yaml\\", + \\"lockFiles\\": [ + \\"pnpm-lock.yaml\\" + ], \\"forceEnvironment\\": \\"NETLIFY_USE_PNPM\\" } }"