-
Notifications
You must be signed in to change notification settings - Fork 15
/
cli.js
71 lines (58 loc) · 1.59 KB
/
cli.js
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
#!/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',
'Write browsers to stdout as a JSON array.\n',
'Includes all browsers unless one or more names are given:',
names.map(name => ' ' + name).join('\n')
].join('\n'))
.boolean('summary')
.boolean('debug')
.describe('summary', 'Less properties')
.describe('debug', 'Enable debug output')
.describe('version', 'Show CLI version number')
.alias({
version: 'v',
help: 'h',
summary: 's',
debug: 'd'
})
.argv
if (argv.debug) {
require('debug').enable('win-detect-browsers')
}
const detect = require('.')
const start = Date.now()
detect(argv._, function (err, browsers, methods) {
if (err) throw err
const duration = Date.now() - start
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.error('\nFound %d browsers in %d ways within %dms.', browsers.length, methods, duration)
})
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
}