blob: cf2b40e5c521afa1e98d7fa4fa7eff88d6158666 [file] [log] [blame]
Matteo Scandolo8a64fa42016-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');
Matteo Scandolo70ac2162016-02-24 15:40:22 -080016var concat = require('gulp-concat-util');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080017var 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');
Matteo Scandolo70ac2162016-02-24 15:40:22 -080025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080029
Matteo Scandolo70ac2162016-02-24 15:40:22 -080030var TEMPLATE_FOOTER = `
31angular.module('xos.diagnostic').run(function($location){
32 $location.path('/')
33});
34angular.bootstrap(angular.element('#xosDiagnostic'), ['xos.diagnostic']);`;
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080035
36module.exports = function(options){
37
38 // delete previous builded file
39 gulp.task('clean', function(){
40 return del(
Matteo Scandolo04564952016-02-24 11:22:48 -080041 [options.dashboards + 'xosDiagnostic.html'],
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080042 {force: true}
43 );
44 });
45
Matteo Scandolo70ac2162016-02-24 15:40:22 -080046 // minify css
47 gulp.task('css', function () {
48 var processors = [
49 autoprefixer({browsers: ['last 1 version']}),
50 mqpacker,
51 csswring
52 ];
53 console.log(options.css);
54 gulp.src([
55 `${options.css}**/*.css`,
56 `!${options.css}dev.css`
57 ])
58 .pipe(postcss(processors))
59 .pipe(gulp.dest(options.tmp + '/css/'));
60 });
61
62 gulp.task('copyCss', ['css'], function(){
63 return gulp.src([`${options.tmp}/css/*.css`])
64 .pipe(concat('xosDiagnostic.css'))
65 .pipe(gulp.dest(options.static + 'css/'))
66 });
67
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080068 // compile and minify scripts
69 gulp.task('scripts', function() {
70 return gulp.src([
Matteo Scandolo70ac2162016-02-24 15:40:22 -080071 options.tmp + '**/*.js',
72 options.tmp + 'templates.js'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080073 ])
74 .pipe(ngAnnotate())
75 .pipe(angularFilesort())
Matteo Scandolo04564952016-02-24 11:22:48 -080076 .pipe(concat('xosDiagnostic.js'))
Matteo Scandolo70ac2162016-02-24 15:40:22 -080077 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
78 .pipe(concat.footer(TEMPLATE_FOOTER))
79 // .pipe(uglify())
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080080 .pipe(gulp.dest(options.static + 'js/'));
81 });
82
83 // set templates in cache
84 gulp.task('templates', function(){
85 return gulp.src('./src/templates/*.html')
86 .pipe(templateCache({
Matteo Scandolo70ac2162016-02-24 15:40:22 -080087 module: 'xos.diagnostic',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080088 root: 'templates/',
Matteo Scandolo70ac2162016-02-24 15:40:22 -080089 // templateFooter: TEMPLATE_FOOTER
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080090 }))
91 .pipe(gulp.dest(options.tmp));
92 });
93
94 // copy html index to Django Folder
95 gulp.task('copyHtml', ['clean'], function(){
96 return gulp.src(options.src + 'index.html')
97 // remove dev dependencies from html
98 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
99 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
100 .pipe(replace(/ng-app=".*"\s/, ''))
101 // injecting minified files
102 .pipe(
103 inject(
104 gulp.src([
Matteo Scandolo04564952016-02-24 11:22:48 -0800105 options.static + 'js/vendor/xosDiagnosticVendor.js',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800106 options.static + 'js/xosDiagnostic.js',
107 options.static + 'css/xosDiagnostic.css'
108 ]),
109 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800110 )
111 )
Matteo Scandolo04564952016-02-24 11:22:48 -0800112 .pipe(rename('xosDiagnostic.html'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800113 .pipe(gulp.dest(options.dashboards));
114 });
115
116 // minify vendor js files
117 gulp.task('wiredep', function(){
118 var bowerDeps = wiredep().js;
119 if(!bowerDeps){
120 return;
121 }
122
123 // remove angular (it's already loaded)
124 _.remove(bowerDeps, function(dep){
125 return dep.indexOf('angular/angular.js') !== -1;
126 });
127
128 return gulp.src(bowerDeps)
Matteo Scandolo04564952016-02-24 11:22:48 -0800129 .pipe(concat('xosDiagnosticVendor.js'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800130 .pipe(uglify())
131 .pipe(gulp.dest(options.static + 'js/vendor/'));
132 });
133
134 gulp.task('lint', function () {
135 return gulp.src(['src/js/**/*.js'])
136 .pipe(eslint())
137 .pipe(eslint.format())
138 .pipe(eslint.failAfterError());
139 });
140
141 gulp.task('build', function() {
142 runSequence(
143 'templates',
144 'babel',
145 'scripts',
146 'wiredep',
147 'copyHtml',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800148 'copyCss'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800149 );
150 });
151};