Skip to content

Commit 3c4e72f

Browse files
committed
improve parsing of arguments
1 parent 73e3e54 commit 3c4e72f

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# testingbot-tunnel-launcher
22

3-
[![Greenkeeper badge](https://badges.greenkeeper.io/testingbot/testingbot-tunnel-launcher.svg)](https://greenkeeper.io/)
43
[![npm](https://img.shields.io/npm/v/testingbot-tunnel-launcher.svg?maxAge=2592000)](https://www.npmjs.com/package/testingbot-tunnel-launcher)
54
[![dependencies Status](https://david-dm.org/testingbot/testingbot-tunnel-launcher/status.svg)](https://david-dm.org/testingbot/testingbot-tunnel-launcher)
65
[![devDependencies Status](https://david-dm.org/testingbot/testingbot-tunnel-launcher/dev-status.svg)](https://david-dm.org/testingbot/testingbot-tunnel-launcher?type=dev)
@@ -71,7 +70,10 @@ var testingbotTunnel = require('testingbot-tunnel-launcher'),
7170
logfile: null,
7271

7372
// Change the tunnel version - see versions on https://testingbot.com/support/other/tunnel
74-
tunnelVersion: "1.19" // or 2.1 (Java 8)
73+
tunnelVersion: "1.19", // or 2.1 (Java 8)
74+
75+
// Gives this tunnel a unique identifier
76+
tunnelIdentifier: "myIdentifier"
7577
};
7678

7779
testingbotTunnel(options, function(err, tunnel) {
@@ -98,6 +100,10 @@ npm test
98100

99101
## Changelog
100102

103+
### v1.1.11
104+
- Add support for `tunnelIdentifier`
105+
- Improve parsing of arguments passed to the tunnel
106+
101107
### v1.1.11
102108
- Throw error when user does not have any minutes left
103109

lib/tunnel-launcher.js

+42-29
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,16 @@ function downloadTunnel (url, tunnelLocation, callback) {
5252
})
5353
}
5454

55-
function run (options, callback) {
56-
if (!fs.existsSync(tunnelLocation)) {
57-
return callback(new Error(`Tunnel jar file is not present in ${tunnelLocation}`))
58-
}
59-
60-
const checkJava = spawn('java')
61-
checkJava.on('error', err => {
62-
return callback(new Error(`Java might not be installed, necessary to use testingbot-tunnel ${err.message}`))
63-
})
64-
65-
const onReady = function () {
66-
started = true
67-
logger('Tunnel is ready')
68-
callback(null, activeTunnel)
69-
}
70-
55+
function createArgs (options) {
7156
const args = []
7257

7358
args.push('-jar')
7459
args.push(tunnelLocation)
7560

61+
const optionMapping = {
62+
'tunnelIdentifier': 'tunnel-identifier'
63+
}
64+
7665
if (options.apiKey) {
7766
args.push(options.apiKey)
7867
}
@@ -86,25 +75,36 @@ function run (options, callback) {
8675
continue
8776
}
8877

89-
if (options[option]) {
90-
args.push(`--${option}`)
78+
const optionName = optionMapping[option] || option
79+
80+
if (options[option] && typeof (options[option]) === 'string') {
81+
args.push(`--${optionName}`)
9182
args.push(options[option])
92-
} else {
93-
args.push(`--${option}`)
83+
} else if (options[option]) {
84+
args.push(`--${optionName}`)
9485
}
9586
}
9687

97-
const readyFile = path.join(os.tmpdir(), 'testingbot.ready')
98-
try {
99-
if (fs.statSync(readyFile).isFile()) {
100-
logger('Tunnel Readyfile already exists, removing')
101-
fs.unlinkSync(readyFile)
102-
}
103-
} catch (ignore) {}
88+
return args
89+
}
10490

105-
args.push(`-f`)
106-
args.push(readyFile)
91+
function run (options, callback) {
92+
if (!fs.existsSync(tunnelLocation)) {
93+
return callback(new Error(`Tunnel jar file is not present in ${tunnelLocation}`))
94+
}
95+
96+
const checkJava = spawn('java')
97+
checkJava.on('error', err => {
98+
return callback(new Error(`Java might not be installed, necessary to use testingbot-tunnel ${err.message}`))
99+
})
100+
101+
const onReady = function () {
102+
started = true
103+
logger('Tunnel is ready')
104+
callback(null, activeTunnel)
105+
}
107106

107+
const readyFile = path.join(os.tmpdir(), 'testingbot.ready')
108108
const readyFileChecker = setInterval(() => {
109109
fs.stat(readyFile, (error, stat) => {
110110
if (!error) {
@@ -114,6 +114,18 @@ function run (options, callback) {
114114
})
115115
}, 800)
116116

117+
const args = createArgs(options)
118+
119+
try {
120+
if (fs.statSync(readyFile).isFile()) {
121+
logger('Tunnel Readyfile already exists, removing')
122+
fs.unlinkSync(readyFile)
123+
}
124+
} catch (ignore) {}
125+
126+
args.push(`-f`)
127+
args.push(readyFile)
128+
117129
if (options.verbose) {
118130
logger('Starting tunnel with options', args)
119131
}
@@ -199,3 +211,4 @@ function downloadAndRun (options, callback) {
199211

200212
module.exports = downloadAndRun
201213
module.exports.kill = killTunnel
214+
module.exports.createArgs = createArgs

test/tunnel-launcher_test.js

+12
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ describe('Tunnel Launcher', function() {
2727
});
2828
});
2929

30+
it('should correctly parse arguments', function(done) {
31+
const args = tunnelLauncher.createArgs({ apiKey: 'fake', apiSecret: 'fake', tunnelIdentifier: 'my-tunnel' });
32+
assert(args.indexOf('--tunnel-identifier') > -1);
33+
assert(args.indexOf('my-tunnel') > -1);
34+
35+
const args2 = tunnelLauncher.createArgs({ apiKey: 'fake', apiSecret: 'fake', debug: true });
36+
assert(args2.indexOf('--debug') > -1);
37+
38+
const args3 = tunnelLauncher.createArgs({ apiKey: 'fake', apiSecret: 'fake', debug: null });
39+
assert.equal(args3.indexOf('--debug'), -1);
40+
done();
41+
});
3042
});

0 commit comments

Comments
 (0)