This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
346 lines (298 loc) · 9.04 KB
/
gulpfile.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*eslint-disable */
var fs = require('fs')
var gulp = require('gulp')
var taskListing = require('gulp-task-listing')
var karma = require('karma')
var runSequence = require('run-sequence')
var webdriver = require('gulp-webdriver')
var selenium = require('selenium-standalone')
var replace = require('gulp-replace')
var webpack = require('webpack')
var afterAll = require('after-all')
var path = require('path')
var connect = require('gulp-connect')
require('gulp-release-tasks')(gulp)
var webdriverConfig = {
user: process.env.SAUCE_USERNAME || 'opbeat-react',
key: process.env.SAUCE_ACCESS_KEY || '699838bc-49b3-4c92-adf0-690ba855e0d6',
host: process.env.SAUCE_HOST || 'ondemand.saucelabs.com',
port: process.env.SAUCE_PORT || 80,
baseUrl: process.env.SAUCE_BASEURL || 'http://localhost:8000'
}
// Static file server
gulp.task('examples:serve', function () {
connect.server({
root: ['examples', 'dist'],
port: 7000,
livereload: false,
open: false
})
})
function getMajorVersion () {
var version = require('./package').version
var majorVersion = version.match(/^(\d).(\d).(\d)/)[1]
return majorVersion
}
gulp.task('build:e2e', function (done) {
var dirNeedsBuilding = [
'./test/e2e/router/webpack.config.js',
'./test/e2e/router/webpack.server.config.js',
'./test/e2e/react/webpack.config.js',
'./test/e2e/redux/webpack.config.js',
'./test/e2e/no-init/webpack.config.js',
'./test/e2e/fetch/webpack.config.js'
]
var left = dirNeedsBuilding.length
var next = afterAll(done)
dirNeedsBuilding.forEach(function (dir) {
var buildDone = next()
console.log('Building', dir)
var webpackConfig = require(dir)
webpack(webpackConfig).run(function (err, stats) {
if (err) throw err // throw err
if (stats.hasErrors()) console.log('!! there were errors building', dir)
var jsonStats = stats.toJson()
if (jsonStats.errors.length > 0) {
jsonStats.errors.forEach(function (error) {
console.log('Error:', error)
})
}
buildDone()
})
})
})
gulp.task('build:release', function (done) {
var prodPath = './dist/opbeat-react'
var version = require('./package.json').version
var next = afterAll(done)
gulp.src(['src/**/*.js'])
.pipe(replace(
new RegExp(RegExp.escape('%%VERSION%%'), 'g'),
'v' + version
))
.pipe(gulp.dest(prodPath))
.on('end', next())
gulp.src(['./README.md', './package.json', './LICENSE'])
.pipe(gulp.dest(prodPath))
.on('end', next())
})
// Development mode
gulp.task('watch', [], function (cb) {
gulp.run(
'build',
'examples:serve'
)
// Watch JS files
gulp.watch(['libs/**', 'src/**'], function () { runSequence('build', 'karma-run') })
console.log('\nExample site running on http://localhost:7000/\n')
})
function runKarma (configFile, done) {
var exec = require('child_process').exec
var cmd = process.platform === 'win32' ? 'node_modules\\.bin\\karma run ' :
'node node_modules/.bin/karma run '
cmd += configFile
exec(cmd, function (e, stdout) {
// ignore errors, we don't want to fail the build in the interactive (non-ci) mode
// karma server will print all test failures
done()
})
}
gulp.task('karma-run', function (done) {
// run the run command in a new process to avoid duplicate logging by both server and runner from
// a single process
runKarma('karma.conf.js', done)
})
gulp.task('test', function (done) {
new karma.Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start()
})
// Run end-to-end tests on the local machine using webdriver configuration
gulp.task('test:e2e:run', function (done) {
gulp.src('wdio.conf.js')
.pipe(webdriver())
.on('error', function () {
return process.exit(1)
})
.on('end', function () {
return process.exit(0)
})
})
gulp.task('test:e2e:sauceconnect:failsafe', function () {
var failSafeStream = gulp.src('wdio.failsafe.conf.js')
.pipe(webdriver(webdriverConfig))
.on('error', function () {
console.log('Exiting process with status 1')
process.exit(1)
})
.on('end', function () {
console.log('Tests complete')
})
return failSafeStream
})
// Run end-to-end tests remotely in saucelabs using webdriver configuration
gulp.task('test:e2e:sauceconnect', ['test:e2e:sauceconnect:failsafe'], function () {
var e2eStream = gulp.src('wdio.sauce.conf.js')
.pipe(webdriver(webdriverConfig))
.on('error', function () {
console.log('Exiting process with status 1')
process.exit(1)
})
.on('end', function () {
console.log('Tests complete')
})
return e2eStream
})
// Launch sauce connect and connect
gulp.task('test:e2e:launchsauceconnect', function (done) {
var sauceConnectLauncher = require('sauce-connect-launcher')
var config = {
username: webdriverConfig.user,
accessKey: webdriverConfig.key,
logger: console.log
}
var tryConnect = function (maxAttempts, currAttempts, done) {
sauceConnectLauncher(config, function (err, sauceConnectProcess) {
if (err) {
console.error(err.message)
if (currAttempts <= maxAttempts) {
console.log('Retrying... (attempt ' + currAttempts + ' of ' + maxAttempts + ')')
tryConnect(maxAttempts, ++currAttempts, done)
} else {
return process.exit(1)
}
} else {
console.log('Sauce Connect ready')
done()
}
})
}
tryConnect(3, 1, done)
})
// Serve test application
gulp.task('test:e2e:serve', function () {
return connect.server({
root: ['test/e2e', 'src', './'],
port: 8000,
livereload: false,
open: false,
middleware: function (connect, opt) {
var middlewares = []
middlewares.push(connect.favicon())
// used to test truncated XHR traces
middlewares.push(function (request, response, next) {
if (request.url == '/slow-response') {
setTimeout(function () { response.write('Slow!'); response.end()}, 5000)
}else {
next()
}
})
return middlewares
}
})
})
gulp.task('test:e2e:run-ssr', function () {
var childProcess = require('child_process')
var path = require('path')
var cp = childProcess.fork(path.join(__dirname, 'test/e2e/router/server.bundle.js'))
onExit(function () {
cp.kill()
})
cp.on('error', console.error.bind(console))
})
function onExit (callback) {
function exitHandler (err) {
try {
callback(err)
}
finally {
if (err) console.log(err.stack)
}
}
process.on('exit', exitHandler)
process.on('SIGINT', exitHandler)
process.on('uncaughtException', exitHandler)
}
function startSelenium (callback, manualStop) {
selenium.install({ logger: console.log }, function (installError) {
if (installError) {
console.log('Error while installing selenium:', installError)
}
selenium.start(function (startError, child) {
if (startError) {
console.log('Error while starting selenium:', startError)
return process.exit(1)
} else {
console.log('Selenium started!')
function killSelenium () {
child.kill()
console.log('Just killed selenium!')
}
if (manualStop) {
callback(killSelenium)
}else {
onExit(killSelenium)
callback()
}
}
})
})
}
// Install and start selenium
gulp.task('test:e2e:selenium', function (done) {
startSelenium(function () {
done()
})
})
gulp.task('test:e2e:start-local', ['test:e2e:serve', 'test:e2e:selenium', 'test:e2e:run-ssr'])
gulp.task('test:e2e:react-run', function (done) {
runSequence('build:release', 'build:e2e', 'test:e2e:start-local', 'test:e2e:run', function (err) {
if (err) {
return taskFailed(err)
} else {
return sequenceSucceeded(done)
}
})
})
// Run all required tasks to perform remote end-to-end testing
gulp.task('test:e2e:start-sauce', function (done) {
runSequence('build:release', 'test:e2e:launchsauceconnect', function () {
console.log('All tasks completed.')
})
})
gulp.task('test:e2e', function (done) {
runSequence('build:e2e', ['build:release', 'test:e2e:start-local'], ['test:e2e:launchsauceconnect'], 'test:e2e:sauceconnect', function (err) {
if (err) {
return taskFailed(err)
} else {
return sequenceSucceeded(done)
}
})
})
function taskFailed (err) {
var exitCode = 2
console.log('[ERROR] gulp build task failed', err)
console.log('[FAIL] gulp build task failed - exiting with code ' + exitCode)
return process.exit(exitCode)
}
function sequenceSucceeded (done) {
console.log('All tasks completed.')
done()
return process.exit(0)
}
gulp.task('test:unit:sauce', function (done) {
runSequence(['build:release', 'test:e2e:launchsauceconnect'], 'test', function (err) {
if (err) {
return taskFailed(err)
} else {
return sequenceSucceeded(done)
}
})
})
gulp.task('watch:e2e', ['e2e-serve', 'selenium-start'], function (done) {
gulp.watch(['test/e2e/**'], function () {
runSequence('test:e2e')
})
})
gulp.task('default', taskListing)