update-db.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. var childProcess = require('child_process')
  2. var escalade = require('escalade/sync')
  3. var pico = require('picocolors')
  4. var path = require('path')
  5. var fs = require('fs')
  6. var BrowserslistError = require('./error')
  7. function detectLockfile() {
  8. var packageDir = escalade('.', function (dir, names) {
  9. return names.indexOf('package.json') !== -1 ? dir : ''
  10. })
  11. if (!packageDir) {
  12. throw new BrowserslistError(
  13. 'Cannot find package.json. ' +
  14. 'Is this the right directory to run `npx browserslist --update-db` in?'
  15. )
  16. }
  17. var lockfileNpm = path.join(packageDir, 'package-lock.json')
  18. var lockfileShrinkwrap = path.join(packageDir, 'npm-shrinkwrap.json')
  19. var lockfileYarn = path.join(packageDir, 'yarn.lock')
  20. var lockfilePnpm = path.join(packageDir, 'pnpm-lock.yaml')
  21. if (fs.existsSync(lockfilePnpm)) {
  22. return { mode: 'pnpm', file: lockfilePnpm }
  23. } else if (fs.existsSync(lockfileNpm)) {
  24. return { mode: 'npm', file: lockfileNpm }
  25. } else if (fs.existsSync(lockfileYarn)) {
  26. var lock = { mode: 'yarn', file: lockfileYarn }
  27. lock.content = fs.readFileSync(lock.file).toString()
  28. lock.version = /# yarn lockfile v1/.test(lock.content) ? 1 : 2
  29. return lock
  30. } else if (fs.existsSync(lockfileShrinkwrap)) {
  31. return { mode: 'npm', file: lockfileShrinkwrap }
  32. }
  33. throw new BrowserslistError(
  34. 'No lockfile found. Run "npm install", "yarn install" or "pnpm install"'
  35. )
  36. }
  37. function getLatestInfo(lock) {
  38. if (lock.mode === 'yarn') {
  39. if (lock.version === 1) {
  40. return JSON.parse(
  41. childProcess.execSync('yarn info caniuse-lite --json').toString()
  42. ).data
  43. } else {
  44. return JSON.parse(
  45. childProcess.execSync('yarn npm info caniuse-lite --json').toString()
  46. )
  47. }
  48. }
  49. return JSON.parse(
  50. childProcess.execSync('npm show caniuse-lite --json').toString()
  51. )
  52. }
  53. function getBrowsersList() {
  54. return childProcess
  55. .execSync('npx browserslist')
  56. .toString()
  57. .trim()
  58. .split('\n')
  59. .map(function (line) {
  60. return line.trim().split(' ')
  61. })
  62. .reduce(function (result, entry) {
  63. if (!result[entry[0]]) {
  64. result[entry[0]] = []
  65. }
  66. result[entry[0]].push(entry[1])
  67. return result
  68. }, {})
  69. }
  70. function diffBrowsersLists(old, current) {
  71. var browsers = Object.keys(old).concat(
  72. Object.keys(current).filter(function (browser) {
  73. return old[browser] === undefined
  74. })
  75. )
  76. return browsers
  77. .map(function (browser) {
  78. var oldVersions = old[browser] || []
  79. var currentVersions = current[browser] || []
  80. var intersection = oldVersions.filter(function (version) {
  81. return currentVersions.indexOf(version) !== -1
  82. })
  83. var addedVersions = currentVersions.filter(function (version) {
  84. return intersection.indexOf(version) === -1
  85. })
  86. var removedVersions = oldVersions.filter(function (version) {
  87. return intersection.indexOf(version) === -1
  88. })
  89. return removedVersions
  90. .map(function (version) {
  91. return pico.red('- ' + browser + ' ' + version)
  92. })
  93. .concat(
  94. addedVersions.map(function (version) {
  95. return pico.green('+ ' + browser + ' ' + version)
  96. })
  97. )
  98. })
  99. .reduce(function (result, array) {
  100. return result.concat(array)
  101. }, [])
  102. .join('\n')
  103. }
  104. function updateNpmLockfile(lock, latest) {
  105. var metadata = { latest: latest, versions: [] }
  106. var content = deletePackage(JSON.parse(lock.content), metadata)
  107. metadata.content = JSON.stringify(content, null, ' ')
  108. return metadata
  109. }
  110. function deletePackage(node, metadata) {
  111. if (node.dependencies) {
  112. if (node.dependencies['caniuse-lite']) {
  113. var version = node.dependencies['caniuse-lite'].version
  114. metadata.versions[version] = true
  115. delete node.dependencies['caniuse-lite']
  116. }
  117. for (var i in node.dependencies) {
  118. node.dependencies[i] = deletePackage(node.dependencies[i], metadata)
  119. }
  120. }
  121. return node
  122. }
  123. var yarnVersionRe = /version "(.*?)"/
  124. function updateYarnLockfile(lock, latest) {
  125. var blocks = lock.content.split(/(\n{2,})/).map(function (block) {
  126. return block.split('\n')
  127. })
  128. var versions = {}
  129. blocks.forEach(function (lines) {
  130. if (lines[0].indexOf('caniuse-lite@') !== -1) {
  131. var match = yarnVersionRe.exec(lines[1])
  132. versions[match[1]] = true
  133. if (match[1] !== latest.version) {
  134. lines[1] = lines[1].replace(
  135. /version "[^"]+"/,
  136. 'version "' + latest.version + '"'
  137. )
  138. lines[2] = lines[2].replace(
  139. /resolved "[^"]+"/,
  140. 'resolved "' + latest.dist.tarball + '"'
  141. )
  142. if (lines.length === 4) {
  143. lines[3] = latest.dist.integrity
  144. ? lines[3].replace(
  145. /integrity .+/,
  146. 'integrity ' + latest.dist.integrity
  147. )
  148. : ''
  149. }
  150. }
  151. }
  152. })
  153. var content = blocks
  154. .map(function (lines) {
  155. return lines.join('\n')
  156. })
  157. .join('')
  158. return { content: content, versions: versions }
  159. }
  160. function updateLockfile(lock, latest) {
  161. if (!lock.content) lock.content = fs.readFileSync(lock.file).toString()
  162. if (lock.mode === 'yarn') {
  163. return updateYarnLockfile(lock, latest)
  164. } else {
  165. return updateNpmLockfile(lock, latest)
  166. }
  167. }
  168. function updatePackageManually(print, lock, latest) {
  169. var lockfileData = updateLockfile(lock, latest)
  170. var caniuseVersions = Object.keys(lockfileData.versions).sort()
  171. if (caniuseVersions.length === 1 && caniuseVersions[0] === latest.version) {
  172. print(
  173. 'Installed version: ' +
  174. pico.bold(pico.green(latest.version)) +
  175. '\n' +
  176. pico.bold(pico.green('caniuse-lite is up to date')) +
  177. '\n'
  178. )
  179. return
  180. }
  181. if (caniuseVersions.length === 0) {
  182. caniuseVersions[0] = 'none'
  183. }
  184. print(
  185. 'Installed version' +
  186. (caniuseVersions.length === 1 ? ': ' : 's: ') +
  187. pico.bold(pico.red(caniuseVersions.join(', '))) +
  188. '\n' +
  189. 'Removing old caniuse-lite from lock file\n'
  190. )
  191. fs.writeFileSync(lock.file, lockfileData.content)
  192. var install = lock.mode === 'yarn' ? 'yarn add -W' : lock.mode + ' install'
  193. print(
  194. 'Installing new caniuse-lite version\n' +
  195. pico.yellow('$ ' + install + ' caniuse-lite') +
  196. '\n'
  197. )
  198. try {
  199. childProcess.execSync(install + ' caniuse-lite')
  200. } catch (e) /* c8 ignore start */ {
  201. print(
  202. pico.red(
  203. '\n' +
  204. e.stack +
  205. '\n\n' +
  206. 'Problem with `' +
  207. install +
  208. ' caniuse-lite` call. ' +
  209. 'Run it manually.\n'
  210. )
  211. )
  212. process.exit(1)
  213. } /* c8 ignore end */
  214. var del = lock.mode === 'yarn' ? 'yarn remove -W' : lock.mode + ' uninstall'
  215. print(
  216. 'Cleaning package.json dependencies from caniuse-lite\n' +
  217. pico.yellow('$ ' + del + ' caniuse-lite') +
  218. '\n'
  219. )
  220. childProcess.execSync(del + ' caniuse-lite')
  221. }
  222. function updateWith(print, cmd) {
  223. print('Updating caniuse-lite version\n' + pico.yellow('$ ' + cmd) + '\n')
  224. try {
  225. childProcess.execSync(cmd)
  226. } catch (e) /* c8 ignore start */ {
  227. print(pico.red(e.stdout.toString()))
  228. print(
  229. pico.red(
  230. '\n' +
  231. e.stack +
  232. '\n\n' +
  233. 'Problem with `' +
  234. cmd +
  235. '` call. ' +
  236. 'Run it manually.\n'
  237. )
  238. )
  239. process.exit(1)
  240. } /* c8 ignore end */
  241. }
  242. module.exports = function updateDB(print) {
  243. var lock = detectLockfile()
  244. var latest = getLatestInfo(lock)
  245. var browsersListRetrievalError
  246. var oldBrowsersList
  247. try {
  248. oldBrowsersList = getBrowsersList()
  249. } catch (e) {
  250. browsersListRetrievalError = e
  251. }
  252. print('Latest version: ' + pico.bold(pico.green(latest.version)) + '\n')
  253. if (lock.mode === 'yarn' && lock.version !== 1) {
  254. updateWith(print, 'yarn up -R caniuse-lite')
  255. } else if (lock.mode === 'pnpm') {
  256. updateWith(print, 'pnpm up caniuse-lite')
  257. } else {
  258. updatePackageManually(print, lock, latest)
  259. }
  260. print('caniuse-lite has been successfully updated\n')
  261. var currentBrowsersList
  262. if (!browsersListRetrievalError) {
  263. try {
  264. currentBrowsersList = getBrowsersList()
  265. } catch (e) /* c8 ignore start */ {
  266. browsersListRetrievalError = e
  267. } /* c8 ignore end */
  268. }
  269. if (browsersListRetrievalError) {
  270. print(
  271. pico.red(
  272. '\n' +
  273. browsersListRetrievalError.stack +
  274. '\n\n' +
  275. 'Problem with browser list retrieval.\n' +
  276. 'Target browser changes won’t be shown.\n'
  277. )
  278. )
  279. } else {
  280. var targetBrowserChanges = diffBrowsersLists(
  281. oldBrowsersList,
  282. currentBrowsersList
  283. )
  284. if (targetBrowserChanges) {
  285. print('\nTarget browser changes:\n')
  286. print(targetBrowserChanges + '\n')
  287. } else {
  288. print('\n' + pico.green('No target browser changes') + '\n')
  289. }
  290. }
  291. }