You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.6 KiB

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