forked from anupamm/yours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
352 lines (309 loc) · 10.9 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
347
348
349
350
351
352
'use strict'
let babelify = require('babelify')
let browserSyncCreator = require('browser-sync')
let browserify = require('browserify')
let config = require('datt/config')
let createAppServer = require('./server/app').createAppServer
let createRendezvousServer = require('datt/server/rendezvous').createRendezvousServer
let envify = require('envify')
let exec = require('child_process').exec
let fs = require('fs')
let glob = require('glob')
let gulp = require('gulp')
let gutil = require('gulp-util')
let karma = require('gulp-karma')
let mocha = require('gulp-mocha')
let path = require('path')
let plumber = require('gulp-plumber')
let q = require('q')
let watch = require('gulp-watch')
let watchify = require('watchify')
let browserSyncs = []
browserSyncs['3040'] = browserSyncCreator.create()
browserSyncs['3041'] = browserSyncCreator.create()
browserSyncs['3042'] = browserSyncCreator.create()
browserSyncs['3043'] = browserSyncCreator.create()
browserSyncs['3044'] = browserSyncCreator.create()
let jsfiles = ['*.js', 'bin/*.js', 'react/**/*.js', 'react/**/*.jsx', 'core/**/*.js', 'test/**/*.js', 'test/**/*.jsx']
let cssfiles = ['build/main.css']
let allfiles = [].concat(jsfiles).concat(cssfiles)
let browserifyOpts
// For some weird reason, while watching files, they appear not to exist
// immediately after noticing they've changed. Thus we set a timeout not to
// rebuild bundles for a brief period after a file has changed. If this timeout
// isn't set, there will often be a "file doesn't exist" error while rebuilding
// bundles that depend on the changed files. At least, that's what happens on
// Linux - presumably this is a platform issue.
let watchifytimeout = 1000
function build_workerpool () {
return new Promise((resolve, reject) => {
fs.createReadStream(path.join(__dirname, 'node_modules', 'workerpool', 'dist', 'workerpool.js'))
.pipe(fs.createWriteStream(path.join(__dirname, 'build', config.DATT_CORE_JS_WORKERPOOL_FILE)))
.on('close', resolve)
})
}
gulp.task('build-workerpool', function () {
return build_workerpool()
})
browserifyOpts = {debug: false}
browserifyOpts = Object.assign({}, watchify.args, browserifyOpts)
let build_worker_browserify
function set_build_worker_browserify () {
build_worker_browserify = watchify(browserify(browserifyOpts))
build_worker_browserify
// The polyfill needs to be included exactly once per page. We put it in
// the worker and in the bundle.
.add(require.resolve('babel-polyfill'))
.transform(envify)
.transform(babelify, {presets: ['es2015', 'react'], sourceMaps: false})
.add(require.resolve('datt/core/worker.js'), {entry: true})
}
set_build_worker_browserify()
function build_worker () {
return new Promise((resolve, reject) => {
q.delay(watchifytimeout).then(() => {
build_worker_browserify
.bundle()
.on('error', (err) => {
gutil.log.bind(gutil, 'Browserify Error')
build_worker_browserify.close()
set_build_worker_browserify()
reject(err)
})
.pipe(fs.createWriteStream(path.join(__dirname, 'build', config.DATT_CORE_JS_WORKER_FILE)))
.once('finish', resolve)
.once('error', reject)
})
})
}
gulp.task('build-worker', ['build-workerpool'], function () {
return build_worker()
})
browserifyOpts = {debug: false}
browserifyOpts = Object.assign({}, watchify.args, browserifyOpts)
let build_core_browserify
function set_build_core_browserify () {
build_core_browserify = watchify(browserify(browserifyOpts))
build_core_browserify
.on('error', gutil.log.bind(gutil, 'Unknown Error'))
// The polyfill needs to be included exactly once per page. We put it in
// the worker and in the bundle.
.add(require.resolve('babel-polyfill'))
.transform(envify)
.transform(babelify, {presets: ['es2015', 'react'], sourceMaps: false})
.require(require.resolve('datt/core/index.js'), {entry: true})
}
set_build_core_browserify()
function build_core () {
return new Promise((resolve, reject) => {
q.delay(watchifytimeout).then(() => {
build_core_browserify
.bundle()
.on('error', (err) => {
gutil.log.bind(gutil, 'Browserify Error')
build_core_browserify.close()
set_build_core_browserify()
reject(err)
})
.pipe(fs.createWriteStream(path.join(__dirname, 'build', config.DATT_CORE_JS_BUNDLE_FILE)))
.once('finish', resolve)
.once('error', reject)
})
})
}
gulp.task('build-core', ['build-worker'], () => {
return build_core()
})
browserifyOpts = {debug: false}
browserifyOpts = Object.assign({}, watchify.args, browserifyOpts)
let build_react_browserify
function set_build_react_browserify () {
build_react_browserify = watchify(browserify(browserifyOpts))
build_react_browserify
// Do not include the polyfill - it is already included by datt-core.js
.transform('reactify')
.transform(babelify, {presets: ['es2015', 'react'], sourceMaps: false})
.add(require.resolve('./react/index.js'), {entry: true})
}
set_build_react_browserify()
function build_react () {
let p1 = new Promise((resolve, reject) => {
fs.createReadStream(path.join(__dirname, 'node_modules', 'bootstrap', 'dist', 'css', 'bootstrap.css'))
.pipe(fs.createWriteStream(path.join(__dirname, 'build', 'bootstrap.css')))
.on('close', resolve)
.on('error', reject)
})
let p2 = new Promise((resolve, reject) => {
q.delay(watchifytimeout).then(() => {
build_react_browserify
.bundle()
.on('error', (err) => {
gutil.log.bind(gutil, 'Browserify Error')
build_react_browserify.close()
set_build_react_browserify()
reject(err)
})
.pipe(fs.createWriteStream(path.join(__dirname, 'build', config.DATT_REACT_JS_FILE)))
.once('finish', resolve)
.once('error', reject)
})
})
return p1.then(function () {
return p2
})
}
gulp.task('build-react', function () {
return build_react()
})
browserifyOpts = {debug: false}
browserifyOpts = Object.assign({}, watchify.args, browserifyOpts)
let build_tests_browserify
function set_build_tests_browserify () {
return new Promise((resolve, reject) => {
glob('./test/**/*+(.js|.jsx)', {}, (err, files) => {
build_tests_browserify = watchify(browserify(browserifyOpts))
if (err) {
reject(err)
return
}
build_tests_browserify
.transform(envify)
.transform(babelify, {presets: ['es2015', 'react'], sourceMaps: false})
.ignore('jsdom')
for (let file of files) {
build_tests_browserify.add(file)
}
resolve()
})
})
}
gulp.task('build-tests-prebundle', () => {
return set_build_tests_browserify()
})
function build_tests () {
return new Promise((resolve, reject) => {
q.delay(watchifytimeout).then(() => {
build_tests_browserify
.bundle()
.on('error', (err) => {
gutil.log.bind(gutil, 'Browserify Error')
build_tests_browserify.close()
set_build_tests_browserify().then(function () {
reject(err)
}).catch((error) => {
reject(err + ', ' + error)
})
})
.pipe(fs.createWriteStream(path.join(__dirname, 'build', config.DATT_JS_TESTS_FILE)))
.on('finish', resolve)
})
})
}
gulp.task('build-tests', ['build-tests-prebundle', 'build-core', 'build-worker'], () => {
return build_tests()
})
gulp.task('build-mocha', () => {
// copy the mocha js and css files to our build directory so you can use them
// in the tests HTML file
let p1 = new Promise((resolve, reject) => {
fs.createReadStream(path.join(__dirname, 'node_modules', 'mocha', 'mocha.js'))
.pipe(fs.createWriteStream(path.join(__dirname, 'build', 'mocha.js')))
.on('close', resolve)
.on('error', reject)
})
let p2 = new Promise((resolve, reject) => {
fs.createReadStream(path.join(__dirname, 'node_modules', 'mocha', 'mocha.css'))
.pipe(fs.createWriteStream(path.join(__dirname, 'build', 'mocha.css')))
.on('close', resolve)
.on('error', reject)
})
return p1.then(() => {
return p2
})
})
gulp.task('build', ['build-react', 'build-core', 'build-tests', 'build-mocha'])
gulp.task('build-exit', ['build'], () => {
process.exit()
})
function test_node (end) {
return gulp.src(['./test/*.js', './test/**/*.js', './test/**/*.jsx'])
.pipe(plumber()) // keeps gulp from crashing when there is an exception
.pipe(mocha({
reporter: 'dot',
compilers: {
jsx: require('babel-core/register')
}
}))
.once('end', () => {
if (end) {
process.exit()
}
})
}
gulp.task('test-node', () => {
return test_node(true)
})
gulp.task('watch-test-node', () => {
// runs the mocha node tests and runs js standard on all the files
watch(jsfiles, function () {
q.delay(watchifytimeout).then(() => {
exec('node_modules/.bin/standard *.js ./server/**/*.js ./react/**/*.js ./react/**/*.jsx ./core/**/*.js ./test/**/*.js', {cwd: __dirname}, (err, stdout, stderr) => {
if (err) {
console.log(stdout)
}
test_node()
})
})
})
})
gulp.task('build-karma-url', () => {
// karma serves static files, including js files, from /base/
config.DATT_JS_BASE_URL = '/base/'
})
gulp.task('build-karma', ['build-karma-url', 'build'])
gulp.task('test-karma', ['build-karma'], () => {
let rendezvousServer = createRendezvousServer(3031)
let appServer = createAppServer(3030)
return gulp.src([])
.pipe(karma({
configFile: '.karma.conf.js',
action: 'run'
}))
.on('error', () => {
process.exit(1)
})
.on('end', () => {
rendezvousServer.close()
appServer.close()
process.exit()
})
})
gulp.task('watch-test-karma', () => {})
gulp.task('build-browsersync', ['build'], () => {
browserSyncs['3040'].reload()
browserSyncs['3041'].reload()
browserSyncs['3042'].reload()
browserSyncs['3043'].reload()
browserSyncs['3044'].reload()
})
gulp.task('serve', ['build'], () => {
// Create two rendezvous servers, one for the tests and one for the UI, so
// that network connections do not overlap
createRendezvousServer(3031) // For the tests (i.e., localhost:3040/tests.html)
createRendezvousServer(3032) // For the UI (i.e., localhost:3040)
// One app server, that delivers the app and the tests file
createAppServer(3030)
let config = {
ui: false,
proxy: 'http://localhost:3030',
open: false // don't automatically open browser window
}
console.log('browser-sync proxy on ports 3040 - 3044')
browserSyncs['3040'].init(Object.assign({port: 3040}, config))
browserSyncs['3041'].init(Object.assign({port: 3041}, config))
browserSyncs['3042'].init(Object.assign({port: 3042}, config))
browserSyncs['3043'].init(Object.assign({port: 3043}, config))
browserSyncs['3044'].init(Object.assign({port: 3044}, config))
gulp.watch(allfiles, ['build-browsersync'])
})
gulp.task('default', ['build-exit'])