Skip to content

Instantly share code, notes, and snippets.

@Ynote
Last active July 14, 2016 11:42
Show Gist options
  • Save Ynote/0168c2d982053fa9641c to your computer and use it in GitHub Desktop.
Save Ynote/0168c2d982053fa9641c to your computer and use it in GitHub Desktop.
[Node] - Simple module for Sauce Connect
# modules
spawn = require('child_process').spawn
credentials = require('sauce-labs-credentials')
scPath = 'path/to/sc/bin'
R = require('ramda')
Q = require('q')
# global var
startDeferred = null
sc = null
handleError = (data) ->
console.log '[Sauce Connect] Error while connecting on Sauce Labs'
startDeferred.reject data
handleResolve = (data) ->
console.log '[Sauce Connect] Tunnel with Sauce Labs created'
startDeferred.resolve()
# This object enables us to
# parse Sauce Connect returned data
scRes =
'HTTP response code indicated failure' : handleError
'Not authorized' : handleError
'Error' : handleError
'{"error":}' : handleError
'This version of Sauce Connect is outdated' : handleError
'Sauce Connect is up' : handleResolve
executeCallbackOnRes = (res) ->
hasAssociatedCallback = (message) ->
return res.indexOf(message) != -1
executeCallback = (key) ->
scRes[key] res
isEmpty = (array) ->
return array.length == 0
R.pipe(
R.keys,
R.filter hasAssociatedCallback
R.ifElse isEmpty, R.identity, executeCallback
)(scRes)
sauceTunnel =
start: ->
startDeferred = Q.defer()
sc = spawn(scPath, ['-u', credentials.sauceUser, '-k', credentials.sauceKey])
sc.stdout.on 'data', (data) ->
data = data.toString().trim()
console.log data
executeCallbackOnRes data
sc.stderr.on 'data', (data) ->
data = data.toString().trim()
console.log data
startDeferred.reject data
sc.on 'exit', (code) ->
console.log '[Sauce Connect] Sauce Labs tunnel disconnected'
console.log '- code: ' + code
startDeferred.promise
stop: ->
# killing the process call automatically
# Sauce Labs REST API to clean the tunnel
sc.kill('SIGTERM')
console.log 'Tunnel closed'
module.exports = sauceTunnel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment