Skip to content

Commit 1994e28

Browse files
Add support for Bun dependency updates and improve test coverage
1 parent 0e3191e commit 1994e28

4 files changed

Lines changed: 101 additions & 34 deletions

File tree

index.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let { execSync } = require('child_process')
22
let escalade = require('escalade/sync')
33
let { existsSync, readFileSync, writeFileSync } = require('fs')
4-
let { join } = require('path')
4+
let { dirname, join } = require('path')
55
let pico = require('picocolors')
66

77
let { detectEOL, detectIndent } = require('./utils')
@@ -264,6 +264,55 @@ function updatePackageManually(print, lock, latest) {
264264
execSync(del + ' caniuse-lite baseline-browser-mapping')
265265
}
266266

267+
/**
268+
* Bun cannot update a transitive dependency by name: `bun update caniuse-lite`
269+
* adds caniuse-lite to package.json as a new direct dependency at the latest
270+
* version and leaves every nested copy - the ones that actually get resolved at
271+
* runtime - on the old version. A temporary `overrides` entry reaches those,
272+
* and the resolutions survive once it is taken back out.
273+
*/
274+
function updateBun(print, lock, latest) {
275+
let pkgFile = join(dirname(lock.file), 'package.json')
276+
let original = readFileSync(pkgFile)
277+
let pkg = JSON.parse(original.toString())
278+
let withOverride = {
279+
...pkg,
280+
overrides: {
281+
...pkg.overrides,
282+
'baseline-browser-mapping': 'latest',
283+
'caniuse-lite': latest.version
284+
}
285+
}
286+
287+
// The file is restored byte for byte below, so its formatting does not matter.
288+
writeFileSync(pkgFile, JSON.stringify(withOverride, null, 2) + '\n')
289+
290+
print(
291+
'Updating caniuse-lite version\n' +
292+
pico.yellow('$ bun install') +
293+
' (with a temporary caniuse-lite override)\n'
294+
)
295+
try {
296+
execSync('bun install')
297+
} catch (e) /* c8 ignore start */ {
298+
writeFileSync(pkgFile, original)
299+
print(pico.red(e.stdout.toString()))
300+
print(
301+
pico.red(
302+
'\n' +
303+
e.stack +
304+
'\n\n' +
305+
'Problem with `bun install` call. ' +
306+
'Run it manually.\n'
307+
)
308+
)
309+
process.exit(1)
310+
} /* c8 ignore end */
311+
312+
writeFileSync(pkgFile, original)
313+
updateWith(print, 'bun install', 'Removing the temporary override')
314+
}
315+
267316
function updateWith(print, cmd, message = 'Updating caniuse-lite version') {
268317
print(message + '\n' + pico.yellow('$ ' + cmd) + '\n')
269318
try {
@@ -308,7 +357,7 @@ module.exports = function updateDB(print = defaultPrint) {
308357
: 'caniuse-lite'
309358
updateWith(print, 'pnpm up --depth=Infinity --no-save ' + packages)
310359
} else if (lock.mode === 'bun') {
311-
updateWith(print, 'bun update caniuse-lite baseline-browser-mapping')
360+
updateBun(print, lock, latest)
312361
} else if (lock.mode === 'deno') {
313362
updateWith(print, 'deno add npm:caniuse-lite npm:baseline-browser-mapping')
314363
updateWith(

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
},
4343
"devDependencies": {
4444
"@logux/oxc-configs": "^1.1.0",
45-
"actions-up": "^1.16.0",
46-
"browserslist": "^4.28.7",
45+
"actions-up": "^1.17.0",
46+
"browserslist": "^4.28.8",
4747
"c8": "^12.0.0",
4848
"eslint-plugin-prefer-let": "^4.2.2",
4949
"oxlint": "^1.77.0",

pnpm-lock.yaml

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.test.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,37 @@ test('updates caniuse-lite for pnpm', async () => {
311311

312312
if (bunInstalled) {
313313
test('updates caniuse-lite for bun', async () => {
314-
await chdir('update-bun', 'package.json', 'bun.lockb')
314+
let dir = await chdir('update-bun', 'package.json', 'bun.lockb')
315+
let pkgBefore = (await readFile(join(dir, 'package.json'))).toString()
316+
315317
match(
316318
runUpdate(),
317319
`Latest version: ${caniuse.version}\n` +
318320
'Updating caniuse-lite version\n' +
319-
'$ bun update caniuse-lite baseline-browser-mapping\n' +
321+
'$ bun install (with a temporary caniuse-lite override)\n' +
322+
'Removing the temporary override\n' +
323+
'$ bun install\n' +
320324
'caniuse-lite has been successfully updated\n'
321325
)
322326

323327
let dependencies = execSync('bun pm ls --all', {
324328
env: { ...process.env, FORCE_COLOR: '0', NO_COLOR: '1' }
325329
}).toString()
326-
ok(dependencies.includes(`caniuse-lite@${caniuse.version}`))
330+
let versions = [...dependencies.matchAll(/caniuse-lite@(\S+)/g)].map(
331+
i => i[1]
332+
)
333+
334+
// Every copy has to move, including the nested ones. `bun update
335+
// caniuse-lite` only ever added a new hoisted copy at the latest version
336+
// and left those behind on the old one.
337+
ok(versions.length > 0)
338+
equal(
339+
versions.filter(i => i !== caniuse.version),
340+
[]
341+
)
342+
343+
// The override is temporary and must not survive in package.json.
344+
equal((await readFile(join(dir, 'package.json'))).toString(), pkgBefore)
327345
})
328346
}
329347

0 commit comments

Comments
 (0)