blob: ecf9ed3e8e72c6e19987fd4dc564c4bfd8f0466b [file] [log] [blame]
Matteo Scandolobe9b13d2016-01-21 11:21:03 -08001'use strict';
2
3// BUILD
4//
5// The only purpose of this gulpfile is to build a XOS view and copy the correct files into
6// .html => dashboards
7// .js (minified and concat) => static/js
8//
9// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
12var ngAnnotate = require('gulp-ng-annotate');
13var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
16var concat = require('gulp-concat');
17var del = require('del');
18var wiredep = require('wiredep');
19var angularFilesort = require('gulp-angular-filesort');
20var _ = require('lodash');
21var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
25
26var TEMPLATE_FOOTER = `}]);
27angular.module('xos.serviceTopology').run(function($location){$location.path('/')});
28angular.bootstrap(angular.element('#xosServiceTopology'), ['xos.serviceTopology']);`;
29
30module.exports = function(options){
31
32 // delete previous builded file
33 gulp.task('clean', function(){
34 return del(
35 [options.dashboards + 'xosServiceTopology.html'],
36 {force: true}
37 );
38 });
39
40 // compile and minify scripts
41 gulp.task('scripts', function() {
42 return gulp.src([
43 options.tmp + '**/*.js'
44 ])
45 .pipe(ngAnnotate())
46 .pipe(angularFilesort())
47 .pipe(concat('xosServiceTopology.js'))
48 .pipe(uglify())
49 .pipe(gulp.dest(options.static + 'js/'));
50 });
51
52 // set templates in cache
53 gulp.task('templates', function(){
54 return gulp.src('./src/templates/*.html')
55 .pipe(templateCache({
56 module: 'xos.serviceTopology',
57 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
59 }))
60 .pipe(gulp.dest(options.tmp));
61 });
62
63 // copy html index to Django Folder
64 gulp.task('copyHtml', ['clean'], function(){
65 return gulp.src(options.src + 'index.html')
66 // remove dev dependencies from html
67 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
68 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
69 .pipe(replace(/ng-app=".*"\s/, ''))
70 // injecting minified files
71 .pipe(
72 inject(
73 gulp.src([
74 options.static + 'js/vendor/xosServiceTopologyVendor.js',
75 options.static + 'js/xosServiceTopology.js'
76 ])
77 )
78 )
79 .pipe(rename('xosServiceTopology.html'))
80 .pipe(gulp.dest(options.dashboards));
81 });
82
83 // minify vendor js files
84 gulp.task('wiredep', function(){
85 var bowerDeps = wiredep().js;
86 if(!bowerDeps){
87 return;
88 }
89
90 // remove angular (it's already loaded)
91 _.remove(bowerDeps, function(dep){
92 return dep.indexOf('angular/angular.js') !== -1;
93 });
94
95 return gulp.src(bowerDeps)
96 .pipe(concat('xosServiceTopologyVendor.js'))
97 .pipe(uglify())
98 .pipe(gulp.dest(options.static + 'js/vendor/'));
99 });
100
101 gulp.task('lint', function () {
102 return gulp.src(['src/js/**/*.js'])
103 .pipe(eslint())
104 .pipe(eslint.format())
105 .pipe(eslint.failAfterError());
106 });
107
108 gulp.task('build', function() {
109 runSequence(
110 'templates',
111 'babel',
112 'scripts',
113 'wiredep',
114 'copyHtml',
115 'cleanTmp'
116 );
117 });
118};