Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 1 | '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 | |
| 11 | var gulp = require('gulp'); |
| 12 | var ngAnnotate = require('gulp-ng-annotate'); |
| 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 16 | var concat = require('gulp-concat-util'); |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
| 19 | var angularFilesort = require('gulp-angular-filesort'); |
| 20 | var _ = require('lodash'); |
| 21 | var eslint = require('gulp-eslint'); |
| 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
| 24 | var replace = require('gulp-replace'); |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 30 | const TEMPLATE_FOOTER = ` |
| 31 | angular.module('xos.openVPNDashboard') |
| 32 | .run(['$location', function(a){ |
| 33 | a.path('/'); |
| 34 | }]) |
| 35 | ` |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 36 | |
| 37 | module.exports = function(options){ |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 38 | |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 39 | // delete previous builded file |
| 40 | gulp.task('clean', function(){ |
| 41 | return del( |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 42 | [ |
| 43 | options.dashboards + 'xosOpenVPNDashboard.html', |
| 44 | options.static + 'css/xosOpenVPNDashboard.css' |
| 45 | ], |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 46 | {force: true} |
| 47 | ); |
| 48 | }); |
| 49 | |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 50 | // minify css |
| 51 | gulp.task('css', function () { |
| 52 | var processors = [ |
| 53 | autoprefixer({browsers: ['last 1 version']}), |
| 54 | mqpacker, |
| 55 | csswring |
| 56 | ]; |
| 57 | |
| 58 | gulp.src([ |
| 59 | `${options.css}**/*.css`, |
| 60 | `!${options.css}dev.css` |
| 61 | ]) |
| 62 | .pipe(postcss(processors)) |
| 63 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 64 | }); |
| 65 | |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 66 | // copy css in correct folder |
| 67 | gulp.task('copyCss', ['wait'], function(){ |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 68 | return gulp.src([`${options.tmp}/css/*.css`]) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 69 | .pipe(concat('xosOpenVPNDashboard.css')) |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 70 | .pipe(gulp.dest(options.static + 'css/')) |
| 71 | }); |
| 72 | |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 73 | // compile and minify scripts |
| 74 | gulp.task('scripts', function() { |
| 75 | return gulp.src([ |
| 76 | options.tmp + '**/*.js' |
| 77 | ]) |
| 78 | .pipe(ngAnnotate()) |
| 79 | .pipe(angularFilesort()) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 80 | .pipe(concat('xosOpenVPNDashboard.js')) |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 81 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 82 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 83 | .pipe(uglify()) |
| 84 | .pipe(gulp.dest(options.static + 'js/')); |
| 85 | }); |
| 86 | |
| 87 | // set templates in cache |
| 88 | gulp.task('templates', function(){ |
| 89 | return gulp.src('./src/templates/*.html') |
| 90 | .pipe(templateCache({ |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 91 | module: 'xos.openVPNDashboard', |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 92 | root: 'templates/' |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 93 | })) |
| 94 | .pipe(gulp.dest(options.tmp)); |
| 95 | }); |
| 96 | |
| 97 | // copy html index to Django Folder |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 98 | gulp.task('copyHtml', function(){ |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 99 | return gulp.src(options.src + 'index.html') |
| 100 | // remove dev dependencies from html |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 101 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 102 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 103 | // injecting minified files |
| 104 | .pipe( |
| 105 | inject( |
| 106 | gulp.src([ |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 107 | options.static + 'js/vendor/xosOpenVPNDashboardVendor.js', |
| 108 | options.static + 'js/xosOpenVPNDashboard.js', |
| 109 | options.static + 'css/xosOpenVPNDashboard.css' |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 110 | ]), |
| 111 | {ignorePath: '/../../../xos/core/xoslib'} |
| 112 | ) |
| 113 | ) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 114 | .pipe(rename('xosOpenVPNDashboard.html')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 115 | .pipe(gulp.dest(options.dashboards)); |
| 116 | }); |
| 117 | |
| 118 | // minify vendor js files |
| 119 | gulp.task('wiredep', function(){ |
| 120 | var bowerDeps = wiredep().js; |
| 121 | if(!bowerDeps){ |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // remove angular (it's already loaded) |
| 126 | _.remove(bowerDeps, function(dep){ |
| 127 | return dep.indexOf('angular/angular.js') !== -1; |
| 128 | }); |
| 129 | |
| 130 | return gulp.src(bowerDeps) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 131 | .pipe(concat('xosOpenVPNDashboardVendor.js')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 132 | .pipe(uglify()) |
| 133 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 134 | }); |
| 135 | |
| 136 | gulp.task('lint', function () { |
| 137 | return gulp.src(['src/js/**/*.js']) |
| 138 | .pipe(eslint()) |
| 139 | .pipe(eslint.format()) |
| 140 | .pipe(eslint.failAfterError()); |
| 141 | }); |
| 142 | |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 143 | gulp.task('wait', function (cb) { |
| 144 | // setTimeout could be any async task |
| 145 | setTimeout(function () { |
| 146 | cb(); |
| 147 | }, 1000); |
| 148 | }); |
| 149 | |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 150 | gulp.task('build', function() { |
| 151 | runSequence( |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 152 | 'clean', |
| 153 | 'sass', |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 154 | 'templates', |
| 155 | 'babel', |
| 156 | 'scripts', |
| 157 | 'wiredep', |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 158 | 'css', |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 159 | 'copyCss', |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 160 | 'copyHtml', |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 161 | 'cleanTmp' |
| 162 | ); |
| 163 | }); |
Matteo Scandolo | 9ef11a8 | 2016-05-17 17:30:14 -0700 | [diff] [blame] | 164 | }; |