Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +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'); |
| 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'); |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 11 | var del = require('del'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 12 | |
Matteo Scandolo | af977a3 | 2015-11-30 17:25:39 -0800 | [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') |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 20 | } |
| 21 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 22 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 23 | target: conf.host || 'http://0.0.0.0:9999' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 24 | }); |
| 25 | |
| 26 | |
| 27 | proxy.on('error', function(error, req, res) { |
| 28 | res.writeHead(500, { |
| 29 | 'Content-Type': 'text/plain' |
| 30 | }); |
| 31 | |
| 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 | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 40 | // reloadDelay: 500, |
| 41 | // logLevel: 'debug', |
| 42 | // logConnections: true, |
Matteo Scandolo | 807fb01 | 2015-11-09 11:25:40 +0100 | [diff] [blame] | 43 | startPath: '#/', |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 44 | snippetOptions: { |
| 45 | rule: { |
| 46 | match: /<!-- browserSync -->/i |
| 47 | } |
| 48 | }, |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 49 | server: { |
| 50 | baseDir: options.src, |
| 51 | routes: { |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 52 | '/api': options.api, |
Matteo Scandolo | 6328d9b | 2015-11-05 16:02:11 +0100 | [diff] [blame] | 53 | '/xosHelpers/src': options.helpers |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 54 | }, |
| 55 | middleware: function(req, res, next){ |
Matteo Scandolo | c8b9532 | 2015-11-06 09:34:03 +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 | d3de390 | 2015-11-25 12:08:41 -0800 | [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 | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 65 | proxy.web(req, res); |
| 66 | } |
| 67 | else{ |
| 68 | next(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | |
Matteo Scandolo | 9348777 | 2015-11-04 15:15:17 +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 | }); |
Matteo Scandolo | 4ea1cdb | 2015-11-04 11:34:44 +0100 | [diff] [blame] | 78 | gulp.watch(options.src + '**/*.html', function(){ |
| 79 | browserSync.reload(); |
| 80 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 81 | }); |
| 82 | |
| 83 | // transpile js with sourceMaps |
| 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 | |
| 90 | // inject scripts |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 91 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 92 | return gulp.src(options.src + 'index.html') |
| 93 | .pipe( |
| 94 | inject( |
| 95 | gulp.src([ |
| 96 | options.tmp + '**/*.js', |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 97 | options.api + '*.js', |
| 98 | options.helpers + '**/*.js' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 99 | ]) |
| 100 | .pipe(angularFilesort()), |
| 101 | { |
| 102 | ignorePath: [options.src, '/../../ngXosLib'] |
| 103 | } |
| 104 | ) |
| 105 | ) |
| 106 | .pipe(gulp.dest(options.src)); |
| 107 | }); |
| 108 | |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +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 | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 123 | // inject bower dependencies with wiredep |
| 124 | gulp.task('bower', function () { |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 125 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 126 | .pipe(wiredep({devDependencies: true})) |
| 127 | .pipe(gulp.dest(options.src)); |
| 128 | }); |
| 129 | |
Matteo Scandolo | ab91f43 | 2015-11-06 18:25:52 +0100 | [diff] [blame] | 130 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 131 | browserSync.reload(); |
| 132 | }); |
| 133 | |
| 134 | gulp.task('cleanTmp', function(){ |
| 135 | return del([options.tmp + '**/*']); |
| 136 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 137 | |
| 138 | gulp.task('serve', function() { |
| 139 | runSequence( |
| 140 | 'bower', |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 141 | 'injectScript', |
| 142 | 'injectCss', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 143 | ['browser'] |
| 144 | ); |
| 145 | }); |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 146 | }; |