-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.js
executable file
·61 lines (56 loc) · 1.87 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
#!/usr/bin/env node
const lib = require('./lib')
const toTable = require('./formatters/table')
const toCsv = require('./formatters/csv')
const toJson = require('./formatters/json')
const toXml = require('./formatters/xml')
const ora = require('ora')
const cliOptions = require('./cli-options')
const argv = require('yargs')
.usage('$0', 'List licenses for installed packages (https://github.com/morficus/license-ls)')
.options(cliOptions)
.hide('table')
// force comma-separated values in to arrays
.coerce(['include'], function (input) {
if (input.length > 1) {
return input
} else {
return input[0].split(',')
}
})
.argv
const options = Object.assign({}, argv)
// remove the useless stuff form yargs
delete options._
delete options.$0
const spinner = ora('Analyzing').start()
lib(options)
.then(async results => {
let output = ''
// grab only the properties that need to be included
// this is basically the same as _.pluck is doing
const limited = results.map(package => {
return options.include.reduce((aggregate, key) => {
aggregate[key] = package[key]
return aggregate
}, {})
})
spinner.succeed().start('Building output format')
const format = options.format
switch (format) {
case 'table':
output = await toTable({data: limited, header: options.table.header})
break
case 'csv':
output = await toCsv(limited, options.csv.delimiter)
break
case 'json':
output = toJson(limited)
break
case 'xml':
output = toXml({data: limited, options: options.xml})
break
}
spinner.succeed()
console.log(output)
})