Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Commit 6906374

Browse files
committed
support array variables
1 parent 4bd436d commit 6906374

File tree

8 files changed

+115
-29
lines changed

8 files changed

+115
-29
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util/args.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util/misc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,48 @@ class PGB {
1818
this.session = session
1919
this.cliOpts = {
2020
flags: {
21-
version: 'v', bare: 'b', ascii: 'a', json: 'j', debug: 'd', noprogress: '', help: '?h', force: 'fy', completion: '', stdout: 's', nocolours: 'c', nounlock: '', exit: 'e', exitcode: ''
21+
version: 'v',
22+
bare: 'b',
23+
ascii: 'a',
24+
json: 'j',
25+
debug: 'd',
26+
noprogress: '',
27+
help: '?h',
28+
force: 'fy',
29+
completion: '',
30+
stdout: 's',
31+
nocolours: 'c',
32+
nounlock: '',
33+
exit: 'e',
34+
exitcode: ''
35+
},
36+
variables: {
37+
ignore: true,
38+
keystore_password: false,
39+
key_password: false,
40+
auth_token: false,
41+
publisher_id: false,
42+
title: false,
43+
profile: false,
44+
key: false,
45+
default: false,
46+
lock: false,
47+
alias: false,
48+
hydrates: false,
49+
share: false,
50+
tag: false,
51+
debug: false,
52+
private: false,
53+
pull: false,
54+
zip: false,
55+
'android-phonegap': false,
56+
'winphone-phonegap': false,
57+
'ios-phonegap': false,
58+
phonegap: false,
59+
'android-key': false,
60+
'windows-key': false,
61+
'ios-key': false,
62+
'winphone-key': false
2263
},
2364
aliases: {
2465
add: 'new',
@@ -82,6 +123,7 @@ class PGB {
82123
process.on('debug', pgb.debug.bind(this))
83124

84125
command = this.opts.commands[0] || 'usage'
126+
command = command.toLowerCase()
85127

86128
if (this.opts.completion) command = 'completion'
87129
else if (this.opts.version) command = 'version'

src/util/args.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,27 @@ module.exports = (opts) => {
1818
}
1919
}
2020

21-
const parseVariable = arg => {
21+
let rawArgs = process.argv.slice(2)
22+
for (let i = 0; i < rawArgs.length; i++) {
23+
let arg = rawArgs[i]
2224
let args = arg.split('=')
23-
variables[args.shift().toLowerCase()] = args.join('=')
24-
}
25+
let key = args.shift().toLowerCase().replace(/^--/, '')
26+
let variable = (key in opts.variables) || args.length
2527

26-
let rawArgs = process.argv.slice(2)
27-
for (let arg of rawArgs) {
28-
if (arg.startsWith('--')) parseArgs([arg.replace(/-/g, '')])
29-
else if (arg.startsWith('-')) parseArgs(arg.slice(1))
30-
else if (arg.indexOf('=') > -1) parseVariable(arg)
31-
else result.commands.push(arg.toLowerCase())
28+
if (variable) {
29+
let value = (args.length) ? args.join('=') : rawArgs[++i]
30+
if (opts.variables[key]) {
31+
variables[key] = (variables[key] || []).concat(value.split(','))
32+
} else {
33+
variables[key] = value
34+
}
35+
} else if (arg.startsWith('--')) {
36+
parseArgs([arg.slice(2)])
37+
} else if (arg.startsWith('-')) {
38+
parseArgs(arg.slice(1))
39+
} else {
40+
result.commands.push(arg)
41+
}
3242
}
3343

3444
if (result.commands[0]) {

src/util/misc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const promiseSeries = (...funcs) =>
1818
const merge = function() {
1919
const clone = (source, dest) => {
2020
for (let prop in source) {
21-
if (source[prop] && source[prop].constructor === Object) {
21+
if (source[prop] && source[prop].constructor === Array && (dest[prop] == null || dest[prop].constructor === Array)) {
22+
dest[prop] = dest[prop] || []
23+
dest[prop] = source[prop].concat(dest[prop])
24+
} else if (source[prop] && source[prop].constructor === Object) {
2225
dest[prop] = dest[prop] || {}
2326
dest[prop] = clone(source[prop], dest[prop])
2427
} else {

test/util/args.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,103 @@ describe('args', () => {
1212
})
1313

1414
test('should return an object', () => {
15-
let opts = { flags: {}, aliases: {} }
15+
let opts = { flags: {}, aliases: {}, variables: {} }
1616
process.argv = [ '', '' ]
1717
return parseArgs(opts).then((result) => {
1818
expect(result).toEqual({ commands: [ ], variables: {} })
1919
})
2020
})
2121

2222
test('should parse single commands', () => {
23-
let opts = { flags: {}, aliases: {} }
23+
let opts = { flags: {}, aliases: {}, variables: {} }
2424
argv('foo')
2525
return parseArgs(opts).then((result) => {
2626
expect(result).toEqual({ commands: [ 'foo' ], variables: {} })
2727
})
2828
})
2929

3030
test('should parse multiple commands', () => {
31-
let opts = { flags: {}, aliases: {} }
31+
let opts = { flags: {}, aliases: {}, variables: {} }
3232
argv('foo bar')
3333
return parseArgs(opts).then((result) => {
3434
expect(result).toEqual({ commands: [ 'foo', 'bar' ], variables: {} })
3535
})
3636
})
3737

3838
test('should parse flags', () => {
39-
let opts = { flags: { foo: 'b' }, aliases: {} }
39+
let opts = { flags: { foo: 'b' }, aliases: {}, variables: {} }
4040
argv('foo -b')
4141
return parseArgs(opts).then((result) => {
4242
expect(result).toEqual({ commands: [ 'foo' ], foo: true, variables: {} })
4343
})
4444
})
4545

4646
test('should parse flags with multiple aliases', () => {
47-
let opts = { flags: { foo: 'br' }, aliases: {} }
47+
let opts = { flags: { foo: 'br' }, aliases: {}, variables: {} }
4848
argv('foo -r')
4949
return parseArgs(opts).then((result) => {
5050
expect(result).toEqual({ commands: [ 'foo' ], foo: true, variables: {} })
5151
})
5252
})
5353

5454
test('should parse compound flags', () => {
55-
let opts = { flags: { bar: 'x', roo: 'r' }, aliases: {} }
55+
let opts = { flags: { bar: 'x', roo: 'r' }, aliases: {}, variables: {} }
5656
argv('foo -xr')
5757
return parseArgs(opts).then((result) => {
5858
expect(result).toEqual({ commands: [ 'foo' ], bar: true, roo: true, variables: {} })
5959
})
6060
})
6161

6262
test('should parse full flags', () => {
63-
let opts = { flags: { bar: 'x', roo: 'y', oot: 'z' }, aliases: {} }
63+
let opts = { flags: { bar: 'x', roo: 'y', oot: 'z' }, aliases: {}, variables: {} }
6464
argv('foo --bar --roo')
6565
return parseArgs(opts).then((result) => {
6666
expect(result).toEqual({ commands: [ 'foo' ], bar: true, roo: true, variables: {} })
6767
})
6868
})
6969

7070
test('should parse variables', () => {
71-
let opts = { flags: { }, aliases: {} }
71+
let opts = { flags: {}, aliases: {}, variables: {} }
7272
argv('foo var1=abc var2=def')
7373
return parseArgs(opts).then((result) => {
7474
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'abc', var2: 'def' } })
7575
})
7676
})
7777

7878
test('should parse variables correctly with equal signs in value', () => {
79-
let opts = { flags: { }, aliases: {} }
79+
let opts = { flags: { }, aliases: {}, variables: { var1: false } }
8080
argv('foo var1=abc var2=bar=12')
8181
return parseArgs(opts).then((result) => {
8282
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'abc', var2: 'bar=12' } })
8383
})
8484
})
8585

86+
test('should parse variables correctly with --', () => {
87+
let opts = { flags: { }, aliases: {}, variables: { var2: false } }
88+
argv('foo var1=abc --var2 bar=12')
89+
return parseArgs(opts).then((result) => {
90+
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'abc', var2: 'bar=12' } })
91+
})
92+
})
93+
94+
test('should parse variables correctly with -- and equal signs', () => {
95+
let opts = { flags: { }, aliases: {}, variables: { var2: false } }
96+
argv('foo var1=abc --var2=bar=12 --var3=12')
97+
return parseArgs(opts).then((result) => {
98+
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'abc', var2: 'bar=12', 'var3': '12' } })
99+
})
100+
})
101+
102+
test('should parse variables and use arrays for duplicate variables', () => {
103+
let opts = { flags: { }, aliases: {}, variables: { var2: true, var4: true } }
104+
argv('foo var1=abc --var2=bar=12 --var3=12 --var2=bar=24 --var3=24 --var4=1,2,3')
105+
return parseArgs(opts).then((result) => {
106+
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'abc', var2: ['bar=12', 'bar=24'], 'var3': '24', 'var4': ['1', '2', '3'] } })
107+
})
108+
})
109+
86110
test('should include env variables with pgb_ prefix', () => {
87-
let opts = { flags: { }, aliases: {} }
111+
let opts = { flags: { }, aliases: {}, variables: {} }
88112
argv('foo')
89113
process.env['pgb_var1'] = 'abc'
90114
return parseArgs(opts).then((result) => {
@@ -93,7 +117,7 @@ describe('args', () => {
93117
})
94118

95119
test('should obey precedence, variables > envs', () => {
96-
let opts = { flags: { }, aliases: {} }
120+
let opts = { flags: { }, aliases: {}, variables: {} }
97121
argv('foo var1=bar')
98122
process.env['pgb_var1'] = 'foo'
99123
return parseArgs(opts).then((result) => {
@@ -109,7 +133,7 @@ describe('args', () => {
109133
afterAll(() => stdin.restore())
110134

111135
test('should parse piped data', (done) => {
112-
let opts = { flags: { }, aliases: {} }
136+
let opts = { flags: { }, aliases: {}, variables: {} }
113137
argv('foo')
114138
parseArgs(opts).then((result) => {
115139
expect(result).toEqual({ commands: [ 'foo' ], variables: { foo: 'bar' } })
@@ -121,7 +145,7 @@ describe('args', () => {
121145
})
122146

123147
test('should allow empty input', (done) => {
124-
let opts = { flags: { }, aliases: {} }
148+
let opts = { flags: { }, aliases: {}, variables: {} }
125149
argv('foo')
126150
parseArgs(opts).then((result) => {
127151
expect(result).toEqual({ commands: [ 'foo' ], variables: { } })
@@ -133,7 +157,7 @@ describe('args', () => {
133157
})
134158

135159
test('should fail on bad json', (done) => {
136-
let opts = { flags: { }, aliases: {} }
160+
let opts = { flags: { }, aliases: {}, variables: {} }
137161
argv('foo')
138162
parseArgs(opts).then((result) => {
139163
done.fail('pipe successful')
@@ -147,7 +171,7 @@ describe('args', () => {
147171
})
148172

149173
test('should obey precedence, variables > pipe > envs', (done) => {
150-
let opts = { flags: { }, aliases: {} }
174+
let opts = { flags: { }, aliases: {}, variables: {} }
151175
process.env['pgb_var1'] = 'foo'
152176
parseArgs(opts).then((result) => {
153177
expect(result).toEqual({ commands: [ 'foo' ], variables: { var1: 'yut' } })

test/util/misc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ describe('merge', () => {
2424
expect(misc.merge({ a: 1 }, { a: 2 }, { a: 3 })).toEqual({ a: 3 })
2525
})
2626

27+
test('concats arrays', () => {
28+
expect(misc.merge({ a: [] }, {})).toEqual({ a: [] })
29+
expect(misc.merge({ a: [2] }, { a: [1] })).toEqual({ a: [1, 2] })
30+
expect(misc.merge({ a: [] }, { a: 56 })).toEqual({ a: 56 })
31+
expect(misc.merge({ a: 56 }, { a: [] })).toEqual({ a: [] })
32+
})
33+
2734
test('does nested merging', () => {
2835
expect(misc.merge({ a: { c: [], b: 1 } }, { a: { b: 2 } }, { a: { c: 3 } })).toEqual({ a: { b: 2, c: 3 } })
2936
})

0 commit comments

Comments
 (0)