wishthis/node_modules/update-browserslist-db/index.js

310 lines
8.4 KiB
JavaScript
Raw Normal View History

2022-07-11 11:25:19 +00:00
let childProcess = require('child_process')
let escalade = require('escalade/sync')
let pico = require('picocolors')
let path = require('path')
let fs = require('fs')
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
)
}
2022-07-11 11:25:19 +00:00
let lockfileNpm = path.join(packageDir, 'package-lock.json')
let lockfileShrinkwrap = path.join(packageDir, 'npm-shrinkwrap.json')
let lockfileYarn = path.join(packageDir, 'yarn.lock')
let lockfilePnpm = path.join(packageDir, 'pnpm-lock.yaml')
2022-01-21 08:28:41 +00:00
if (fs.existsSync(lockfilePnpm)) {
return { mode: 'pnpm', file: lockfilePnpm }
} else if (fs.existsSync(lockfileNpm)) {
return { mode: 'npm', file: lockfileNpm }
} else if (fs.existsSync(lockfileYarn)) {
2022-07-11 11:25:19 +00:00
let lock = { mode: 'yarn', file: lockfileYarn }
2022-01-21 08:28:41 +00:00
lock.content = fs.readFileSync(lock.file).toString()
lock.version = /# yarn lockfile v1/.test(lock.content) ? 1 : 2
return lock
} else if (fs.existsSync(lockfileShrinkwrap)) {
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) {
return JSON.parse(
childProcess.execSync('yarn info caniuse-lite --json').toString()
).data
} else {
return JSON.parse(
childProcess.execSync('yarn npm info caniuse-lite --json').toString()
)
}
}
return JSON.parse(
childProcess.execSync('npm show caniuse-lite --json').toString()
)
}
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)
}
}
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) {
if (!lock.content) lock.content = fs.readFileSync(lock.file).toString()
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: ' +
pico.bold(pico.green(latest.version)) +
'\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'
)
fs.writeFileSync(lock.file, lockfileData.content)
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 {
childProcess.execSync(install + ' caniuse-lite')
} 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'
)
childProcess.execSync(del + ' caniuse-lite')
}
function updateWith(print, cmd) {
print('Updating caniuse-lite version\n' + pico.yellow('$ ' + cmd) + '\n')
try {
childProcess.execSync(cmd)
} 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 wont 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')
}
}
}