Skip to content

Commit

Permalink
Change CLI to always print result as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Nov 12, 2021
1 parent ca8d84e commit 88dc694
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 122 deletions.
77 changes: 10 additions & 67 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ const names = Object.keys(require('./lib/browsers'))
const argv = require('yargs')
.usage([
'win-detect-browsers [options] [name, name..]\n',
'Names:',
'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('json')
.boolean('summary')
.boolean('debug')
.describe('json', 'Print results as JSON array')
.describe('summary', 'Less properties')
.describe('debug', 'Enable debug scope')
.describe('debug', 'Enable debug output')
.describe('version', 'Show CLI version number')
.alias({
version: 'v',
help: 'h',
json: 'j',
summary: 's',
debug: 'd'
})
Expand All @@ -32,77 +30,22 @@ if (argv.debug) {
const detect = require('.')
const start = Date.now()

detect(argv._, argv, function (err, browsers, methods) {
detect(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 }))
if (argv.summary) {
browsers = browsers.map(b => {
const { name, version, channel, arch, path } = b
return { name, path, version, channel, arch }
})
}

console.error('Found %d browsers in %d ways within %dms.', browsers.length, methods, duration)
console.log(JSON.stringify(browsers.map(ordered), null, 2))
console.error('\nFound %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))
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
"dependencies": {
"after": "^0.8.2",
"catering": "^2.1.0",
"chalk": "^4.1.2",
"debug": "^4.1.0",
"existent": "^1.0.1",
"pascal-case": "^3.1.1",
"pe-machine-type": "^1.0.0",
"pretty-tree": "^1.0.0",
"registry-js": "^1.9.0",
"which": "^2.0.1",
"win-version-info": "^5.0.1",
Expand Down
58 changes: 6 additions & 52 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

- [Usage](#usage)
- [API](#api)
- [`detect([names, ]callback)`](#detectnames-callback)
- [`detect([names][, callback])`](#detectnames-callback)
- [CLI](#cli)
- [Sample](#sample)
- [Full Sample](#full-sample)
- [Install](#install)
- [Background](#background)
- [License](#license)
Expand Down Expand Up @@ -63,7 +62,7 @@ Each `result` is an object with the following properties:
- `path` (string): absolute path to executable
- `version` (string)
- `arch` (string): CPU type the executable was built for: `amd64`, `i386` or [other](https://github.com/vweevers/pe-machine-type#types);
- `info` (object): executable metadata (see [full sample](#full-sample) below).
- `info` (object): executable metadata (see [sample](#sample) below).

Additional properties are usually available but not guaranteed:

Expand All @@ -79,63 +78,18 @@ Additional properties are usually available but not guaranteed:
win-detect-browsers [options] [name, name..]
```

Write browsers to stdout as a JSON array. Includes all browsers unless one or more names are given.

Options:

- `--help` `-h`: Show help
- `--version` `-v`: Show CLI version number
- `--json` `-j`: Print results as JSON array
- `--summary` `-s`: Less properties;
- `--debug` `-d`: Enable [debug](https://github.com/visionmedia/debug) scope.
- `--debug` `-d`: Enable debug output.

### Sample

On Windows 10 with `--summary`:

<details><summary>Click to expand</summary>

```
IE 11 64-bit
├── Path: C:\Program Files\Internet Explorer\iexplore.exe
└── Version: 11.0.17134.1
IE 11 32-bit
├── Path: C:\Program Files (x86)\Internet Explorer\iexplore.exe
└── Version: 11.0.17134.1
CHROME 68 CANARY 64-bit
├── Path: C:\Users\vweevers\AppData\Local\Google\Chrome SxS\Application\chrome.exe
├── Version: 68.0.3436.0
└── GUID: 4EA16AC7-FD5A-47C3-875B-DBF4A2008C20
CHROME 66 STABLE 64-bit
├── Path: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
├── Version: 66.0.3359.181
└── GUID: 8A69D345-D564-463C-AFF1-A69D9E530F96
FIREFOX 61 RELEASE 64-bit
├── Path: C:\Program Files\Mozilla Firefox\firefox.exe
└── Version: 61.0.0.6711
FIREFOX 62 NIGHTLY 64-bit
├── Path: C:\Program Files\Firefox Nightly\firefox.exe
└── Version: 62.0.0.6712
FIREFOX 61 DEVELOPER 64-bit
├── Path: C:\Program Files\Firefox Developer Edition\firefox.exe
└── Version: 61.0.0.6711
OPERA 53 BETA 64-bit
├── Path: C:\Program Files\Opera beta\Launcher.exe
└── Version: 53.0.2907.31
Found 9 browsers in 26 ways within 76ms.
```

</details>

### Full Sample

On Windows 10 with `--json`:
On Windows 10:

<details><summary>Click to expand</summary>

Expand Down

0 comments on commit 88dc694

Please sign in to comment.