summaryrefslogtreecommitdiffstats
path: root/tasks/common.js
blob: d4ba65820311cecf6ea851dd762dc8b9f4abaca7 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var args = require('yargs').argv;
var chalk = require('chalk');
var wct = require('web-component-tester').test;
var _ = require('lodash');
var gutil = require('gulp-util');

function cleanDone(done) {
  return function(error) {
    if (error) {
      // Pretty error for gulp.
      error = new Error(chalk.red(error.message || error));
      error.showStack = false;
    }
    done(error);
  };
}

function checkArguments(arguments) {
  _.forEach(arguments, function(a) {
    if(!args.hasOwnProperty(a)) {
      throw Error('Required argument \'--'+ a +'\' is missing.');
    }
  });
}

function localAddress() {
  var ip, tun, ifaces = require('os').networkInterfaces();
  Object.keys(ifaces).forEach(function (ifname) {
    ifaces[ifname].forEach(function (iface) {
      if ('IPv4' == iface.family && !iface.internal) {
        if (!ip) ip = iface.address;
        if (/tun/.test(ifname)) tun = iface.address;
      }
    });
  });
  return tun || ip;
}

function test(options, done) {
  wct(options, cleanDone(done));
}

module.exports = {
  localAddress: localAddress,
  test: test,
  checkArguments: checkArguments,

  testSauce: function(suites, browsers, build, done) {
    test(
      {
        suites: suites,
        browserOptions: {
          name: localAddress() + ' / ' + new Date(),
          build: build
        },

        plugins: {
          //local: {
          //  browsers: ['chrome']
          //},
          sauce: {
            username: args.sauceUsername,
            accessKey: args.sauceAccessKey,
            browsers: browsers
          },
          'teamcity-reporter': args.teamcity
        },
        webserver: {
          hostname: localAddress()
        }
    }, done);
  },

  autoRevert: function(err, handler, done) {
    if(err) {
      gutil.log(err.toString());
      if(args.autoRevert) {
        handler();
      } else {
        gutil.log('No action. Use --auto-revert to revert changes.')
        done(err);
      }
    } else {
      done();
    }
  }
};