source: trip-planner-front/node_modules/browserslist/cli.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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