forked from ThirstyHead/meaningfuljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
596 lines (529 loc) · 18.6 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
'use strict';
// Load Node Modules/Plugins
var gulp = require('gulp-help')(require('gulp'));
var gulpPlugin = require('gulp-load-plugins')({lazy: true});
var gulpNgConfig = require('gulp-ng-config');
var del = require('del');
var plato = require('plato');
var browserSync = require('browser-sync');
var wiredep = require('wiredep').stream;
var lodash = require('lodash');
var eventStream = require('event-stream');
var os = require('os');
var fs = require('fs-extra');
// read in package.json
var pkginfo = require('pkginfo')(module);
var packageJson = module.exports;
// Testing Modules
var selenium = require('selenium-standalone');
var protractor = require('gulp-protractor').protractor;
var jasmineReporters = require('jasmine-reporters');
var JasmineSpecReporter = require('jasmine-spec-reporter');
var karma = require('karma');
var karmaParseConfig = require('karma/lib/config').parseConfig;
// variables and settings
var dir = {};
dir.build = './build';
dir.dist = './dist';
dir.reports = './reports';
dir.src = './src';
dir.client = dir.src + '/client';
dir.server = dir.src + '/server';
dir.protractor = dir.src + '/protractor';
dir.bower = './bower_components';
dir.npm = './node_modules';
var src = {};
src.index = [dir.client + '/index.html'];
src.specRunner = [dir.client + '/specs.html'];
src.html = [dir.client + '/**/*.html'];
src.css = [dir.client + '/**/*.css'];
src.img = [
dir.client + '/**/*.png',
dir.client + '/**/*.jpg',
dir.client + '/**/*.gif'
];
src.favicon = [dir.client + '/favicon.ico'];
src.js = [dir.client + '/**/*.js'];
src.json = [dir.client + '/**/*.json'];
src.jsNoTests = [
dir.client + '/**/*.js',
'!' + dir.client + '/**/*.spec.js',
];
src.less = [
dir.client + '/**/*.less',
dir.bower + '/theme/less/theme.less'
];
src.protractor = [dir.protractor + '/**/*.spec.js'];
src.jasmine = [dir.client + '/**/*.spec.js'];
var build = {};
build.html = dir.build;
build.css = dir.build + '/css';
build.img = dir.build + '/img';
build.js = dir.build + '/js';
build.allExceptTests = [
dir.build + '/**/*.*',
'!' + dir.build + '/**.spec.js',
'!' + dir.build + '/specs.html'
];
build.jsExceptTests = dir.build + '/js/**/!(*.spec).js';
var dist = {};
dist.html = dir.dist;
dist.css = dir.dist + '/css';
dist.img = dir.dist + '/img';
dist.js = dir.dist + '/js';
var reports = {};
reports.protractor = dir.reports + '/protractor';
//NOTE: this hides tasks from 'gulp help',
// but leaves them runnable as dependent tasks
var hideTask = false;
/**
* Default task
*/
gulp.task('default',
'*** Default task ***',
['help']);
/**
* Builds runnable website
*/
gulp.task('build',
'Builds runnable website (dev-mode)',
['build-js',
'build-css',
'build-img',
'build-favicon',
'build-angular-const',
'build-json'],
function() {
// NOTE: wiredep is for bower_components
// These custom options strip the leading
// relative path (/../../) off
var wiredepOptions = {
dependencies: true,
devDependencies: false,
ignorePath: /^\/|(\.\.\/){1,2}/,
fileTypes:{
html: {
replace: {
js: '<script src="{{filePath}}"></script>',
css: '<link rel="stylesheet" href="{{filePath}}" />'
}
}
}
};
// NOTE: gulp-inject is for our app / custom code
var injectJs = gulp.src(['./**/*.js'],
{cwd: __dirname + '/build'})
.pipe(gulpPlugin.angularFilesort());
var injectJsNoTests = gulp.src(['./**/*.js', '!./**/*.spec.js'],
{cwd: __dirname + '/build'})
.pipe(gulpPlugin.angularFilesort());
var injectCss = gulp.src(['./**/*.css'],
{cwd: __dirname + '/build'});
var jsTransform = function(filepath) {
return '<script src="' + filepath.replace('../../build/', '') + '"></script>';
};
var cssTransform = function(filepath) {
return '<link rel="stylesheet" href="' + filepath.replace('../../build/', '') + '" />';
};
// copy over all html
gulp.src(src.html)
.pipe(gulp.dest(build.html));
// inject index.html
gulp.src(src.index)
.pipe(wiredep(wiredepOptions))
.pipe(gulpPlugin.inject(injectJsNoTests, {relative: true, transform: jsTransform}))
.pipe(gulpPlugin.inject(injectCss, {relative: true, transform: cssTransform}))
.pipe(gulp.dest(build.html));
// inject specs.html
// NOTE: add in devDependencies as well
var testOptions = {};
lodash.merge(testOptions, wiredepOptions);
testOptions.devDependencies = true;
return gulp.src(src.specRunner)
.pipe(wiredep(testOptions))
.pipe(gulpPlugin.inject(injectJs))
.pipe(gulpPlugin.inject(injectCss))
.pipe(gulp.dest(build.html));
});
/**
* Builds Angular environment-specific
* constants file based on the NODE_ENV
* environment variable
*
* NOTE: defaults to 'development'
*/
gulp.task('build-angular-const',
hideTask,
function() {
var env = process.env.NODE_ENV || 'development';
var ngConfigOptions = {
environment: env,
wrap: true
};
return gulp.src(dir.client + '/app/app.config.json')
.pipe(gulpNgConfig('app.config',
ngConfigOptions))
.pipe(gulpPlugin.replace('"', '\''))
.pipe(gulp.dest(build.js + '/app'));
});
/**
* Builds CSS
*/
gulp.task('build-css',
hideTask,
function() {
gulp.src(src.css)
.pipe(gulp.dest(dir.build));
return gulp.src(src.less)
.pipe(gulpPlugin.sourcemaps.init())
.pipe(gulpPlugin.plumber())
.pipe(gulpPlugin.less())
.pipe(gulpPlugin.autoprefixer({browsers: ['last 2 version', '> 5%']}))
.pipe(gulpPlugin.sourcemaps.write('/maps'))
.pipe(gulp.dest(build.css));
});
/**
* Builds favicon
*/
gulp.task('build-favicon',
hideTask,
function() {
return gulp.src(src.favicon)
.pipe(gulp.dest(build.html));
});
/**
* Builds images
*/
gulp.task('build-img',
hideTask,
function() {
return gulp.src(src.img)
.pipe(gulp.dest(build.img));
});
/**
* Builds JS
*/
gulp.task('build-js',
hideTask,
function() {
// check this file for errors,
// but don't deploy to build
gulp.src('./gulpfile.js')
.pipe(gulpPlugin.jshint())
.pipe(gulpPlugin.jshint.reporter('jshint-stylish', {verbose: true}))
.pipe(gulpPlugin.jshint.reporter('fail'))
.pipe(gulpPlugin.jscs());
return gulp.src(src.js)
.pipe(gulpPlugin.jshint())
.pipe(gulpPlugin.jshint.reporter('jshint-stylish', {verbose: true}))
.pipe(gulpPlugin.jshint.reporter('fail'))
.pipe(gulpPlugin.jscs())
.pipe(gulp.dest(build.js));
});
/**
* Builds JSON
*/
gulp.task('build-json',
hideTask,
function() {
return gulp.src(src.json)
.pipe(gulpPlugin.jsonlint())
.pipe(gulpPlugin.jsonlint.reporter())
.pipe(gulp.dest(dir.build));
});
/**
* Deletes generated artifacts
*/
gulp.task('clean',
'Deletes generated artifacts',
function(cb) {
del.sync([dir.build, dir.dist, dir.reports]);
return cb();
});
/**
* Builds production website
*/
gulp.task('dist',
'Builds production website (prod-mode)',
['clean', 'build', 'dist-js', 'dist-css', 'dist-json'],
function() {
var wiredepOptions = {
dependencies: true,
devDependencies: false,
ignorePath: /^\/|(\.\.\/){1,2}/,
fileTypes:{
html: {
replace: {
js: '<script src="{{filePath}}"></script>',
css: '<link rel="stylesheet" href="{{filePath}}" />'
}
}
}
};
// NOTE: gulp-inject is for our app / custom code
var injectJs = gulp.src(['./**/*.js'],
{cwd: __dirname + '/dist'})
.pipe(gulpPlugin.angularFilesort());
var injectJsNoTests = gulp.src(['./**/*.js', '!./**/*.spec.js'],
{cwd: __dirname + '/dist'})
.pipe(gulpPlugin.angularFilesort());
var injectCss = gulp.src(['./**/*.css'],
{cwd: __dirname + '/dist'});
var jsTransform = function(filepath) {
return '<script src="' + filepath.replace('../../dist/', '') + '"></script>';
};
var cssTransform = function(filepath) {
return '<link rel="stylesheet" href="' +
filepath.replace('../../dist/', '') + '" />';
};
// copy over all html
gulp.src(src.html.concat(['!**/specs.html']))
.pipe(gulp.dest(dist.html));
// inject index.html
return gulp.src(src.index)
.pipe(wiredep(wiredepOptions))
.pipe(gulpPlugin.inject(injectJsNoTests,
{relative: true, transform: jsTransform}))
.pipe(gulpPlugin.inject(injectCss, {relative:true, transform:cssTransform}))
.pipe(gulp.dest(dist.html));
});
gulp.task('dist-js',
hideTask,
['build-js'],
function() {
return gulp.src(build.jsExceptTests)
.pipe(gulpPlugin.sourcemaps.init())
.pipe(gulpPlugin.concat('app/app.js'))
.pipe(gulpPlugin.uglify())
.pipe(gulpPlugin.sourcemaps.write('/maps'))
.pipe(gulp.dest(dir.dist));
});
gulp.task('dist-css',
hideTask,
['build-css'],
function() {
return gulp.src(build.css + '**/*.css')
.pipe(gulpPlugin.sourcemaps.init('/maps'))
.pipe(gulpPlugin.minifyCss())
.pipe(gulpPlugin.sourcemaps.write('/maps'))
.pipe(gulp.dest(dir.dist));
});
gulp.task('dist-json',
hideTask,
['build-json'],
function() {
return gulp.src(dir.build + '/**/*.json')
.pipe(gulp.dest(dir.dist));
});
/**
* Runs unit test in multiple browsers via Karma
*/
gulp.task('karma',
hideTask,
function(cb) {
var server = karma.server;
var config = karmaParseConfig(__dirname + '/karma.conf.js', {});
server.start(config, function(exitCode) {
console.log('Karma has exited with ' + exitCode);
cb();
process.exit(exitCode);
});
});
/**
* Keeps web server up and running
* Called by 'run' task
*/
gulp.task('nodemon',
hideTask,
function(cb) {
// We use this `called` variable to make sure the callback is only executed once
var called = false;
var port = process.env.PORT || 3000;
var nodemonOptions = {
port: port,
script: dir.server + '/server.js',
watch: [dir.server + '/server.js',
dir.build + '/**/*.*']
};
return gulpPlugin.nodemon(nodemonOptions)
.on('start', function onStart() {
console.log('*** nodemon started');
if (!called) {
// Also reload the browsers after a slight delay
setTimeout(function reload() {
browserSync.notify('reloading now ...');
browserSync.reload({
stream: false
});
cb();
}, 1000);
}
called = true;
})
.on('restart', function onRestart() {
console.log('*** nodemon restarted');
// Also reload the browsers after a slight delay
setTimeout(function reload() {
browserSync.notify('reloading now ...');
browserSync.reload({
stream: false
});
}, 1000);
})
.on('crash', function () {
console.log('*** nodemon crashed: script crashed for some reason');
})
.on('exit', function () {
console.log('*** nodemon exited cleanly');
});
});
/**
* Create a visualizer report
*/
gulp.task('plato',
'Runs Plato report on the project',
['clean', 'build'],
function(done) {
console.log('Analyzing source with Plato');
console.log('Browse to ' + dir.reports + '/plato/index.html to see Plato results');
var options = {
title: 'Plato Inspections Report',
exclude: /.*\.spec\.js/
};
plato.inspect([build.js + '/**/*.js'],
dir.reports + '/plato/',
options,
platoCompleted);
function platoCompleted(report) {
var overview = plato.getOverviewReport(report);
console.log(overview.summary);
if (done) {
done();
}
}
});
/**
* Runs Protractor tests (browser acceptance tests)
*/
gulp.task('protractor',
'Runs Protractor tests',
function() {
del.sync([reports.protractor]);
var port = process.env.PORT || 3000;
var protractorOptions = {
configFile: dir.protractor + '/configuration/protractor.conf.js',
args: ['--baseUrl', 'http://127.0.0.1:' + port + '/' + packageJson.name]
};
return gulp.src(src.protractor)
.pipe(protractor(protractorOptions))
.on('error', function(e) { throw e; })
.on('end', function() {
console.log('=========================');
console.log('Protractor test run done.');
console.log('See ' + reports.protractor + '/html/index.html');
console.log('=========================');
});
});
/**
* Deletes generated and downloaded artifacts (node_modules, bower_components)
*/
gulp.task('reset',
'Deletes generated (build, dist, reports) ' +
'and downloaded artifacts (node_modules, bower_components)',
function(cb) {
del.sync([dir.build, dir.dist, dir.reports, dir.bower, dir.npm]);
return cb();
});
/**
* Runs website (dev-mode)
*/
gulp.task('run',
'Runs website',
['nodemon'],
function() {
var port = process.env.PORT || 3000;
var syncPort = process.env.SYNC_PORT || 4000;
var publicDir = (process.env.NODE_ENV === 'production' ? 'dist' : 'build');
var browserSyncOptions = {
// All of the following files will be watched
files: [dir[publicDir] + '/**/*.*'],
// Tells BrowserSync on where the express app is running
proxy: 'http://localhost:' + port + '/' + packageJson.name,
// This port should be different from the express app port
port: syncPort,
// Which browser should we launch?
browser: ['google chrome']
};
browserSync.init(browserSyncOptions);
// Register a watcher on the src directory for changes,
// which will update the build directory,
// which will trigger browserSync + nodemon
return gulp.watch(dir.src + '/**/*.*', [publicDir]);
});
/**
* Runs Selenium server (for Protractor tests)
*/
gulp.task('selenium',
'Downloads and runs Selenium server',
function (done) {
// NOTE: this redirects Selenium server output
// from std.err to std.out
var seleniumOptions = {
spawnOptions: {
stdio: 'inherit'
}
};
var seleniumInstallOptions = {
logger: function (message) {
console.log(message);
}
};
selenium.install(seleniumInstallOptions,
function (err) {
if (err) { return done(err); }
selenium.start(seleniumOptions,
function (err, child) {
if (err) { return done(err); }
selenium.child = child;
done();
});
});
});
/**
* Runs Jasmine tests
*/
gulp.task('test',
'Runs Jasmine tests',
function(cb) {
// prints JUnit XML
var junitReporter = new jasmineReporters.JUnitXmlReporter({
savePath: dir.reports + '/jasmine/junit',
consolidateAll: false
});
// prints test results to the console
var jasmineSpecReporter = new JasmineSpecReporter({
displayFailuresSummary: true,
displaySuccessfulSpec: true,
displayFailedSpec: true,
displayPendingSpec: true
});
// configuration for the Jasmine test runner
var jasmineOptions = {
verbose: true,
reporter: [junitReporter, jasmineSpecReporter]
};
// delete old test run data
del.sync([dir.reports + '/jasmine', dir.reports + '/coverage']);
// instrument the classes for coverage, then run the tests
return gulp.src(src.jsNoTests)
.pipe(gulpPlugin.istanbul({includeUntested: true}))
.on('finish', function () {
gulp.src(src.jasmine)
.pipe(gulpPlugin.jasmine(jasmineOptions))
.pipe(gulpPlugin.istanbul.writeReports({
dir: dir.reports + '/coverage',
reporters: ['lcov', 'text-summary'],
reportOpts: {dir: dir.reports + '/coverage'}
}));
});
});