UNPKG

3.52 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 */
10var _ = require('lodash');
11var chalk = require('chalk');
12var cleankill = require('cleankill');
13var fs = require('fs');
14var path = require('path');
15var sauceConnect = require('sauce-connect-launcher');
16var temp = require('temp');
17var uuid = require('uuid');
18
19/**
20 * @param {!Object} config
21 * @param {!EventEmitter} emitter
22 * @param {function(*, string)} done
23 */
24function startTunnel(config, emitter, done) {
25 if (!config.username || !config.accessKey) {
26 return done('Missing Sauce credentials. Did you forget to set SAUCE_USERNAME and/or SAUCE_ACCESS_KEY?');
27 }
28
29 // If anything goes wrong, sc tends to have a bit more detail in its log, so
30 // let's make that easy(ish) to get at:
31 temp.mkdir('wct', function(error, logDir) {
32 if (error) return done(error);
33 var logPath = path.join(logDir, 'sc.log');
34
35 var connectOptions = {
36 username: config.username,
37 accessKey: config.accessKey,
38 tunnelIdentifier: uuid.v4(),
39 logger: emitter.emit.bind(emitter, 'log:debug'),
40 logfile: logPath,
41 port: config.port
42 };
43 _.assign(connectOptions, config.tunnelOptions);
44 var tunnelId = connectOptions.tunnelIdentifier;
45
46 emitter.emit('log:info', 'Creating Sauce Connect tunnel');
47 emitter.emit('log:info', 'Sauce Connect log:', chalk.magenta(logPath));
48
49 setSauceConnectDownloadVersion();
50
51 sauceConnect(connectOptions, function(error, tunnel) {
52 if (error) {
53 emitter.emit('log:error', 'Sauce tunnel failed:');
54 } else {
55 emitter.emit('log:info', 'Sauce tunnel active:', chalk.yellow(tunnelId));
56 emitter.emit('sauce:tunnel-active', tunnelId);
57 }
58 done(error, tunnelId);
59 });
60 // SauceConnectLauncher only supports one tunnel at a time; this allows us to
61 // kill it before we've gotten our callback.
62 cleankill.onInterrupt(sauceConnect.kill.bind(sauceConnect));
63 });
64}
65
66function isTravisSauceConnectRunning() {
67 // https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
68 if (!process.env.TRAVIS) {
69 return false;
70 }
71
72 try {
73 // when using the travis sauce_connect addon, the file
74 // /home/travis/sauce-connect.log is written to with the sauce logs.
75 // If this file exists, then the sauce_connect addon is in use
76 // If fs.statSync throws, then the file does not exist
77 var travisScLog = path.join(process.env.HOME, 'sauce-connect.log');
78 if (fs.statSync(travisScLog)) {
79 return true;
80 }
81 return false;
82 } catch (e) {
83 return false;
84 }
85}
86
87// sauce-connect-launcher only checks the environment variable
88// $SAUCE_CONNECT_VERSION
89function setSauceConnectDownloadVersion() {
90 process.env.SAUCE_CONNECT_VERSION = '4.4.0';
91}
92
93module.exports = {
94 startTunnel: startTunnel,
95 isTravisSauceConnectRunning: isTravisSauceConnectRunning,
96 setSauceConnectDownloadVersion: setSauceConnectDownloadVersion
97};