cli.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env node
  2. var updateDb = require('update-browserslist-db')
  3. var fs = require('fs')
  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 3 */
  35. updateDb(function (str) {
  36. process.stdout.write(str)
  37. })
  38. } else {
  39. var mode = 'browsers'
  40. var opts = {}
  41. var queries
  42. var areas
  43. for (var i = 0; i < args.length; i++) {
  44. if (args[i][0] !== '-') {
  45. queries = args[i].replace(/^["']|["']$/g, '')
  46. continue
  47. }
  48. var arg = args[i].split('=')
  49. var name = arg[0]
  50. var value = arg[1]
  51. if (value) value = value.replace(/^["']|["']$/g, '')
  52. if (name === '--config' || name === '-b') {
  53. opts.config = value
  54. } else if (name === '--env' || name === '-e') {
  55. opts.env = value
  56. } else if (name === '--stats' || name === '-s') {
  57. opts.stats = value
  58. } else if (name === '--coverage' || name === '-c') {
  59. if (mode !== 'json') mode = 'coverage'
  60. if (value) {
  61. areas = value.split(',')
  62. } else {
  63. areas = ['global']
  64. }
  65. } else if (name === '--json') {
  66. mode = 'json'
  67. } else if (name === '--mobile-to-desktop') {
  68. /* c8 ignore next */
  69. opts.mobileToDesktop = true
  70. } else if (name === '--ignore-unknown-versions') {
  71. /* c8 ignore next */
  72. opts.ignoreUnknownVersions = true
  73. } else {
  74. error('Unknown arguments ' + args[i] + '.\n\n' + USAGE)
  75. }
  76. }
  77. var browsers
  78. try {
  79. browsers = browserslist(queries, opts)
  80. } catch (e) {
  81. if (e.name === 'BrowserslistError') {
  82. error(e.message)
  83. } /* c8 ignore start */ else {
  84. throw e
  85. } /* c8 ignore end */
  86. }
  87. var coverage
  88. if (mode === 'browsers') {
  89. browsers.forEach(function (browser) {
  90. process.stdout.write(browser + '\n')
  91. })
  92. } else if (areas) {
  93. coverage = areas.map(function (area) {
  94. var stats
  95. if (area !== 'global') {
  96. stats = area
  97. } else if (opts.stats) {
  98. stats = JSON.parse(fs.readFileSync(opts.stats))
  99. }
  100. var result = browserslist.coverage(browsers, stats)
  101. var round = Math.round(result * 100) / 100.0
  102. return [area, round]
  103. })
  104. if (mode === 'coverage') {
  105. var prefix = 'These browsers account for '
  106. process.stdout.write(prefix)
  107. coverage.forEach(function (data, index) {
  108. var area = data[0]
  109. var round = data[1]
  110. var end = 'globally'
  111. if (area && area !== 'global') {
  112. end = 'in the ' + area.toUpperCase()
  113. } else if (opts.stats) {
  114. end = 'in custom statistics'
  115. }
  116. if (index !== 0) {
  117. process.stdout.write(prefix.replace(/./g, ' '))
  118. }
  119. process.stdout.write(round + '% of all users ' + end + '\n')
  120. })
  121. }
  122. }
  123. if (mode === 'json') {
  124. var data = { browsers: browsers }
  125. if (coverage) {
  126. data.coverage = coverage.reduce(function (object, j) {
  127. object[j[0]] = j[1]
  128. return object
  129. }, {})
  130. }
  131. process.stdout.write(JSON.stringify(data, null, ' ') + '\n')
  132. }
  133. }