cli.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env node
  2. var fs = require('fs')
  3. var updateDb = require('update-browserslist-db')
  4. var browserslist = require('./')
  5. var pkg = require('./package.json')
  6. var args = process.argv.slice(2)
  7. var USAGE =
  8. 'Usage:\n' +
  9. ' npx browserslist\n' +
  10. ' npx browserslist "QUERIES"\n' +
  11. ' npx browserslist --json "QUERIES"\n' +
  12. ' npx browserslist --config="path/to/browserlist/file"\n' +
  13. ' npx browserslist --coverage "QUERIES"\n' +
  14. ' npx browserslist --coverage=US "QUERIES"\n' +
  15. ' npx browserslist --coverage=US,RU,global "QUERIES"\n' +
  16. ' npx browserslist --env="environment name defined in config"\n' +
  17. ' npx browserslist --stats="path/to/browserlist/stats/file"\n' +
  18. ' npx browserslist --mobile-to-desktop\n' +
  19. ' npx browserslist --ignore-unknown-versions\n'
  20. function isArg(arg) {
  21. return args.some(function (str) {
  22. return str === arg || str.indexOf(arg + '=') === 0
  23. })
  24. }
  25. function error(msg) {
  26. process.stderr.write('browserslist: ' + msg + '\n')
  27. process.exit(1)
  28. }
  29. if (isArg('--help') || isArg('-h')) {
  30. process.stdout.write(pkg.description + '.\n\n' + USAGE + '\n')
  31. } else if (isArg('--version') || isArg('-v')) {
  32. process.stdout.write('browserslist ' + pkg.version + '\n')
  33. } else if (isArg('--update-db')) {
  34. /* c8 ignore next 8 */
  35. process.stdout.write(
  36. 'The --update-db command is deprecated.\n' +
  37. 'Please use npx update-browserslist-db@latest instead.\n'
  38. )
  39. process.stdout.write('Browserslist DB update will still be made.\n')
  40. updateDb(function (str) {
  41. process.stdout.write(str)
  42. })
  43. } else {
  44. var mode = 'browsers'
  45. var opts = {}
  46. var queries
  47. var areas
  48. for (var i = 0; i < args.length; i++) {
  49. if (args[i][0] !== '-') {
  50. queries = args[i].replace(/^["']|["']$/g, '')
  51. continue
  52. }
  53. var arg = args[i].split('=')
  54. var name = arg[0]
  55. var value = arg[1]
  56. if (value) value = value.replace(/^["']|["']$/g, '')
  57. if (name === '--config' || name === '-b') {
  58. opts.config = value
  59. } else if (name === '--env' || name === '-e') {
  60. opts.env = value
  61. } else if (name === '--stats' || name === '-s') {
  62. opts.stats = value
  63. } else if (name === '--coverage' || name === '-c') {
  64. if (mode !== 'json') mode = 'coverage'
  65. if (value) {
  66. areas = value.split(',')
  67. } else {
  68. areas = ['global']
  69. }
  70. } else if (name === '--json') {
  71. mode = 'json'
  72. } else if (name === '--mobile-to-desktop') {
  73. /* c8 ignore next */
  74. opts.mobileToDesktop = true
  75. } else if (name === '--ignore-unknown-versions') {
  76. /* c8 ignore next */
  77. opts.ignoreUnknownVersions = true
  78. } else {
  79. error('Unknown arguments ' + args[i] + '.\n\n' + USAGE)
  80. }
  81. }
  82. var browsers
  83. try {
  84. browsers = browserslist(queries, opts)
  85. } catch (e) {
  86. if (e.name === 'BrowserslistError') {
  87. error(e.message)
  88. } /* c8 ignore start */ else {
  89. throw e
  90. } /* c8 ignore end */
  91. }
  92. var coverage
  93. if (mode === 'browsers') {
  94. browsers.forEach(function (browser) {
  95. process.stdout.write(browser + '\n')
  96. })
  97. } else if (areas) {
  98. coverage = areas.map(function (area) {
  99. var stats
  100. if (area !== 'global') {
  101. stats = area
  102. } else if (opts.stats) {
  103. stats = JSON.parse(fs.readFileSync(opts.stats))
  104. }
  105. var result = browserslist.coverage(browsers, stats)
  106. var round = Math.round(result * 100) / 100.0
  107. return [area, round]
  108. })
  109. if (mode === 'coverage') {
  110. var prefix = 'These browsers account for '
  111. process.stdout.write(prefix)
  112. coverage.forEach(function (data, index) {
  113. var area = data[0]
  114. var round = data[1]
  115. var end = 'globally'
  116. if (area && area !== 'global') {
  117. end = 'in the ' + area.toUpperCase()
  118. } else if (opts.stats) {
  119. end = 'in custom statistics'
  120. }
  121. if (index !== 0) {
  122. process.stdout.write(prefix.replace(/./g, ' '))
  123. }
  124. process.stdout.write(round + '% of all users ' + end + '\n')
  125. })
  126. }
  127. }
  128. if (mode === 'json') {
  129. var data = { browsers: browsers }
  130. if (coverage) {
  131. data.coverage = coverage.reduce(function (object, j) {
  132. object[j[0]] = j[1]
  133. return object
  134. }, {})
  135. }
  136. process.stdout.write(JSON.stringify(data, null, ' ') + '\n')
  137. }
  138. }