1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | require('babel-register')({presets: ['es2015']});
|
19 |
|
20 |
|
21 | const {spawn} = require('child_process');
|
22 | const fs = require('fs-extra');
|
23 | const eslint = require('gulp-eslint');
|
24 | const glob = require('glob');
|
25 | const gulp = require('gulp');
|
26 | const gutil = require('gulp-util');
|
27 | const webdriver = require('gulp-webdriver');
|
28 | const gzipSize = require('gzip-size');
|
29 | const {rollup} = require('rollup');
|
30 | const nodeResolve = require('rollup-plugin-node-resolve');
|
31 | const babel = require('rollup-plugin-babel');
|
32 | const runSequence = require('run-sequence');
|
33 | const sauceConnectLauncher = require('sauce-connect-launcher');
|
34 | const seleniumServerJar = require('selenium-server-standalone-jar');
|
35 | const webpack = require('webpack');
|
36 | const build = require('./bin/build');
|
37 | const logBuildErrors = require('./bin/errors');
|
38 | const server = require('./test/e2e/server');
|
39 |
|
40 |
|
41 | let seleniumServer;
|
42 | let sshTunnel;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | const isProd = () => {
|
50 | return process.env.NODE_ENV == 'production' || process.env.CI;
|
51 | };
|
52 |
|
53 |
|
54 | gulp.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 |
|
88 | gulp.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 |
|
125 | gulp.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 |
|
142 | gulp.task('test:e2e', ['javascript', 'lint', 'tunnel', 'selenium'], () => {
|
143 | const stopServers = () => {
|
144 |
|
145 |
|
146 |
|
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 |
|
159 | gulp.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 |
|
167 | gulp.task('test', (done) => {
|
168 | runSequence('test:e2e', 'test:unit', done);
|
169 | });
|
170 |
|
171 |
|
172 | gulp.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 |
|
186 |
|
187 |
|
188 | done();
|
189 | }
|
190 | });
|
191 | });
|
192 |
|
193 |
|
194 | gulp.task('serve', ['javascript', 'javascript:unit'], (done) => {
|
195 | server.start(done);
|
196 | process.on('exit', server.stop.bind(server));
|
197 | });
|
198 |
|
199 |
|
200 | gulp.task('selenium', (done) => {
|
201 |
|
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 |
|
214 | gulp.task('watch', ['serve'], () => {
|
215 | gulp.watch('./lib/**/*.js', ['javascript']);
|
216 | gulp.watch([
|
217 | './lib/**/*.js',
|
218 | './test/unit/**/*-test.js',
|
219 | ], ['javascript:unit']);
|
220 | });
|