UNPKG

2.75 kBJavaScriptView Raw
1/**
2 * Copyright 2016 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18var browserify = require('browserify');
19var buffer = require('vinyl-buffer');
20var connect = require('connect');
21var fs = require('fs');
22var gulp = require('gulp');
23var gutil = require('gulp-util');
24var seleniumServerJar = require('selenium-server-standalone-jar');
25var shell = require('shelljs');
26var serveStatic = require('serve-static');
27var source = require('vinyl-source-stream');
28var sourcemaps = require('gulp-sourcemaps');
29var spawn = require('child_process').spawn;
30var uglify = require('gulp-uglify');
31var webdriver = require('gulp-webdriver');
32
33
34var server;
35
36
37gulp.task('javascript', function(done) {
38
39 // Gets the license string from this file, the first 15 lines.
40 var license = fs.readFileSync(__filename, 'utf-8')
41 .split('\n').slice(0, 15).join('\n');
42
43 browserify('./', {
44 debug: true
45 })
46 .bundle()
47
48 // TODO(philipwalton): Add real error handling.
49 // This temporary hack fixes an issue with tasks not restarting in
50 // watch mode after a syntax error is fixed.
51 .on('error', function(err) { gutil.beep(); done(err); })
52 .on('end', done)
53
54 .pipe(source('./autotrack.js'))
55 .pipe(buffer())
56 .pipe(sourcemaps.init({loadMaps: true}))
57 .pipe(uglify({output: {preamble: license}}))
58 .pipe(sourcemaps.write('./'))
59 .pipe(gulp.dest('./'));
60});
61
62
63gulp.task('test', ['javascript', 'serve', 'selenium'], function() {
64 function stopServers() {
65 server.close();
66 if (!process.env.CI) seleniumServer.kill();
67 }
68 return gulp.src('./wdio.conf.js')
69 .pipe(webdriver())
70 .on('end', stopServers);
71});
72
73
74gulp.task('serve', ['javascript'], function(done) {
75 server = connect().use(serveStatic('./')).listen(8080, done);
76});
77
78
79gulp.task('selenium', function(done) {
80 // Don't start the selenium server on CI.
81 if (process.env.CI) return done();
82
83 seleniumServer = spawn('java', ['-jar', seleniumServerJar.path]);
84 seleniumServer.stderr.on('data', function(data) {
85 if (data.indexOf('Selenium Server is up and running') > -1) {
86 done();
87 }
88 });
89});
90
91
92gulp.task('watch', ['serve'], function() {
93 gulp.watch('./lib/**/*.js', ['javascript']);
94});
95
96
97gulp.task('build', ['test']);