cli.js 4.1 KB

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