Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -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 | 8386933 | 2015-12-14 14:26:20 -0800 | [diff] [blame] | 12 | var debug = require('gulp-debug'); |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -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 | console.log(conf); |
| 24 | |
| 25 | var proxy = httpProxy.createProxyServer({ |
| 26 | target: conf.host || 'http://0.0.0.0:9999' |
| 27 | }); |
| 28 | |
| 29 | |
| 30 | proxy.on('error', function(error, req, res) { |
| 31 | res.writeHead(500, { |
| 32 | 'Content-Type': 'text/plain' |
| 33 | }); |
| 34 | |
| 35 | console.error('[Proxy]', error); |
| 36 | }); |
| 37 | |
| 38 | module.exports = function(options){ |
| 39 | |
| 40 | // open in browser with sync and proxy to 0.0.0.0 |
| 41 | gulp.task('browser', function() { |
| 42 | browserSync.init({ |
| 43 | // reloadDelay: 500, |
| 44 | // logLevel: 'debug', |
| 45 | // logConnections: true, |
| 46 | startPath: '#/', |
| 47 | snippetOptions: { |
| 48 | rule: { |
| 49 | match: /<!-- browserSync -->/i |
| 50 | } |
| 51 | }, |
| 52 | server: { |
| 53 | baseDir: options.src, |
| 54 | routes: { |
| 55 | '/api': options.api, |
| 56 | '/xosHelpers/src': options.helpers |
| 57 | }, |
| 58 | middleware: function(req, res, next){ |
| 59 | if( |
| 60 | req.url.indexOf('/xos/') !== -1 || |
| 61 | req.url.indexOf('/xoslib/') !== -1 || |
| 62 | req.url.indexOf('/hpcapi/') !== -1 |
| 63 | ){ |
| 64 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 65 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 66 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 67 | } |
| 68 | proxy.web(req, res); |
| 69 | } |
| 70 | else{ |
| 71 | next(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 78 | |
| 79 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 80 | console.log('Bower Package added!'); |
| 81 | browserSync.reload(); |
| 82 | }); |
| 83 | gulp.watch(options.src + '**/*.html', function(){ |
| 84 | browserSync.reload(); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | // transpile js with sourceMaps |
| 89 | gulp.task('babel', function(){ |
Matteo Scandolo | 8386933 | 2015-12-14 14:26:20 -0800 | [diff] [blame] | 90 | return gulp.src([options.scripts + '**/*.js']) |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 91 | .pipe(babel({sourceMaps: true})) |
| 92 | .pipe(gulp.dest(options.tmp)); |
| 93 | }); |
| 94 | |
| 95 | // inject scripts |
Matteo Scandolo | 8386933 | 2015-12-14 14:26:20 -0800 | [diff] [blame] | 96 | gulp.task('injectScript', function(){ |
| 97 | console.log(options.tmp); |
| 98 | runSequence( |
| 99 | 'cleanTmp', |
| 100 | 'babel', |
| 101 | function() { |
| 102 | return gulp.src(options.src + 'index.html') |
| 103 | .pipe( |
| 104 | inject( |
| 105 | gulp.src([ |
| 106 | options.tmp + '**/*.js', |
| 107 | options.api + '*.js', |
| 108 | options.helpers + '**/*.js' |
| 109 | ]) |
Matteo Scandolo | 6c6b928 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 110 | // .pipe(debug({title: 'unicorn:'})) |
Matteo Scandolo | 8386933 | 2015-12-14 14:26:20 -0800 | [diff] [blame] | 111 | .pipe(angularFilesort()), |
| 112 | { |
| 113 | ignorePath: [options.src, '/../../ngXosLib'] |
| 114 | } |
| 115 | ) |
| 116 | ) |
| 117 | .pipe(gulp.dest(options.src)); |
| 118 | } |
| 119 | ); |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 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 | 'bower', |
| 154 | 'injectScript', |
| 155 | 'injectCss', |
| 156 | ['browser'] |
| 157 | ); |
| 158 | }); |
| 159 | }; |