-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.js
More file actions
76 lines (65 loc) · 1.73 KB
/
cli.js
File metadata and controls
76 lines (65 loc) · 1.73 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
#!/usr/bin/env node
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import debug from 'debug'
import detect from './index.js'
import browsers from './lib/browsers.js'
const names = Object.keys(browsers)
const argv = yargs(hideBin(process.argv))
.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) {
debug.enable('win-detect-browsers')
}
async function main () {
const start = Date.now()
let browsers = await detect(argv._)
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 %dms.', browsers.length, duration)
}
main().catch(err => {
console.error(err)
process.exit(1)
})
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
}