forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-raw-flags.js
More file actions
51 lines (47 loc) · 1.1 KB
/
Copy pathparse-raw-flags.js
File metadata and controls
51 lines (47 loc) · 1.1 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
/* Get flags from `raw` data
Needed for commands using Command.strict = false
*/
/** Usage:
const { flags, raw } = this.parse(addonsCreateCommand)
// flags = {}
const rawFlags = parseRawFlags(raw)
// rawFlags = {stuff: yay!}
*/
exports.parseRawFlags = parseRawFlags
function parseRawFlags(raw) {
const rawFlags = raw.reduce((acc, curr, index, array) => {
if (curr.input.match(/^-{1,2}/)) {
const key = curr.input.replace(/^-{1,2}/, '')
const next = array[index + 1]
if (!next) {
acc[key] = true
} else if (next && next.input && next.input.match(/^-{1,2}/)) {
acc[key] = true
} else {
acc[key] = next ? aggressiveJSONParse(next.input) : true
}
}
return acc
}, {})
return rawFlags
}
exports.aggressiveJSONParse = aggressiveJSONParse
function aggressiveJSONParse(value) {
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
let parsed
try {
parsed = JSON.parse(value)
} catch (e) {
try {
parsed = JSON.parse(`"${value}"`)
} catch (e) {
parsed = value
}
}
return parsed
}