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