diff --git a/README.md b/README.md index 7cf783a7..b8823ad4 100644 --- a/README.md +++ b/README.md @@ -604,7 +604,7 @@ Specifies the package manager to use when looking up versions. npmSystem-installed npm. Default. yarnSystem-installed yarn. Automatically used if yarn.lock is present. pnpmSystem-installed pnpm. Automatically used if pnpm-lock.yaml is present. - bunSystem-installed bun. Automatically used if bun.lockb is present. + bunSystem-installed bun. Automatically used if bun.lock or bun.lockb is present. ## peer diff --git a/src/cli-options.ts b/src/cli-options.ts index b9182a6a..df2c300b 100755 --- a/src/cli-options.ts +++ b/src/cli-options.ts @@ -436,7 +436,7 @@ const extendedHelpPackageManager: ExtendedHelp = ({ markdown }) => { ['npm', `System-installed npm. Default.`], ['yarn', `System-installed yarn. Automatically used if yarn.lock is present.`], ['pnpm', `System-installed pnpm. Automatically used if pnpm-lock.yaml is present.`], - ['bun', `System-installed bun. Automatically used if bun.lockb is present.`], + ['bun', `System-installed bun. Automatically used if bun.lock or bun.lockb is present.`], ], }) diff --git a/src/lib/doctor.ts b/src/lib/doctor.ts index 8c2a5663..6e3a9d0e 100644 --- a/src/lib/doctor.ts +++ b/src/lib/doctor.ts @@ -91,13 +91,15 @@ const loadPackageFileForDoctor = async (options: Options): Promise // we have to pass run directly since it would be a circular require if doctor included this file const doctor = async (run: Run, options: Options): Promise => { await chalkInit() - const lockFileName = + + // bun lockFileName defaults to bun.lock but will be overwritten to bun.lockb if detected at the readFile step below + let lockFileName: 'package-lock.json' | 'yarn.lock' | 'pnpm-lock.yaml' | 'bun.lock' | 'bun.lockb' = options.packageManager === 'yarn' ? 'yarn.lock' : options.packageManager === 'pnpm' ? 'pnpm-lock.yaml' : options.packageManager === 'bun' - ? 'bun.lockb' + ? 'bun.lock' : 'package-lock.json' const { pkg, pkgFile }: PackageInfo = await loadPackageFileForDoctor(options) @@ -163,7 +165,16 @@ const doctor = async (run: Run, options: Options): Promise => { let lockFile = '' try { lockFile = await fs.readFile(lockFileName, 'utf-8') - } catch (e) {} + } catch (e) { + // try bun.lockb if bun.lock was not found + // set lockFileName so the rest of doctor mode uses bun.lockb for lock file updating and restoration + if (options.packageManager === 'bun') { + lockFileName = 'bun.lockb' + try { + lockFile = await fs.readFile(lockFileName, 'utf-8') + } catch (e) {} + } + } // make sure current tests pass before we begin try { diff --git a/src/lib/findLockfile.ts b/src/lib/findLockfile.ts index d303fc28..8badf04f 100644 --- a/src/lib/findLockfile.ts +++ b/src/lib/findLockfile.ts @@ -34,6 +34,8 @@ export default async function findLockfile( return { directoryPath: currentPath, filename: 'deno.jsonc' } } else if (files.includes('bun.lockb')) { return { directoryPath: currentPath, filename: 'bun.lockb' } + } else if (files.includes('bun.lock')) { + return { directoryPath: currentPath, filename: 'bun.lock' } } const pathParent = path.resolve(currentPath, '..') diff --git a/test/determinePackageManager.test.ts b/test/determinePackageManager.test.ts index fe798fb4..302a0395 100644 --- a/test/determinePackageManager.test.ts +++ b/test/determinePackageManager.test.ts @@ -27,6 +27,27 @@ describe('determinePackageManager', () => { packageManager.should.equal('bun') }) + it('returns bun if bun.lock exists in cwd', async () => { + /** Mock for filesystem calls. */ + function readdirMock(path: string): Promise { + switch (path) { + case '/home/test-repo': + case 'C:\\home\\test-repo': + return Promise.resolve(['bun.lock']) + } + + throw new Error(`Mock cannot handle path: ${path}.`) + } + + const packageManager = await determinePackageManager( + { + cwd: isWindows ? 'C:\\home\\test-repo' : '/home/test-repo', + }, + readdirMock, + ) + packageManager.should.equal('bun') + }) + it('returns bun if bun.lockb exists in an ancestor directory', async () => { /** Mock for filesystem calls. */ function readdirMock(path: string): Promise {