Skip to content

Commit

Permalink
Simplify pnpm update of caniuse-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Dec 20, 2021
1 parent f2b63c7 commit 32c0ae7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 68 deletions.
22 changes: 7 additions & 15 deletions test/update-db/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,18 @@ if (!NODE_8 && !NODE_10) {
}

if (!NODE_8 && !NODE_10 && isInstalled('pnpm')) {
let versions = ['1.0.30001000', '1.0.1234', '1.0.3000', '1.0.30001035']

test('updates caniuse-lite for pnpm', async () => {
let dir = await chdir('update-pnpm', 'package.json', 'pnpm-lock.yaml')
checkRunUpdateContents(versions.sort().join(', '), 'pnpm')

let lock = (await readFile(join(dir, 'pnpm-lock.yaml'))).toString()
match(lock, `/caniuse-lite/${caniuse.version}:`)
})

test('skips the pnpm update if caniuse-lite is up to date', async () => {
let dir = await chdir('update-pnpm', 'package.json', 'pnpm-lock.yaml')
match(
runUpdate(),
`Latest version: ${caniuse.version}\n` +
'Updating caniuse-lite version\n' +
'$ pnpm up caniuse-lite\n' +
'caniuse-lite has been successfully updated\n'
)

checkRunUpdateContents(versions.sort().join(', '), 'pnpm')
let lock = (await readFile(join(dir, 'pnpm-lock.yaml'))).toString()
match(lock, `/caniuse-lite/${caniuse.version}:`)

checkRunUpdateNoChanges()
lock = (await readFile(join(dir, 'pnpm-lock.yaml'))).toString()
match(lock, `/caniuse-lite/${caniuse.version}:`)
})
}

Expand Down
55 changes: 2 additions & 53 deletions update-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,64 +170,11 @@ function updateYarnLockfile(lock, latest) {
return { content: content, versions: versions }
}

function updatePnpmLockfile(lock, latest) {
var versions = {}
var lines = lock.content.split('\n')
var i
var j
var lineParts

for (i = 0; i < lines.length; i++) {
if (lines[i].indexOf('caniuse-lite:') >= 0) {
lineParts = lines[i].split(/:\s?/, 2)
if (lineParts[1].indexOf('/') >= 0) {
/* c8 ignore start */
var sublineParts = lineParts[1].split(/([/:])/)
for (j = 0; j < sublineParts.length; j++) {
if (sublineParts[j].indexOf('caniuse-lite') >= 0) {
versions[sublineParts[j + 2]] = true
sublineParts[j + 2] = latest.version
break
}
}
lineParts[1] = sublineParts.join('')
/* c8 ignore stop */
} else {
versions[lineParts[1]] = true
}
lines[i] = lineParts[0] + ': ' + latest.version
} else if (lines[i].indexOf('/caniuse-lite') >= 0) {
lineParts = lines[i].split(/([/:])/)
for (j = 0; j < lineParts.length; j++) {
if (lineParts[j].indexOf('caniuse-lite') >= 0) {
versions[lineParts[j + 2]] = true
lineParts[j + 2] = latest.version
break
}
}
lines[i] = lineParts.join('')
for (i = i + 1; i < lines.length; i++) {
if (lines[i].indexOf('integrity: ') !== -1) {
lines[i] = lines[i].replace(
/integrity: .+/,
'integrity: ' + latest.dist.integrity
)
} else if (lines[i].indexOf(' /') !== -1) {
break
}
}
}
}
return { content: lines.join('\n'), versions: versions }
}

function updateLockfile(lock, latest) {
if (!lock.content) lock.content = fs.readFileSync(lock.file).toString()

if (lock.mode === 'yarn') {
return updateYarnLockfile(lock, latest)
} else if (lock.mode === 'pnpm') {
return updatePnpmLockfile(lock, latest)
} else {
return updateNpmLockfile(lock, latest)
}
Expand Down Expand Up @@ -328,6 +275,8 @@ module.exports = function updateDB(print) {

if (lock.mode === 'yarn' && lock.version !== 1) {
updateWith(print, 'yarn up -R caniuse-lite')
} else if (lock.mode === 'pnpm') {
updateWith(print, 'pnpm up caniuse-lite')
} else {
updatePackageManually(print, lock, latest)
}
Expand Down

0 comments on commit 32c0ae7

Please sign in to comment.