-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
120 lines (109 loc) · 2.63 KB
/
cli.js
File metadata and controls
120 lines (109 loc) · 2.63 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
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
#!/usr/bin/env node
// use strict
// self
const gw = require('.')
const { commands } = gw
const { name } = require('./package.json')
// npm
const meow = require('meow')
const language = { type: 'string' }
if (process.env.LANG || process.env.LANGUAGE) {
language.default = (process.env.LANG || process.env.LANGUAGE).slice(0, 2).toLowerCase()
}
const cli = meow(`
Usage
$ ${name} <command> [options]
Usage Examples
$ ${name} products --language=fr
$ ${name} products --in-stock=yes --language=en
$ ${name} products --in-stock --language=en
$ ${name} products --in-stock=no --language=en
$ ${name} stores
$ ${name} locations
$ ${name} categories
$ ${name} products --location=qc # also accepts qu(e|é)bec and sqdc
${['Commands', ...Object.keys(commands).sort().map((cmd) => ` ${cmd}\t\t${commands[cmd].description}`)].join('\n')}
Options
--category -c\tFilter by category
--details -d\tMore detailled output (not implemented yet)
--force -f\tBypass cached files if any and force download (not implemented yet)
--in-stock -s\tIn stock only; in-stock=false for the reverse
--language \tLanguage (fr or en), defaults to $LANG or $LANGUAGE
--location -l\tSpecify location
--sku \tProduct id (sku), used with the specs command
--quiet -q\tQuiet
--version\t\tOutput software version
--help\t\tThis help text
`, {
booleanDefault: undefined,
flags: {
language,
location: {
type: 'string',
alias: 'l'
},
help: {
type: 'boolean',
alias: 'h'
},
version: {
type: 'boolean',
alias: 'v'
},
category: {
type: 'string',
alias: 'c'
},
force: {
type: 'boolean',
alias: 'f',
default: false
},
details: {
type: 'boolean',
alias: 'd',
default: false
},
quiet: {
type: 'boolean',
alias: 'q',
default: false
},
sku: {
type: 'string'
},
'in-stock': {
type: 'boolean',
alias: 's'
},
debug: {
type: 'boolean',
default: false
}
}
})
const jsoned = (j) => {
if (!j) {
cli.showHelp(0) // also exits
}
if (j && (j.length || Object.keys(j).length)) {
console.log(JSON.stringify(j, null, ' '))
} else {
console.log('Nothing found.')
}
}
gw(cli)
.then(jsoned)
.catch((e) => {
if (cli && cli.flags && cli.flags.debug) {
console.error(e)
} else {
console.error(e.toString())
}
const code = e.code || 127
delete e.code
if (Object.keys(e).length) {
console.error(JSON.stringify(e))
}
cli.showHelp(code)
})