Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var gulp = require('gulp'); |
| 4 | var browserSync = require('browser-sync').create(); |
| 5 | var inject = require('gulp-inject'); |
| 6 | var runSequence = require('run-sequence'); |
| 7 | var angularFilesort = require('gulp-angular-filesort'); |
| 8 | var babel = require('gulp-babel'); |
| 9 | var wiredep = require('wiredep').stream; |
| 10 | var httpProxy = require('http-proxy'); |
| 11 | var del = require('del'); |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 12 | var sass = require('gulp-sass'); |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 13 | |
| 14 | const environment = process.env.NODE_ENV; |
| 15 | |
| 16 | if (environment){ |
| 17 | var conf = require(`../env/${environment}.js`); |
| 18 | } |
| 19 | else{ |
| 20 | var conf = require('../env/default.js') |
| 21 | } |
| 22 | |
| 23 | var proxy = httpProxy.createProxyServer({ |
| 24 | target: conf.host || 'http://0.0.0.0:9999' |
| 25 | }); |
| 26 | |
| 27 | |
| 28 | proxy.on('error', function(error, req, res) { |
| 29 | res.writeHead(500, { |
| 30 | 'Content-Type': 'text/plain' |
| 31 | }); |
| 32 | |
| 33 | console.error('[Proxy]', error); |
| 34 | }); |
| 35 | |
| 36 | module.exports = function(options){ |
| 37 | |
| 38 | // open in browser with sync and proxy to 0.0.0.0 |
| 39 | gulp.task('browser', function() { |
| 40 | browserSync.init({ |
| 41 | // reloadDelay: 500, |
| 42 | // logLevel: 'debug', |
| 43 | // logConnections: true, |
| 44 | startPath: '#/', |
| 45 | snippetOptions: { |
| 46 | rule: { |
| 47 | match: /<!-- browserSync -->/i |
| 48 | } |
| 49 | }, |
| 50 | server: { |
| 51 | baseDir: options.src, |
| 52 | routes: { |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 53 | '/xos/core/xoslib/static/js/vendor': options.helpers |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 54 | }, |
| 55 | middleware: function(req, res, next){ |
| 56 | if( |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 57 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 58 | ){ |
| 59 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 60 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 61 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 62 | } |
| 63 | proxy.web(req, res); |
| 64 | } |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 65 | else if(req.url.indexOf('/videoLocal.txt') !== -1){ |
| 66 | let a = (Math.random() * 10).toString(); |
| 67 | res.end(a) |
Matteo Scandolo | f386616 | 2016-03-07 14:14:00 -0800 | [diff] [blame] | 68 | } |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 69 | else{ |
| 70 | next(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 77 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 78 | browserSync.reload(); |
| 79 | }); |
| 80 | gulp.watch(options.src + '**/*.html', function(){ |
| 81 | browserSync.reload(); |
| 82 | }); |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 83 | gulp.watch(options.css + '**/*.css', function(){ |
| 84 | browserSync.reload(); |
| 85 | }); |
| 86 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 87 | browserSync.reload(); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | // compile sass |
| 92 | gulp.task('sass', function () { |
| 93 | return gulp.src(`${options.sass}/**/*.scss`) |
| 94 | .pipe(sass().on('error', sass.logError)) |
| 95 | .pipe(gulp.dest(options.css)); |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | // transpile js with sourceMaps |
| 99 | gulp.task('babel', function(){ |
| 100 | return gulp.src(options.scripts + '**/*.js') |
| 101 | .pipe(babel({sourceMaps: true})) |
| 102 | .pipe(gulp.dest(options.tmp)); |
| 103 | }); |
| 104 | |
| 105 | // inject scripts |
| 106 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 107 | return gulp.src(options.src + 'index.html') |
| 108 | .pipe( |
| 109 | inject( |
| 110 | gulp.src([ |
| 111 | options.tmp + '**/*.js', |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 112 | options.helpers + 'ngXosHelpers.js' |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 113 | ]) |
| 114 | .pipe(angularFilesort()), |
| 115 | { |
| 116 | ignorePath: [options.src, '/../../ngXosLib'] |
| 117 | } |
| 118 | ) |
| 119 | ) |
| 120 | .pipe(gulp.dest(options.src)); |
| 121 | }); |
| 122 | |
| 123 | // inject CSS |
| 124 | gulp.task('injectCss', function(){ |
| 125 | return gulp.src(options.src + 'index.html') |
| 126 | .pipe( |
| 127 | inject( |
| 128 | gulp.src(options.src + 'css/*.css'), |
| 129 | { |
| 130 | ignorePath: [options.src] |
| 131 | } |
| 132 | ) |
| 133 | ) |
| 134 | .pipe(gulp.dest(options.src)); |
| 135 | }); |
| 136 | |
| 137 | // inject bower dependencies with wiredep |
| 138 | gulp.task('bower', function () { |
| 139 | return gulp.src(options.src + 'index.html') |
| 140 | .pipe(wiredep({devDependencies: true})) |
| 141 | .pipe(gulp.dest(options.src)); |
| 142 | }); |
| 143 | |
| 144 | gulp.task('js-watch', ['injectScript'], function(){ |
| 145 | browserSync.reload(); |
| 146 | }); |
| 147 | |
| 148 | gulp.task('cleanTmp', function(){ |
| 149 | return del([options.tmp + '**/*']); |
| 150 | }); |
| 151 | |
| 152 | gulp.task('serve', function() { |
| 153 | runSequence( |
Matteo Scandolo | f6ba666 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 154 | 'sass', |
Matteo Scandolo | 7480252 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 155 | 'bower', |
| 156 | 'injectScript', |
| 157 | 'injectCss', |
| 158 | ['browser'] |
| 159 | ); |
| 160 | }); |
| 161 | }; |