forked from vweevers/win-detect-browsers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
128 lines (102 loc) · 2.95 KB
/
cli.js
File metadata and controls
128 lines (102 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
'use strict'
const names = Object.keys(require('./lib/browsers'))
const argv = require('yargs')
.usage([
'win-detect-browsers [options] [name, name..]\n',
'Names:',
names.map(name => ' ' + name).join('\n')
].join('\n'))
.boolean('json')
.boolean('summary')
.boolean('debug')
.describe('json', 'Print results as JSON array')
.describe('summary', 'Less properties')
.describe('debug', 'Enable debug scope')
.describe('version', 'Show CLI version number')
.alias({
version: 'v',
help: 'h',
json: 'j',
summary: 's',
debug: 'd'
})
.argv
if (argv.debug) {
require('debug').enable('win-detect-browsers')
}
const detect = require('.')
const start = Date.now()
detect(argv._, argv, function (err, browsers, methods) {
if (err) throw err
const duration = Date.now() - start
if (argv.json) {
if (argv.summary) {
browsers = browsers.map(b => {
const { name, version, channel, arch, path } = b
return { name, path, version, channel, arch }
})
}
console.log(JSON.stringify(browsers.map(ordered), null, 2))
console.log()
} else {
const tree = require('pretty-tree')
const chalk = require('chalk')
const pascal = require('pascal-case').pascalCase
browsers.forEach(function print (b) {
const labels = [b.name.toUpperCase(), major(b.version)]
if (b.channel) {
labels.push(b.channel.toUpperCase())
}
if (b.arch === 'amd64') {
labels.push('64-bit')
} else if (b.arch === 'i386') {
labels.push('32-bit')
}
const label = chalk.white(labels.join(' '))
const atomic = ['path', 'version'].filter(key => b[key] != null)
const nonAtomic = Object.keys(b).filter(key => isObject(b[key]))
const pad = atomic.reduce(function (max, key) {
return max.length >= key.length ? max : key.replace(/./g, ' ')
}, ' ')
const nodes = atomic.map(key => {
const value = b[key]
key = pascal(key)
key = key + ':' + pad.slice(key.length - pad.length - 1)
return chalk.cyan(key) + value
})
if (!argv.summary) {
for (const key of nonAtomic) {
nodes.push({ label: pascal(key), leaf: b[key] })
}
}
console.log(tree({ label, nodes }))
})
}
console.error('Found %d browsers in %d ways within %dms.', browsers.length, methods, duration)
})
function isObject (value) {
return typeof value === 'object' && value !== null
}
function major (version) {
return version.split(/[^\d]+/)[0]
}
function ordered (a) {
const b = {}
const remaining = new Set(Object.keys(a))
const objects = []
for (const k of ['name', 'path', 'version', 'channel', 'arch']) {
if (k in a) {
b[k] = a[k]
remaining.delete(k)
}
}
for (const k of remaining) {
if (typeof a[k] === 'object') objects.push(k)
else b[k] = a[k]
}
for (const k of objects.sort()) {
b[k] = a[k]
}
return b
}