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