UNPKG

6 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
18require('babel-register')({presets: ['es2015']});
19
20
21const {spawn} = require('child_process');
22const fs = require('fs-extra');
23const eslint = require('gulp-eslint');
24const glob = require('glob');
25const gulp = require('gulp');
26const gutil = require('gulp-util');
27const webdriver = require('gulp-webdriver');
28const gzipSize = require('gzip-size');
29const {rollup} = require('rollup');
30const nodeResolve = require('rollup-plugin-node-resolve');
31const babel = require('rollup-plugin-babel');
32const runSequence = require('run-sequence');
33const sauceConnectLauncher = require('sauce-connect-launcher');
34const seleniumServerJar = require('selenium-server-standalone-jar');
35const webpack = require('webpack');
36const build = require('./bin/build');
37const logBuildErrors = require('./bin/errors');
38const server = require('./test/e2e/server');
39
40
41let seleniumServer;
42let sshTunnel;
43
44
45/**
46 * @return {boolean} True if NODE_ENV is set to production or the build is
47 * running on CI.
48 */
49const isProd = () => {
50 return process.env.NODE_ENV == 'production' || process.env.CI;
51};
52
53
54gulp.task('javascript', () => {
55 if (isProd()) {
56 return build('autotrack.js').then(({code, map}) => {
57 fs.outputFileSync('autotrack.js', code, 'utf-8');
58 fs.outputFileSync('autotrack.js.map', map, 'utf-8');
59 const size = (gzipSize.sync(code) / 1000).toFixed(1);
60 gutil.log(
61 `Built autotrack.js ${gutil.colors.gray(`(${size} Kb gzipped)`)}`);
62 }).catch((err) => {
63 logBuildErrors(err);
64 throw new Error('failed to build autotrack.js');
65 });
66 } else {
67 return rollup({
68 entry: './lib/index.js',
69 plugins: [
70 nodeResolve(),
71 babel({
72 babelrc: false,
73 plugins: ['external-helpers'],
74 presets: [['es2015', {modules: false}]],
75 }),
76 ],
77 }).then((bundle) => {
78 return bundle.write({
79 dest: 'autotrack.js',
80 format: 'iife',
81 sourceMap: true,
82 });
83 });
84 }
85});
86
87
88gulp.task('javascript:unit', ((compiler) => {
89 const createCompiler = () => {
90 return webpack({
91 entry: glob.sync('./test/unit/**/*-test.js'),
92 output: {
93 path: 'test/unit',
94 filename: 'index.js',
95 },
96 devtool: '#source-map',
97 cache: {},
98 performance: {hints: false},
99 module: {
100 loaders: [{
101 test: /\.js$/,
102 exclude: /node_modules\/(?!(dom-utils)\/).*/,
103 loader: 'babel-loader',
104 query: {
105 babelrc: false,
106 cacheDirectory: false,
107 presets: [
108 ['es2015', {'modules': false}],
109 ],
110 },
111 }],
112 },
113 });
114 };
115 return (done) => {
116 (compiler || (compiler = createCompiler())).run((err, stats) => {
117 if (err) return done(err);
118 gutil.log('[webpack]', stats.toString('minimal'));
119 done();
120 });
121 };
122})());
123
124
125gulp.task('lint', () => {
126 return gulp.src([
127 'gulpfile.babel.js',
128 'bin/autotrack',
129 'bin/*.js',
130 'lib/*.js',
131 'lib/plugins/*.js',
132 'test/e2e/*.js',
133 'test/unit/**/*.js',
134 '!test/unit/index.js',
135 ])
136 .pipe(eslint())
137 .pipe(eslint.format())
138 .pipe(eslint.failAfterError());
139});
140
141
142gulp.task('test:e2e', ['javascript', 'lint', 'tunnel', 'selenium'], () => {
143 const stopServers = () => {
144 // TODO(philipwalton): re-add this logic to close the tunnel once this is
145 // fixed: https://github.com/bermi/sauce-connect-launcher/issues/116
146 // process.on('exit', sshTunnel.close.bind(sshTunnel));
147 sshTunnel.close();
148 server.stop();
149 if (!process.env.CI) {
150 seleniumServer.kill();
151 }
152 };
153 return gulp.src('./test/e2e/wdio.conf.js')
154 .pipe(webdriver())
155 .on('end', stopServers);
156});
157
158
159gulp.task('test:unit', ['javascript', 'javascript:unit'], (done) => {
160 spawn(
161 './node_modules/.bin/easy-sauce',
162 ['-c', 'test/unit/easy-sauce-config.json'],
163 {stdio: [0, 1, 2]}).on('end', done);
164});
165
166
167gulp.task('test', (done) => {
168 runSequence('test:e2e', 'test:unit', done);
169});
170
171
172gulp.task('tunnel', ['serve'], (done) => {
173 const opts = {
174 username: process.env.SAUCE_USERNAME,
175 accessKey: process.env.SAUCE_ACCESS_KEY,
176 verbose: true,
177 verboseDebugging: true,
178 };
179 sauceConnectLauncher(opts, (err, sauceConnectProcess) => {
180 if (err) {
181 done(err);
182 } else {
183 process.env.BASE_URL = 'http://localhost:8080';
184 sshTunnel = sauceConnectProcess;
185 // TODO(philipwalton): re-add this logic to close the tunnel once this is
186 // fixed: https://github.com/bermi/sauce-connect-launcher/issues/116
187 // process.on('exit', sshTunnel.close.bind(sshTunnel));
188 done();
189 }
190 });
191});
192
193
194gulp.task('serve', ['javascript', 'javascript:unit'], (done) => {
195 server.start(done);
196 process.on('exit', server.stop.bind(server));
197});
198
199
200gulp.task('selenium', (done) => {
201 // Don't start the selenium server on CI.
202 if (process.env.CI) return done();
203
204 seleniumServer = spawn('java', ['-jar', seleniumServerJar.path]);
205 seleniumServer.stderr.on('data', (data) => {
206 if (data.indexOf('Selenium Server is up and running') > -1) {
207 done();
208 }
209 });
210 process.on('exit', seleniumServer.kill.bind(seleniumServer));
211});
212
213
214gulp.task('watch', ['serve'], () => {
215 gulp.watch('./lib/**/*.js', ['javascript']);
216 gulp.watch([
217 './lib/**/*.js',
218 './test/unit/**/*-test.js',
219 ], ['javascript:unit']);
220});