Skip to content

Commit

Permalink
Add support for bun.lock (#1483).
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Dec 20, 2024
1 parent 89d235b commit f86fd02
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ Specifies the package manager to use when looking up versions.
<tr><td>npm</td><td>System-installed npm. Default.</td></tr>
<tr><td>yarn</td><td>System-installed yarn. Automatically used if yarn.lock is present.</td></tr>
<tr><td>pnpm</td><td>System-installed pnpm. Automatically used if pnpm-lock.yaml is present.</td></tr>
<tr><td>bun</td><td>System-installed bun. Automatically used if bun.lockb is present.</td></tr>
<tr><td>bun</td><td>System-installed bun. Automatically used if bun.lock or bun.lockb is present.</td></tr>
</table>
## peer
Expand Down
2 changes: 1 addition & 1 deletion src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`],
],
})

Expand Down
17 changes: 14 additions & 3 deletions src/lib/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ const loadPackageFileForDoctor = async (options: Options): Promise<PackageInfo>
// 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<void> => {
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)

Expand Down Expand Up @@ -163,7 +165,16 @@ const doctor = async (run: Run, options: Options): Promise<void> => {
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 {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/findLockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '..')
Expand Down
21 changes: 21 additions & 0 deletions test/determinePackageManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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<string[]> {
Expand Down

0 comments on commit f86fd02

Please sign in to comment.