Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [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'); |
| 12 | var sass = require('gulp-sass'); |
| 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 | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 53 | '/xosHelpers/src': options.helpers |
| 54 | }, |
| 55 | middleware: function(req, res, next){ |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 56 | if( |
| 57 | req.url.indexOf('/xos/') !== -1 || |
| 58 | req.url.indexOf('/xoslib/') !== -1 || |
| 59 | req.url.indexOf('/hpcapi/') !== -1 || |
Matteo Scandolo | bd2e5cd | 2016-04-12 11:59:29 -0700 | [diff] [blame] | 60 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 61 | ){ |
| 62 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 63 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 64 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 65 | } |
| 66 | proxy.web(req, res); |
| 67 | } |
| 68 | else{ |
| 69 | next(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 76 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 77 | browserSync.reload(); |
| 78 | }); |
| 79 | gulp.watch(options.src + '**/*.html', function(){ |
| 80 | browserSync.reload(); |
| 81 | }); |
| 82 | gulp.watch(options.css + '**/*.css', function(){ |
| 83 | browserSync.reload(); |
| 84 | }); |
| 85 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 86 | browserSync.reload(); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | // compile sass |
| 91 | gulp.task('sass', function () { |
| 92 | return gulp.src(`${options.sass}/**/*.scss`) |
| 93 | .pipe(sass().on('error', sass.logError)) |
| 94 | .pipe(gulp.dest(options.css)); |
| 95 | }); |
| 96 | |
| 97 | // transpile js with sourceMaps |
| 98 | gulp.task('babel', function(){ |
| 99 | return gulp.src(options.scripts + '**/*.js') |
| 100 | .pipe(babel({sourceMaps: true})) |
| 101 | .pipe(gulp.dest(options.tmp)); |
| 102 | }); |
| 103 | |
| 104 | // inject scripts |
| 105 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 106 | return gulp.src(options.src + 'index.html') |
| 107 | .pipe( |
| 108 | inject( |
| 109 | gulp.src([ |
| 110 | options.tmp + '**/*.js', |
Matteo Scandolo | bd2e5cd | 2016-04-12 11:59:29 -0700 | [diff] [blame] | 111 | options.helpers + '**/*.js' // todo add Babel here |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 112 | ]) |
| 113 | .pipe(angularFilesort()), |
| 114 | { |
| 115 | ignorePath: [options.src, '/../../ngXosLib'] |
| 116 | } |
| 117 | ) |
| 118 | ) |
| 119 | .pipe(gulp.dest(options.src)); |
| 120 | }); |
| 121 | |
| 122 | // inject CSS |
| 123 | gulp.task('injectCss', function(){ |
| 124 | return gulp.src(options.src + 'index.html') |
| 125 | .pipe( |
| 126 | inject( |
| 127 | gulp.src(options.src + 'css/*.css'), |
| 128 | { |
| 129 | ignorePath: [options.src] |
| 130 | } |
| 131 | ) |
| 132 | ) |
| 133 | .pipe(gulp.dest(options.src)); |
| 134 | }); |
| 135 | |
| 136 | // inject bower dependencies with wiredep |
| 137 | gulp.task('bower', function () { |
| 138 | return gulp.src(options.src + 'index.html') |
| 139 | .pipe(wiredep({devDependencies: true})) |
| 140 | .pipe(gulp.dest(options.src)); |
| 141 | }); |
| 142 | |
| 143 | gulp.task('js-watch', ['injectScript'], function(){ |
| 144 | browserSync.reload(); |
| 145 | }); |
| 146 | |
| 147 | gulp.task('cleanTmp', function(){ |
| 148 | return del([options.tmp + '**/*']); |
| 149 | }); |
| 150 | |
| 151 | gulp.task('serve', function() { |
| 152 | runSequence( |
| 153 | 'sass', |
| 154 | 'bower', |
| 155 | 'injectScript', |
| 156 | 'injectCss', |
| 157 | ['browser'] |
| 158 | ); |
| 159 | }); |
| 160 | }; |