2023-08-17 09:47:40 +00:00
|
|
|
|
let { writeFileSync, readFileSync, existsSync } = require('fs')
|
|
|
|
|
let { execSync } = require('child_process')
|
|
|
|
|
let { join } = require('path')
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let escalade = require('escalade/sync')
|
|
|
|
|
let pico = require('picocolors')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
2022-09-15 12:58:37 +00:00
|
|
|
|
const { detectIndent, detectEOL } = require('./utils')
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
function BrowserslistUpdateError(message) {
|
|
|
|
|
this.name = 'BrowserslistUpdateError'
|
|
|
|
|
this.message = message
|
|
|
|
|
this.browserslist = true
|
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
|
Error.captureStackTrace(this, BrowserslistUpdateError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BrowserslistUpdateError.prototype = Error.prototype
|
|
|
|
|
|
|
|
|
|
/* c8 ignore next 3 */
|
|
|
|
|
function defaultPrint(str) {
|
|
|
|
|
process.stdout.write(str)
|
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
|
|
function detectLockfile() {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let packageDir = escalade('.', (dir, names) => {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return names.indexOf('package.json') !== -1 ? dir : ''
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!packageDir) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
throw new BrowserslistUpdateError(
|
2022-01-21 08:28:41 +00:00
|
|
|
|
'Cannot find package.json. ' +
|
2022-07-11 11:25:19 +00:00
|
|
|
|
'Is this the right directory to run `npx update-browserslist-db` in?'
|
2022-01-21 08:28:41 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
|
let lockfileNpm = join(packageDir, 'package-lock.json')
|
|
|
|
|
let lockfileShrinkwrap = join(packageDir, 'npm-shrinkwrap.json')
|
|
|
|
|
let lockfileYarn = join(packageDir, 'yarn.lock')
|
|
|
|
|
let lockfilePnpm = join(packageDir, 'pnpm-lock.yaml')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
|
if (existsSync(lockfilePnpm)) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return { mode: 'pnpm', file: lockfilePnpm }
|
2023-08-17 09:47:40 +00:00
|
|
|
|
} else if (existsSync(lockfileNpm)) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return { mode: 'npm', file: lockfileNpm }
|
2023-08-17 09:47:40 +00:00
|
|
|
|
} else if (existsSync(lockfileYarn)) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let lock = { mode: 'yarn', file: lockfileYarn }
|
2023-08-17 09:47:40 +00:00
|
|
|
|
lock.content = readFileSync(lock.file).toString()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
lock.version = /# yarn lockfile v1/.test(lock.content) ? 1 : 2
|
|
|
|
|
return lock
|
2023-08-17 09:47:40 +00:00
|
|
|
|
} else if (existsSync(lockfileShrinkwrap)) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return { mode: 'npm', file: lockfileShrinkwrap }
|
|
|
|
|
}
|
2022-07-11 11:25:19 +00:00
|
|
|
|
throw new BrowserslistUpdateError(
|
2022-01-21 08:28:41 +00:00
|
|
|
|
'No lockfile found. Run "npm install", "yarn install" or "pnpm install"'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLatestInfo(lock) {
|
|
|
|
|
if (lock.mode === 'yarn') {
|
|
|
|
|
if (lock.version === 1) {
|
2023-08-17 09:47:40 +00:00
|
|
|
|
return JSON.parse(execSync('yarn info caniuse-lite --json').toString())
|
|
|
|
|
.data
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
return JSON.parse(
|
2023-08-17 09:47:40 +00:00
|
|
|
|
execSync('yarn npm info caniuse-lite --json').toString()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 08:26:02 +00:00
|
|
|
|
if (lock.mode === 'pnpm') {
|
2023-08-17 09:47:40 +00:00
|
|
|
|
return JSON.parse(execSync('pnpm info caniuse-lite --json').toString())
|
2022-10-10 08:26:02 +00:00
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
return JSON.parse(execSync('npm show caniuse-lite --json').toString())
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
function getBrowsers() {
|
|
|
|
|
let browserslist = require('browserslist')
|
|
|
|
|
return browserslist().reduce((result, entry) => {
|
|
|
|
|
if (!result[entry[0]]) {
|
|
|
|
|
result[entry[0]] = []
|
|
|
|
|
}
|
|
|
|
|
result[entry[0]].push(entry[1])
|
|
|
|
|
return result
|
|
|
|
|
}, {})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
function diffBrowsers(old, current) {
|
|
|
|
|
let browsers = Object.keys(old).concat(
|
|
|
|
|
Object.keys(current).filter(browser => old[browser] === undefined)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
)
|
|
|
|
|
return browsers
|
2022-07-11 11:25:19 +00:00
|
|
|
|
.map(browser => {
|
|
|
|
|
let oldVersions = old[browser] || []
|
|
|
|
|
let currentVersions = current[browser] || []
|
|
|
|
|
let common = oldVersions.filter(v => currentVersions.includes(v))
|
|
|
|
|
let added = currentVersions.filter(v => !common.includes(v))
|
|
|
|
|
let removed = oldVersions.filter(v => !common.includes(v))
|
|
|
|
|
return removed
|
|
|
|
|
.map(v => pico.red('- ' + browser + ' ' + v))
|
|
|
|
|
.concat(added.map(v => pico.green('+ ' + browser + ' ' + v)))
|
2022-01-21 08:28:41 +00:00
|
|
|
|
})
|
2022-07-11 11:25:19 +00:00
|
|
|
|
.reduce((result, array) => result.concat(array), [])
|
2022-01-21 08:28:41 +00:00
|
|
|
|
.join('\n')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateNpmLockfile(lock, latest) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let metadata = { latest, versions: [] }
|
|
|
|
|
let content = deletePackage(JSON.parse(lock.content), metadata)
|
2022-09-15 12:58:37 +00:00
|
|
|
|
metadata.content = JSON.stringify(content, null, detectIndent(lock.content))
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return metadata
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deletePackage(node, metadata) {
|
|
|
|
|
if (node.dependencies) {
|
|
|
|
|
if (node.dependencies['caniuse-lite']) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let version = node.dependencies['caniuse-lite'].version
|
2022-01-21 08:28:41 +00:00
|
|
|
|
metadata.versions[version] = true
|
|
|
|
|
delete node.dependencies['caniuse-lite']
|
|
|
|
|
}
|
2022-07-11 11:25:19 +00:00
|
|
|
|
for (let i in node.dependencies) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
node.dependencies[i] = deletePackage(node.dependencies[i], metadata)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
if (node.packages) {
|
|
|
|
|
for (let path in node.packages) {
|
|
|
|
|
if (path.endsWith('/caniuse-lite')) {
|
|
|
|
|
metadata.versions[node.packages[path].version] = true
|
|
|
|
|
delete node.packages[path]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let yarnVersionRe = /version "(.*?)"/
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
|
|
function updateYarnLockfile(lock, latest) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let blocks = lock.content.split(/(\n{2,})/).map(block => {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
return block.split('\n')
|
|
|
|
|
})
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let versions = {}
|
|
|
|
|
blocks.forEach(lines => {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
if (lines[0].indexOf('caniuse-lite@') !== -1) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let match = yarnVersionRe.exec(lines[1])
|
2022-01-21 08:28:41 +00:00
|
|
|
|
versions[match[1]] = true
|
|
|
|
|
if (match[1] !== latest.version) {
|
|
|
|
|
lines[1] = lines[1].replace(
|
|
|
|
|
/version "[^"]+"/,
|
|
|
|
|
'version "' + latest.version + '"'
|
|
|
|
|
)
|
|
|
|
|
lines[2] = lines[2].replace(
|
|
|
|
|
/resolved "[^"]+"/,
|
|
|
|
|
'resolved "' + latest.dist.tarball + '"'
|
|
|
|
|
)
|
|
|
|
|
if (lines.length === 4) {
|
|
|
|
|
lines[3] = latest.dist.integrity
|
|
|
|
|
? lines[3].replace(
|
|
|
|
|
/integrity .+/,
|
|
|
|
|
'integrity ' + latest.dist.integrity
|
|
|
|
|
)
|
|
|
|
|
: ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let content = blocks.map(lines => lines.join('\n')).join('')
|
|
|
|
|
return { content, versions }
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateLockfile(lock, latest) {
|
2023-08-17 09:47:40 +00:00
|
|
|
|
if (!lock.content) lock.content = readFileSync(lock.file).toString()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
2022-09-15 12:58:37 +00:00
|
|
|
|
let updatedLockFile
|
2022-01-21 08:28:41 +00:00
|
|
|
|
if (lock.mode === 'yarn') {
|
2022-09-15 12:58:37 +00:00
|
|
|
|
updatedLockFile = updateYarnLockfile(lock, latest)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} else {
|
2022-09-15 12:58:37 +00:00
|
|
|
|
updatedLockFile = updateNpmLockfile(lock, latest)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
2022-09-15 12:58:37 +00:00
|
|
|
|
updatedLockFile.content = updatedLockFile.content.replace(
|
|
|
|
|
/\n/g,
|
|
|
|
|
detectEOL(lock.content)
|
|
|
|
|
)
|
|
|
|
|
return updatedLockFile
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updatePackageManually(print, lock, latest) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let lockfileData = updateLockfile(lock, latest)
|
|
|
|
|
let caniuseVersions = Object.keys(lockfileData.versions).sort()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
if (caniuseVersions.length === 1 && caniuseVersions[0] === latest.version) {
|
|
|
|
|
print(
|
|
|
|
|
'Installed version: ' +
|
2023-08-17 09:47:40 +00:00
|
|
|
|
pico.bold(pico.green(caniuseVersions[0])) +
|
2022-01-21 08:28:41 +00:00
|
|
|
|
'\n' +
|
|
|
|
|
pico.bold(pico.green('caniuse-lite is up to date')) +
|
|
|
|
|
'\n'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (caniuseVersions.length === 0) {
|
|
|
|
|
caniuseVersions[0] = 'none'
|
|
|
|
|
}
|
|
|
|
|
print(
|
|
|
|
|
'Installed version' +
|
|
|
|
|
(caniuseVersions.length === 1 ? ': ' : 's: ') +
|
|
|
|
|
pico.bold(pico.red(caniuseVersions.join(', '))) +
|
|
|
|
|
'\n' +
|
|
|
|
|
'Removing old caniuse-lite from lock file\n'
|
|
|
|
|
)
|
2023-08-17 09:47:40 +00:00
|
|
|
|
writeFileSync(lock.file, lockfileData.content)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let install = lock.mode === 'yarn' ? 'yarn add -W' : lock.mode + ' install'
|
2022-01-21 08:28:41 +00:00
|
|
|
|
print(
|
|
|
|
|
'Installing new caniuse-lite version\n' +
|
|
|
|
|
pico.yellow('$ ' + install + ' caniuse-lite') +
|
|
|
|
|
'\n'
|
|
|
|
|
)
|
|
|
|
|
try {
|
2023-08-17 09:47:40 +00:00
|
|
|
|
execSync(install + ' caniuse-lite')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} catch (e) /* c8 ignore start */ {
|
|
|
|
|
print(
|
|
|
|
|
pico.red(
|
|
|
|
|
'\n' +
|
|
|
|
|
e.stack +
|
|
|
|
|
'\n\n' +
|
|
|
|
|
'Problem with `' +
|
|
|
|
|
install +
|
|
|
|
|
' caniuse-lite` call. ' +
|
|
|
|
|
'Run it manually.\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
} /* c8 ignore end */
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let del = lock.mode === 'yarn' ? 'yarn remove -W' : lock.mode + ' uninstall'
|
2022-01-21 08:28:41 +00:00
|
|
|
|
print(
|
|
|
|
|
'Cleaning package.json dependencies from caniuse-lite\n' +
|
|
|
|
|
pico.yellow('$ ' + del + ' caniuse-lite') +
|
|
|
|
|
'\n'
|
|
|
|
|
)
|
2023-08-17 09:47:40 +00:00
|
|
|
|
execSync(del + ' caniuse-lite')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateWith(print, cmd) {
|
|
|
|
|
print('Updating caniuse-lite version\n' + pico.yellow('$ ' + cmd) + '\n')
|
|
|
|
|
try {
|
2023-08-17 09:47:40 +00:00
|
|
|
|
execSync(cmd)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} catch (e) /* c8 ignore start */ {
|
|
|
|
|
print(pico.red(e.stdout.toString()))
|
|
|
|
|
print(
|
|
|
|
|
pico.red(
|
|
|
|
|
'\n' +
|
|
|
|
|
e.stack +
|
|
|
|
|
'\n\n' +
|
|
|
|
|
'Problem with `' +
|
|
|
|
|
cmd +
|
|
|
|
|
'` call. ' +
|
|
|
|
|
'Run it manually.\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
} /* c8 ignore end */
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
module.exports = function updateDB(print = defaultPrint) {
|
|
|
|
|
let lock = detectLockfile()
|
|
|
|
|
let latest = getLatestInfo(lock)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let listError
|
|
|
|
|
let oldList
|
2022-01-21 08:28:41 +00:00
|
|
|
|
try {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
oldList = getBrowsers()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} catch (e) {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
listError = e
|
2022-01-21 08:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print('Latest version: ' + pico.bold(pico.green(latest.version)) + '\n')
|
|
|
|
|
|
|
|
|
|
if (lock.mode === 'yarn' && lock.version !== 1) {
|
|
|
|
|
updateWith(print, 'yarn up -R caniuse-lite')
|
2022-03-14 15:40:45 +00:00
|
|
|
|
} else if (lock.mode === 'pnpm') {
|
|
|
|
|
updateWith(print, 'pnpm up caniuse-lite')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
updatePackageManually(print, lock, latest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print('caniuse-lite has been successfully updated\n')
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let newList
|
|
|
|
|
if (!listError) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
try {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
newList = getBrowsers()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} catch (e) /* c8 ignore start */ {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
listError = e
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} /* c8 ignore end */
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:25:19 +00:00
|
|
|
|
if (listError) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
print(
|
|
|
|
|
pico.red(
|
|
|
|
|
'\n' +
|
2022-07-11 11:25:19 +00:00
|
|
|
|
listError.stack +
|
2022-01-21 08:28:41 +00:00
|
|
|
|
'\n\n' +
|
|
|
|
|
'Problem with browser list retrieval.\n' +
|
|
|
|
|
'Target browser changes won’t be shown.\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
2022-07-11 11:25:19 +00:00
|
|
|
|
let changes = diffBrowsers(oldList, newList)
|
|
|
|
|
if (changes) {
|
2022-01-21 08:28:41 +00:00
|
|
|
|
print('\nTarget browser changes:\n')
|
2022-07-11 11:25:19 +00:00
|
|
|
|
print(changes + '\n')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
print('\n' + pico.green('No target browser changes') + '\n')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|