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'); |
| 12 | |
| 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 | |
| 22 | var proxy = httpProxy.createProxyServer({ |
| 23 | target: conf.host || 'http://0.0.0.0:9999' |
| 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({ |
| 40 | // reloadDelay: 500, |
| 41 | // logLevel: 'debug', |
| 42 | // logConnections: true, |
| 43 | startPath: '#/', |
| 44 | snippetOptions: { |
| 45 | rule: { |
| 46 | match: /<!-- browserSync -->/i |
| 47 | } |
| 48 | }, |
| 49 | server: { |
| 50 | baseDir: options.src, |
| 51 | routes: { |
| 52 | '/api': options.api, |
| 53 | '/xosHelpers/src': options.helpers |
| 54 | }, |
| 55 | middleware: function(req, res, next){ |
| 56 | if( |
| 57 | req.url.indexOf('/xos/') !== -1 || |
| 58 | req.url.indexOf('/xoslib/') !== -1 || |
| 59 | req.url.indexOf('/hpcapi/') !== -1 |
| 60 | ){ |
| 61 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 62 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 63 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 64 | } |
| 65 | proxy.web(req, res); |
| 66 | } |
| 67 | else{ |
| 68 | next(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | |
| 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 | }); |
| 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 |
| 91 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 92 | return gulp.src(options.src + 'index.html') |
| 93 | .pipe( |
| 94 | inject( |
| 95 | gulp.src([ |
| 96 | options.tmp + '**/*.js', |
| 97 | options.api + '*.js', |
| 98 | options.helpers + '**/*.js' |
| 99 | ]) |
| 100 | .pipe(angularFilesort()), |
| 101 | { |
| 102 | ignorePath: [options.src, '/../../ngXosLib'] |
| 103 | } |
| 104 | ) |
| 105 | ) |
| 106 | .pipe(gulp.dest(options.src)); |
| 107 | }); |
| 108 | |
| 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 | |
| 123 | // inject bower dependencies with wiredep |
| 124 | gulp.task('bower', function () { |
| 125 | return gulp.src(options.src + 'index.html') |
| 126 | .pipe(wiredep({devDependencies: true})) |
| 127 | .pipe(gulp.dest(options.src)); |
| 128 | }); |
| 129 | |
| 130 | gulp.task('js-watch', ['injectScript'], function(){ |
| 131 | browserSync.reload(); |
| 132 | }); |
| 133 | |
| 134 | gulp.task('cleanTmp', function(){ |
| 135 | return del([options.tmp + '**/*']); |
| 136 | }); |
| 137 | |
| 138 | gulp.task('serve', function() { |
| 139 | runSequence( |
| 140 | 'bower', |
| 141 | 'injectScript', |
| 142 | 'injectCss', |
| 143 | ['browser'] |
| 144 | ); |
| 145 | }); |
| 146 | }; |