This repository has been archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
378 lines (346 loc) · 11.7 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulp_debug = require('gulp-debug');
var gulp_size = require('gulp-size');
var gulp_concat = require('gulp-concat');
var gulp_replace = require('gulp-replace');
var gulp_cached = require('gulp-cached');
var gulp_newer = require('gulp-newer');
var gulp_filter = require('gulp-filter');
var gulp_plumber = require('gulp-plumber');
var gulp_notify = require('gulp-notify');
var gulp_less = require('gulp-less');
var gulp_uglify = require('gulp-uglify');
var gulp_minify_css = require('gulp-minify-css');
var gulp_rename = require('gulp-rename');
var gulp_ng_template = require('gulp-angular-templatecache');
var gulp_jshint = require('gulp-jshint');
var jshint_stylish = require('jshint-stylish');
var vinyl_buffer = require('vinyl-buffer');
var vinyl_source_stream = require('vinyl-source-stream');
var browserify = require('browserify');
var event_stream = require('event-stream');
var gulp_mocha = require('gulp-mocha');
var gulp_istanbul = require('gulp-istanbul');
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var dotenv = require('dotenv');
var through2 = require('through2');
var bower = require('bower');
var Q = require('q');
var _ = require('lodash');
var cf_config = require('./src/utils/cf_config');
var del = require('del');
// NOTE !!!
// process.env is not available during heroku builds because .env is not loaded there,
// se we can only test for the absence of keys, but not for their values!
if (!process.env.PORT) {
console.log('loading .env file ( no foreman ;)');
dotenv.load();
}
var CDN = '';
if (process.env.DEV_MODE !== 'true') {
CDN = cf_config.DEFAULT_PROTO + cf_config.WEBSITE_CF;
console.log('CDN', CDN);
}
var active_server;
function leave_no_wounded(err) {
if (err) {
console.log(err.stack);
}
if (active_server) {
console.log('LEAVE NO WOUNDED - kill active server', active_server.pid);
active_server.removeAllListeners('error');
active_server.removeAllListeners('exit');
active_server.kill('SIGKILL');
}
gutil.beep();
gutil.beep();
process.exit();
}
process.on("uncaughtException", leave_no_wounded);
process.on("SIGINT", leave_no_wounded);
process.on("SIGTERM", leave_no_wounded);
var PATHS = {
css: './src/css/**/*',
css_candidates: ['./src/css/styles.less'],
fonts: [
'./bower_components/bootstrap/dist/fonts/*',
'./bower_components/font-awesome/fonts/*',
],
fonts2: [
'./node_modules/video.js/dist/video-js/font/*',
],
assets: [
'./node_modules/video.js/dist/video-js/video-js.swf',
],
ngview: './src/ngview/**/*',
scripts: ['./src/**/*.js', './*.js'],
// TODO limit mocha tests to src/core/ until we fix/delete the other failing tests
test_scripts: './src/**/test*.js',
html_scripts: [
'./src/views/adminoobaa.html',
'./src/views/planet_boot.html',
],
server_main: './src/server/server.js',
client_main: './src/client/main.js',
client_externals: [
'./bower_components/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/masonry.js/dist/masonry.pkgd.min.js',
'./bower_components/alertify.js/lib/alertify.min.js',
'./node_modules/video.js/dist/video-js/video.dev.js',
// './vendor/flowplayer-5.4.6/flowplayer.js',
],
};
var SRC_DONT_READ = {
read: false
};
function gulp_size_log(title) {
return gulp_size({
title: title
});
}
function simple_bower() {
// create a pass through stream
var done;
var stream = through2.obj(function(file, enc, callback) {
var self = this;
if (done) {
self.push(file);
callback();
return;
}
done = true;
bower.commands.install([], {}, {
directory: './bower_components'
}).on('log', function(result) {
gutil.log('bower', gutil.colors.cyan(result.id), result.message);
}).on('error', function(error) {
stream.emit('error', new gutil.PluginError('simple_bower', error));
stream.end();
callback();
}).on('end', function() {
self.push(file);
stream.end();
callback();
});
});
return stream;
}
function candidate(candidate_src) {
var done;
var stream = through2.obj(function(file, enc, callback) {
var self = this;
if (done) {
return callback();
}
done = true;
gulp.src(candidate_src).pipe(through2.obj(function(c_file, c_enc, c_callback) {
self.push(c_file);
c_callback();
}, function() {
callback();
}));
});
return stream;
}
var PLUMB_CONF = {
errorHandler: gulp_notify.onError("Error: <%= error.message %>")
};
gulp.task('bower', function() {
var DEST = 'build';
var NAME = 'bower.json';
return gulp.src(NAME)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(path.join(DEST, NAME)))
.pipe(simple_bower())
.pipe(gulp.dest(DEST));
});
gulp.task('assets', ['bower'], function() {
var DEST = 'build/public';
var FONTS_DEST = 'build/public/fonts';
var FONTS2_DEST = 'build/public/css/font';
return Q.all([
gulp.src(PATHS.assets)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(DEST))
.pipe(gulp.dest(DEST)),
gulp.src(PATHS.fonts)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(FONTS_DEST))
.pipe(gulp.dest(FONTS_DEST)),
gulp.src(PATHS.fonts2)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(FONTS2_DEST))
.pipe(gulp.dest(FONTS2_DEST))
]);
});
gulp.task('css', ['bower'], function() {
var DEST = 'build/public/css';
var NAME = 'styles.css';
var NAME_MIN = 'styles.min.css';
return gulp.src(PATHS.css, SRC_DONT_READ)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(path.join(DEST, NAME)))
.pipe(candidate(PATHS.css_candidates))
.pipe(gulp_less({
modifyVars: {
cdn: '"' + CDN + '"',
},
}))
.pipe(gulp_rename(NAME))
.pipe(gulp_size_log(NAME))
.pipe(gulp.dest(DEST))
.pipe(gulp_minify_css())
.pipe(gulp_rename(NAME_MIN))
.pipe(gulp_size_log(NAME_MIN))
.pipe(gulp.dest(DEST));
});
gulp.task('ng', function() {
var DEST = 'build/public/js';
var NAME = 'templates.js';
var NAME_MIN = 'templates.min.js';
return gulp.src(PATHS.ngview)
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_newer(path.join(DEST, NAME)))
.pipe(gulp_ng_template({
standalone: true
}))
.pipe(gulp_size_log(NAME))
.pipe(gulp.dest(DEST))
.pipe(gulp_cached(NAME))
.pipe(gulp_uglify())
.pipe(gulp_rename(NAME_MIN))
.pipe(gulp_size_log(NAME_MIN))
.pipe(gulp.dest(DEST));
});
gulp.task('jshint', function() {
return gulp.src(_.flatten([PATHS.scripts, PATHS.html_scripts]))
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_cached('jshint'))
.pipe(gulp_jshint.extract('always'))
.pipe(gulp_jshint())
.pipe(gulp_jshint.reporter(jshint_stylish));
// avoid failing for watch to continue
// .pipe(gulp_jshint.reporter('fail'));
});
gulp.task('client', ['bower', 'ng'], function() {
var DEST = 'build/public/js';
var NAME = 'bundle.js';
var NAME_MIN = 'bundle.min.js';
var bundler = browserify({
entries: PATHS.client_main,
debug: (process.env.DEBUG_MODE === 'true'),
// bare is alias for both --no-builtins, --no-commondir,
// and sets --insert-global-vars to just "__filename,__dirname".
// This is handy if you want to run bundles in node.
bare: true,
detectGlobals: false,
list: true,
});
// using gulp_replace to fix collision of requires
var client_bundle_stream = bundler.bundle()
.pipe(vinyl_source_stream(NAME))
.pipe(vinyl_buffer())
.pipe(gulp_replace(/\brequire\b/g, 'require_browserify'))
.pipe(gulp_replace(/\brequire_node\b/g, 'require'));
var client_merged_stream = event_stream.merge(
client_bundle_stream,
gulp.src(PATHS.client_externals)
);
return client_merged_stream
.pipe(gulp_plumber(PLUMB_CONF))
.pipe(gulp_concat(NAME))
.pipe(gulp_size_log(NAME))
.pipe(gulp.dest(DEST))
.pipe(gulp_cached(NAME))
.pipe(gulp_uglify())
.pipe(gulp_rename(NAME_MIN))
.pipe(gulp_size_log(NAME_MIN))
.pipe(gulp.dest(DEST));
});
gulp.task('mocha', function() {
var mocha_options = {
reporter: 'spec'
};
return gulp.src(PATHS.scripts)
.pipe(gulp_istanbul())
.on('finish', function() {
return gulp.src(PATHS.test_scripts, SRC_DONT_READ)
.pipe(gulp_mocha(mocha_options))
.pipe(gulp_istanbul.writeReports());
});
});
gulp.task('test', ['jshint', 'mocha']);
function serve() {
if (active_server) {
console.log(' ');
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.log('~~~ KILL SERVER ~~~ (pid=' + active_server.pid + ')');
console.log('~~~ (wait exit to respawn) ~~~');
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.log(' ');
active_server.kill();
return;
}
console.log(' ');
console.log('~~~~~~~~~~~~~~~~~~~~~~');
console.log('~~~ START SERVER ~~~');
console.log('~~~~~~~~~~~~~~~~~~~~~~');
console.log(' ');
active_server = child_process.fork(
path.basename(PATHS.server_main), [], {
cwd: path.dirname(PATHS.server_main)
}
);
active_server.on('error', function(err) {
console.error(' ');
console.error('~~~~~~~~~~~~~~~~~~~~~~');
console.error('~~~ SERVER ERROR ~~~', err);
console.error('~~~~~~~~~~~~~~~~~~~~~~');
console.error(' ');
gutil.beep();
});
active_server.on('exit', function(code, signal) {
console.error(' ');
console.error('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.error('~~~ SERVER EXIT ~~~ (rc=' + code + ')');
console.error('~~~ (respawn in 1 second) ~~~');
console.error('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.error(' ');
active_server = null;
setTimeout(serve, 1);
});
gulp_notify('noobaa serving...').end('stam');
}
gulp.task('clean', function(cb) {
var folders_to_del = ['node_modules/', 'bower_components/', 'build/'];
console.log('~~~~~~~~~~~~~~~~~~~~~~');
_.each(folders_to_del, function(folder) {
console.log('~~~ delete folder ' + folder + ' ~~~');
});
console.log('~~~~~~~~~~~~~~~~~~~~~~');
del(folders_to_del, cb);
});
gulp.task('install', ['bower', 'assets', 'css', 'ng', 'jshint', 'client']);
gulp.task('install_and_serve', ['install'], serve);
gulp.task('install_css_and_serve', ['css'], serve);
gulp.task('install_client_and_serve', ['jshint', 'client'], serve);
gulp.task('install_server_and_serve', ['jshint'], serve);
gulp.task('start_dev', ['install_and_serve'], function() {
gulp.watch('src/css/**/*', ['install_css_and_serve']);
gulp.watch(['src/client/**/*', 'src/ngview/**/*', 'src/utils/**/*'], ['install_client_and_serve']);
gulp.watch(['src/server/**/*', 'src/views/**/*'], ['install_server_and_serve']);
});
gulp.task('start_prod', function() {
console.log('~~~ START PROD ~~~');
require(PATHS.server_main);
});
if (process.env.DEV_MODE === 'true') {
gulp.task('start', ['start_dev']);
} else {
gulp.task('start', ['start_prod']);
}
gulp.task('default', ['start']);