From 2cde21b78c673dad91aae04788b9b99aa8d2df97 Mon Sep 17 00:00:00 2001 From: Esteban Pastorino Date: Fri, 29 Sep 2023 16:54:09 +0100 Subject: [PATCH] feat: detect bun as package manager --- .../detect-package-manager.test.ts | 26 +++++++++++++++++-- .../detect-package-manager.ts | 7 +++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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 9186d9d860..b44fffa716 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 @@ -39,20 +39,22 @@ test('should repsect the NETLIFY_USE_PNPM if no lock file is there', async ({ fs expect(pkgManager?.name).toBe('pnpm') }) -test('should favor the NETLIFY_USE_PNPM over a yarn.lock file', async ({ fs }) => { +test('should favor the NETLIFY_USE_PNPM over another lockfile', async ({ fs }) => { const cwd = mockFileSystem({ 'package.json': '{}', 'yarn.lock': '', + 'bun.lockb': '', }) const project = new Project(fs, cwd).setEnvironment({ NETLIFY_USE_PNPM: 'true' }) const pkgManager = await detectPackageManager(project) expect(pkgManager?.name).toBe('pnpm') }) -test('should favor the NETLIFY_USE_YARN over a package-lock.json file', async ({ fs }) => { +test('should favor the NETLIFY_USE_YARN over another lockfile', async ({ fs }) => { const cwd = mockFileSystem({ 'package.json': '{}', 'package-lock.json': '', + 'bun.lockb': '', }) const project = new Project(fs, cwd).setEnvironment({ NETLIFY_USE_YARN: 'true' }) const pkgManager = await detectPackageManager(project) @@ -98,6 +100,16 @@ test('should use pnpm if there is a pnpm-lock.yaml in the root', async ({ fs }) expect(pkgManager?.name).toBe('pnpm') }) +test('should use bun if there is a bun.lockb in the root', async ({ fs }) => { + const cwd = mockFileSystem({ + 'package.json': '{}', + 'bun.lockb': '', + }) + 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' }), @@ -107,6 +119,16 @@ test('should use the `packageManager` property to detect yarn', async ({ fs }) = expect(pkgManager?.name).toBe('yarn') }) +test('should use the prefer the `packageManager` property over a lockfile', async ({ fs }) => { + const cwd = mockFileSystem({ + 'package.json': JSON.stringify({ packageManager: 'yarn@3.2.1' }), + 'bun.lockb': '', + }) + const project = new Project(fs, cwd) + const pkgManager = await detectPackageManager(project) + expect(pkgManager?.name).toBe('yarn') +}) + describe('workspaces package manager detection', () => { test('should use pnpm if there is a pnpm-lock.yaml in the workspace root', async ({ fs }) => { const cwd = mockFileSystem({ 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 76b294eaa4..60cfb3ccad 100644 --- a/packages/build-info/src/package-managers/detect-package-manager.ts +++ b/packages/build-info/src/package-managers/detect-package-manager.ts @@ -6,6 +6,7 @@ import type { Project } from '../project.js' export const enum PkgManager { YARN = 'yarn', PNPM = 'pnpm', + BUN = 'bun', NPM = 'npm', } @@ -43,6 +44,12 @@ export const AVAILABLE_PACKAGE_MANAGERS: Record = lockFile: 'pnpm-lock.yaml', forceEnvironment: 'NETLIFY_USE_PNPM', }, + [PkgManager.BUN]: { + name: PkgManager.BUN, + installCommand: 'bun install', + runCommand: 'bun run', + lockFile: 'bun.lockb', + }, [PkgManager.NPM]: { name: PkgManager.NPM, installCommand: 'npm install',