cli.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env node
  2. var browserslist = require('./')
  3. var pkg = require('./package.json')
  4. var args = process.argv.slice(2)
  5. var USAGE = 'Usage:\n' +
  6. ' ' + pkg.name + '\n' +
  7. ' ' + pkg.name + ' "QUERIES"\n' +
  8. ' ' + pkg.name + ' --config="path/to/browserlist/file"\n' +
  9. ' ' + pkg.name + ' --coverage "QUERIES"\n' +
  10. ' ' + pkg.name + ' --coverage=US "QUERIES"\n' +
  11. ' ' + pkg.name + ' --env="environment name defined in config"\n' +
  12. ' ' + pkg.name + ' --stats="path/to/browserlist/stats/file"'
  13. function isArg (arg) {
  14. return args.some(function (str) {
  15. return str === arg || str.indexOf(arg + '=') === 0
  16. })
  17. }
  18. function error (msg) {
  19. process.stderr.write(pkg.name + ': ' + msg + '\n')
  20. process.exit(1)
  21. }
  22. if (isArg('--help') || isArg('-h')) {
  23. process.stdout.write(pkg.description + '.\n\n' + USAGE + '\n')
  24. } else if (isArg('--version') || isArg('-v')) {
  25. process.stdout.write(pkg.name + ' ' + pkg.version + '\n')
  26. } else {
  27. var mode = 'browsers'
  28. var opts = { }
  29. var queries
  30. var country
  31. for (var i = 0; i < args.length; i++) {
  32. if (args[i][0] !== '-') {
  33. queries = args[i].replace(/^['"]|['"]$/g, '')
  34. continue
  35. }
  36. var arg = args[i].split('=')
  37. var name = arg[0]
  38. var value = arg[1]
  39. if (value) value = value.replace(/^['"]|['"]$/g, '')
  40. if (name === '--config' || name === '-b') {
  41. opts.config = value
  42. } else if (name === '--env' || name === '-e') {
  43. opts.env = value
  44. } else if (name === '--stats' || name === '-s') {
  45. opts.stats = value
  46. } else if (name === '--coverage' || name === '-c') {
  47. mode = 'coverage'
  48. if (value) country = value
  49. } else {
  50. error('Unknown arguments ' + args[i] + '.\n\n' + USAGE)
  51. }
  52. }
  53. if (!queries && !opts.config) {
  54. if (browserslist.findConfig(process.cwd())) {
  55. opts.path = process.cwd()
  56. } else {
  57. error(
  58. 'Browserslist config did not found. ' +
  59. 'Define queries or config path.' +
  60. '\n\n' + USAGE
  61. )
  62. }
  63. }
  64. var browsers
  65. try {
  66. browsers = browserslist(queries, opts)
  67. } catch (e) {
  68. if (e.name === 'BrowserslistError') {
  69. error(e.message)
  70. } else {
  71. throw e
  72. }
  73. }
  74. if (mode === 'browsers') {
  75. browsers.forEach(function (browser) {
  76. process.stdout.write(browser + '\n')
  77. })
  78. } else {
  79. var result = browserslist.coverage(browsers, country)
  80. var round = Math.round(result * 100) / 100.0
  81. var end = 'globally'
  82. if (country && country !== 'global') {
  83. end = 'in the ' + country.toUpperCase()
  84. }
  85. process.stdout.write(
  86. 'These browsers account for ' + round + '% of all users ' +
  87. end + '\n')
  88. }
  89. }