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
54 lines (50 loc) · 1.12 KB
/
Copy pathparse-raw-flags.js
File metadata and controls
54 lines (50 loc) · 1.12 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
// 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!}
//
const parseRawFlags = function (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
}
const aggressiveJSONParse = function (value) {
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
let parsed
try {
parsed = JSON.parse(value)
} catch (error) {
try {
parsed = JSON.parse(`"${value}"`)
} catch (error_) {
parsed = value
}
}
return parsed
}
module.exports = {
parseRawFlags,
aggressiveJSONParse,
}