-
Notifications
You must be signed in to change notification settings - Fork 7
/
bin.js
executable file
·127 lines (112 loc) · 2.86 KB
/
bin.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
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
#!/usr/bin/env node
const BBY_API_KEY_MSG = 'BBY_API_KEY environment variable'
const RESOURCES = ['products', 'categories', 'stores']
const pkg = require('./package')
const updateNotifier = require('update-notifier')
const run = require('.')
function cli (args, stream, cb) {
const clopts = require('cliclopts')([
{
name: 'query',
abbr: 'q',
help: 'use a custom query to filter the results'
},
{
name: 'show',
abbr: 's',
help: 'fields to show'
},
{
name: 'sort',
abbr: 'r',
help: 'sort results by fields (comma separated)'
},
{
name: 'key',
abbr: 'k',
help: 'Best Buy API key',
default: BBY_API_KEY_MSG
},
{
name: 'format',
abbr: 'f',
help: 'format of the response as json, xml, csv or tsv',
default: 'json'
},
{
name: 'output',
abbr: 'o',
help: 'name of file to send output (optional; If not present, out will go to stdout)'
},
{
name: 'bare',
abbr: 'b',
help: 'newline delimited - each item on own line without extra cruft',
default: false
},
{
name: 'version',
abbr: 'v',
boolean: true,
help: 'show version information'
},
{
name: 'help',
abbr: 'h',
help: 'show help',
boolean: true
},
{
name: 'debug',
abbr: 'd',
help: 'show debug information',
boolean: true,
default: false
}
])
const argv = require('minimist')(args, {
alias: clopts.alias(),
boolean: clopts.boolean(),
default: clopts.default()
})
argv.resource = argv.resource || argv._[0]
if (argv.version) {
stream.write(pkg.version + '\n')
return cb()
}
if (!argv.resource) argv.help = true
if (argv.help) {
stream.write(`
Best Buy Bulk Download Tool (https://github.com/BestBuyAPIs/bestbuy-cli)
Usage: bestbuy [resource] [options]
Examples:
bestbuy categories
bestbuy products --query "active=true" --show "name,sku" --output products.json
bestbuy stores --format xml --output stores.xml
resource resource to download: ${RESOURCES.join(', ')}
`)
clopts.print(stream)
stream.write('\nVisit https://developer.bestbuy.com/documentation for more details on writing custom queries.\n')
return cb()
}
argv.query = argv.query || ''
if (argv.key === BBY_API_KEY_MSG) argv.key = process.env.BBY_API_KEY
if (RESOURCES.indexOf(argv.resource) < 0) {
return cb(new Error(`Invalid resource: ${argv.resource}`))
}
run(argv, stream, cb)
}
if (require.main === module) {
cli(process.argv.slice(2), process.stdout, function done (err) {
let code = 0
if (err) {
console.error(err.message)
console.error(err.stack)
code = -1
}
updateNotifier({ pkg: pkg }).notify()
process.exit(code)
})
} else {
module.exports = cli
}