-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgulpfile.js
321 lines (286 loc) · 8.34 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
/* global argv */
// Include Gulp and other build automation tools and utilities
// See: https://github.com/gulpjs/gulp/blob/master/docs/API.md
var gulp = require('gulp')
var $ = require('gulp-load-plugins')()
var del = require('del')
var path = require('path')
var runSequence = require('run-sequence')
var webpack = require('webpack')
var options = require('minimist')(process.argv.slice(2), {
alias: {
debug: 'd',
debugbrk: 'D',
verbose: 'V',
profile: 'p'
},
boolean: ['debug', 'debugbrk', 'verbose', 'profile'],
default: {
debug: false,
debugbrk: false,
verbose: false,
profile: false
}
})
$.util.log('[args]', ' debug = ' + options.debug)
$.util.log('[args]', 'debugbrk = ' + options.debugbrk)
$.util.log('[args]', ' verbose = ' + options.verbose)
$.util.log('[args]', ' profile = ' + options.profile)
// https://github.com/ai/autoprefixer
options.autoprefixer = [
'last 2 version'
]
var paths = {
build: 'build',
dist: 'dist',
lib: 'lib',
src: [
'src/**/*.js',
'!src/server.js',
'!src/**/__tests__/**/*.js',
'!src/**/__mocks__/**/*.js',
'!src/assets/*',
'!src/templates/*',
'!src/tests/*'
]
}
var src = {
assets: [
'src/assets/**',
'src/templates*/**'
],
server: [
paths.build + '/client.js',
paths.build + '/server.js',
paths.build + '/templates/**/*'
]
}
var watch = false
var browserSync
var DEVELOPMENT_HEADER = [
'/**',
' * Ritzy v<%= version %>',
' */'
].join('\n') + '\n'
var PRODUCTION_HEADER = [
'/**',
' * Ritzy v<%= version %>',
' *',
' * Copyright 2015, VIVO Systems, Inc.',
' * All rights reserved.',
' *',
' * This source code is licensed under the Apache v2 license found in the',
' * LICENSE.txt file in the root directory of this source tree.',
' *',
' */'
].join('\n') + '\n'
var webpackOpts = function(output, configs, debug) {
return require('./webpack.config.js')(output, configs, debug, options.verbose, options.autoprefixer)
}
var webpackCompletion = function(err, stats) {
if(err) {
throw new $.util.PluginError('webpack', err, {showStack: true})
}
var jsonStats = stats.toJson()
var statsOptions = { colors: true/*, modulesSort: 'size'*/ }
if(jsonStats.errors.length > 0) {
if(watch) {
$.util.log('[webpack]', stats.toString(statsOptions))
} else {
throw new $.util.PluginError('webpack', stats.toString(statsOptions))
}
}
if(jsonStats.warnings.length > 0 || options.verbose) {
$.util.log('[webpack]', stats.toString(statsOptions))
}
if(jsonStats.errors.length === 0 && jsonStats.warnings.length === 0) {
$.util.log('[webpack]', 'No errors or warnings.')
}
}
// Check the version of node currently being used
gulp.task('node-version', function(cb) { // eslint-disable-line no-unused-vars
return require('child_process').fork(null, {execArgv: ['--version']})
})
// The default task
gulp.task('default', ['serve'])
// Clean output directory
gulp.task('clean', ['clean:lib', 'clean:build', 'clean:dist'], del.bind(
null, ['.tmp'], {dot: true}
))
gulp.task('clean:lib', del.bind(
null, [paths.lib + '/*'], {dot: true}
))
gulp.task('clean:build', del.bind(
null, [paths.build], {dot: true}
))
gulp.task('clean:dist', del.bind(
null, [paths.dist + '/*', '!' + paths.dist + '/.git'], {dot: true}
))
// Static files
gulp.task('assets', function() {
return gulp.src(src.assets)
.pipe($.changed(paths.build))
.pipe(gulp.dest(paths.build))
.pipe($.size({title: 'assets'}))
})
var compile = function(cb, webpackConfigs) {
var started = false
function webpackCb(err, stats) {
webpackCompletion(err, stats)
if (!started) {
started = true
return cb()
}
}
var compiler = webpack(webpackOpts(paths.build, webpackConfigs, true))
if (watch) {
compiler.watch(200, webpackCb)
} else {
compiler.run(webpackCb)
}
}
gulp.task('compile:client', function(cb) {
compile(cb, {client: true})
})
gulp.task('compile:server', function(cb) {
compile(cb, {server: true})
})
// Build the app from source code
gulp.task('build', ['clean:build'], function(cb) {
runSequence(['assets', 'compile:client', 'compile:server'], cb)
})
// Build and start watching for modifications
gulp.task('build:watch', function(cb) {
watch = true
runSequence('build', function(err) {
gulp.watch(src.assets, ['assets'])
cb(err)
})
})
// Launch a Node.js/Express server
gulp.task('serve', ['build:watch'], function(cb) {
var started = false
var cp = require('child_process')
var assign = require('react/lib/Object.assign')
var nodeArgs = {}
if(options.debug) {
$.util.log('[node]', 'Node.js debug port set to 5858.')
if(!nodeArgs.execArgv) nodeArgs.execArgv = []
nodeArgs.execArgv.push('--debug=5858')
} else if(options.debugbrk) {
$.util.log('[node]', 'Node.js debug break port set to 5858.')
if (!nodeArgs.execArgv) nodeArgs.execArgv = []
nodeArgs.execArgv.push('--debug-brk=5858')
}
if(options.profile) {
$.util.log('[node]', 'Node.js v8 profiling activated. Check for v8.log.')
if(!nodeArgs.execArgv) nodeArgs.execArgv = []
nodeArgs.execArgv.push('--prof')
nodeArgs.execArgv.push('--log-timer-events')
}
var server = (function startup() {
var child = cp.fork(paths.build + '/server.js', nodeArgs, {
env: assign({NODE_ENV: 'development'}, process.env)
})
child.once('message', function(message) {
if (message.match(/^online$/)) {
if (browserSync) {
browserSync.reload()
}
if (!started) {
started = true
gulp.watch(src.server, function() {
$.util.log('Restarting development server.')
server.kill('SIGTERM')
server = startup()
})
cb()
}
}
})
return child
})()
})
// Launch BrowserSync development server
gulp.task('sync', ['serve'], function(cb) {
browserSync = require('browser-sync')
browserSync({
notify: false,
// Run as an https by setting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
https: false,
// Informs browser-sync to proxy our Express app which would run
// at the following location
proxy: 'localhost:5000'
}, cb)
process.on('exit', function() {
browserSync.exit()
})
gulp.watch([paths.build + '/**/*.*'].concat(
src.server.map(function(file) { return '!' + file })
), function(file) {
browserSync.reload(path.relative(__dirname, file.path))
})
})
gulp.task('modules', ['clean:lib'], function() {
return gulp
.src(paths.src, {base: 'src'})
.pipe($.babel())
.pipe(gulp.dest(paths.lib))
})
var dist = function(cb, header, debug, lib) {
function webpackCb(err, stats) {
webpackCompletion(err, stats)
gulp.src(paths.build + '/' + lib)
.pipe($.header(header, {
version: process.env.npm_package_version
}))
.pipe(gulp.dest(paths.dist))
return cb()
}
webpack(webpackOpts(paths.build, { lib: true, libName: lib }, debug), webpackCb)
}
gulp.task('dist:dev', ['modules'], function(cb) {
return dist(cb, DEVELOPMENT_HEADER, true, 'ritzy.js')
})
gulp.task('dist:min', ['modules'], function(cb) {
return dist(cb, PRODUCTION_HEADER, false, 'ritzy.min.js')
})
gulp.task('dist', ['clean:dist', 'dist:dev', 'dist:min'], function(cb) {
return gulp.src('src/assets/fonts/**/*', {base: 'src/assets'})
.pipe(gulp.dest(paths.dist))
})
gulp.task('docs', function() {
var tests = ['-testb?\\.js$']
gulp.src('src')
.pipe($.esdoc({
title: 'Ritzy: Collaborative web-based rich text editor',
destination: paths.dist + '/docs',
excludes: tests,
access: ['public', 'protected', 'private'],
test: {
type: 'mocha',
source: 'src/',
includes: tests
}/*,
unexportIdentifier: true*/
}))
})
// Deploy to GitHub Pages
gulp.task('deploy', function() {
// Remove temp folder
if (argv.clean) {
var os = require('os')
var repoPath = path.join(os.tmpdir(), 'tmpRepo')
$.util.log('Delete ' + $.util.colors.magenta(repoPath))
del.sync(repoPath, {force: true})
}
return gulp.src(paths.build + '/**/*')
.pipe($.if('**/robots.txt', !argv.production ?
$.replace('Disallow:', 'Disallow: /') : $.util.noop()))
.pipe($.ghPages({
remoteUrl: 'https://github.com/ritzyed/ritzy.github.io.git',
branch: 'master'
}))
})