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 5
/
gulpfile.js
289 lines (242 loc) · 7.67 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
/*eslint-disable */
var fs = require('fs')
var gulp = require('gulp')
var source = require('vinyl-source-stream')
var rename = require('gulp-rename')
var browserify = require('browserify')
var buffer = require('vinyl-buffer')
var uglify = require('gulp-uglify')
var taskListing = require('gulp-task-listing')
var awspublish = require('gulp-awspublish')
var injectVersion = require('gulp-inject-version')
var derequire = require('gulp-derequire')
var es = require('event-stream')
var karma = require('karma')
var runSequence = require('run-sequence')
var replace = require('gulp-replace')
var connect = require('gulp-connect')
require('gulp-release-tasks')(gulp)
var jeditor = require('gulp-json-editor')
var saucelabsConfig = {
user: process.env.SAUCE_USERNAME || 'opbeat',
key: process.env.SAUCE_ACCESS_KEY || 'de42e589-1450-41a2-8a44-90aa00c15168',
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 createBuildStream (mainFilePath, version) {
if (typeof version !== 'string') {
throw new Error(mainFilePath + ' Expected version as string but got:' + version)
}
return browserify({
entries: [mainFilePath],
standalone: '',
insertGlobalVars: { define: function () { return 'undefined'; }, process: function () { return 'undefined'; } }
})
.bundle()
.pipe(source(mainFilePath))
.pipe(rename({ dirname: '' }))
.pipe(buffer())
.pipe(replace(
new RegExp(RegExp.escape('%%VERSION%%'), 'g'),
'v' + version
))
.pipe(derequire())
}
function writeToDestinations (stream, dests) {
var tasks = dests.map(function (destPath) {
return stream.pipe(gulp.dest(versionPath))
})
return es.merge.apply(null, tasks)
}
function getMajorVersion () {
var version = require('./package').version
var majorVersion = version.match(/^(\d).(\d).(\d)/)[1]
return majorVersion
}
gulp.task('build:release', function () {
var version = require('./package').version
var majorVersion = version.match(/^(\d).(\d).(\d)/)[1]
var versionPath = './dist/cdn/' + majorVersion
var prodPath = './dist/'
var integrations = require('./release/integrations')
var tasks = Object.keys(integrations).map(function (key) {
var integration = integrations[key]
var integrationName = key
var mainStream = createBuildStream(integration.entry, integration.version)
.pipe(gulp.dest(versionPath))
.pipe(gulp.dest(prodPath))
.pipe(gulp.dest(prodPath + integrationName))
.pipe(rename({
extname: '.min.js'
}))
.pipe(uglify())
.pipe(gulp.dest(versionPath))
.pipe(gulp.dest(prodPath))
.pipe(gulp.dest(prodPath + integrationName))
var filename = integration.entry.split('/')
filename = filename[filename.length - 1]
var packagejson = gulp.src(['./release/templates/*.json'])
.pipe(jeditor({
'name': integrationName,
'version': integration.version,
'main': filename,
'description': integration.description
}))
.pipe(gulp.dest(prodPath + integrationName))
return es.merge.apply(null, [mainStream, packagejson, gulp.src(['LICENSE']).pipe(gulp.dest(prodPath + integrationName))])
})
return es.merge.apply(null, tasks)
})
gulp.task('build', function () {
var integrations = require('./release/integrations')
var tasks = Object.keys(integrations).map(function (key) {
var entry = integrations[key].entry
var version = integrations[key].version
return createBuildStream(entry, version)
.pipe(gulp.dest('./dist/dev/'))
.pipe(rename({
extname: '.min.js'
}))
.pipe(uglify())
.pipe(gulp.dest('./dist/dev/'))
})
return es.merge.apply(null, tasks)
})
// Development mode
gulp.task('watch', [], function (cb) {
gulp.run(
'build',
'server'
)
// Watch JS files
gulp.watch(['libs/**', 'src/**'], function () { runSequence('build', 'karma-run') })
console.log('\nExample site running on http://localhost:7000/\n')
})
//
// Deploy task
//
gulp.task('deploy', ['build:release'], function () {
// Load options from file
awsoptions = JSON.parse(fs.readFileSync('aws.json'))
// Hardcoded bucketname, to avoid mistakes
awsoptions.params = {
Bucket: 'opbeat-js-cdn'
}
// Create new publisher
var publisher = awspublish.create(awsoptions)
// Set headers
var headers = {
'Cache-Control': 'max-age=1800, public'
}
var version = require('./package').version
var majorVersion = version.match(/^(\d).(\d).(\d)/)[1]
var versionPath = './dist/cdn/**'
console.warn('Uploading All files in:', versionPath)
return gulp.src([versionPath])
// Gzip
.pipe(awspublish.gzip())
// Publish files with headers
.pipe(publisher.publish(headers))
// Create a cache file to speed up consecutive uploads
.pipe(publisher.cache())
// Print upload updates to console
.pipe(awspublish.reporter())
})
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()
})
// Launch sauce connect and connect
gulp.task('test:launchsauceconnect', function (done) {
var sauceConnectLauncher = require('sauce-connect-launcher')
var config = {
username: saucelabsConfig.user,
accessKey: saucelabsConfig.key,
logger: console.log,
noSslBumpDomains: 'all'
}
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)
})
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 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) {
var sequenceArgs = ['test', function (err) {
if (err) {
return taskFailed(err)
} else {
return sequenceSucceeded(done)
}
}]
var isSauce = process.env.MODE && process.env.MODE.startsWith('saucelabs')
if (isSauce) {
sequenceArgs.unshift(['test:launchsauceconnect'])
}
runSequence.apply(this, sequenceArgs)
})
gulp.task('default', taskListing)