Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -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 | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 12 | var sass = require('gulp-sass'); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -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 | 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: { |
| 53 | '/api': options.api, |
| 54 | '/xosHelpers/src': options.helpers |
| 55 | }, |
| 56 | middleware: function(req, res, next){ |
| 57 | if( |
| 58 | req.url.indexOf('/xos/') !== -1 || |
| 59 | req.url.indexOf('/xoslib/') !== -1 || |
| 60 | req.url.indexOf('/hpcapi/') !== -1 |
| 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 | }); |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 82 | gulp.watch(options.css + '**/*.css', function(){ |
| 83 | browserSync.reload(); |
| 84 | }); |
| 85 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 86 | browserSync.reload(); |
| 87 | }); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 88 | }); |
| 89 | |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 90 | gulp.task('sass', function () { |
| 91 | return gulp.src(`${options.sass}/**/*.scss`) |
| 92 | .pipe(sass().on('error', sass.logError)) |
| 93 | .pipe(gulp.dest(options.css)); |
| 94 | }); |
| 95 | |
| 96 | // gulp.task('sass:watch', function () { |
| 97 | // gulp.watch('./sass/**/*.scss', ['sass']); |
| 98 | // }); |
| 99 | |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 100 | // transpile js with sourceMaps |
| 101 | gulp.task('babel', function(){ |
| 102 | return gulp.src(options.scripts + '**/*.js') |
| 103 | .pipe(babel({sourceMaps: true})) |
| 104 | .pipe(gulp.dest(options.tmp)); |
| 105 | }); |
| 106 | |
| 107 | // inject scripts |
| 108 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 109 | return gulp.src(options.src + 'index.html') |
| 110 | .pipe( |
| 111 | inject( |
| 112 | gulp.src([ |
| 113 | options.tmp + '**/*.js', |
| 114 | options.api + '*.js', |
| 115 | options.helpers + '**/*.js' |
| 116 | ]) |
| 117 | .pipe(angularFilesort()), |
| 118 | { |
| 119 | ignorePath: [options.src, '/../../ngXosLib'] |
| 120 | } |
| 121 | ) |
| 122 | ) |
| 123 | .pipe(gulp.dest(options.src)); |
| 124 | }); |
| 125 | |
| 126 | // inject CSS |
| 127 | gulp.task('injectCss', function(){ |
| 128 | return gulp.src(options.src + 'index.html') |
| 129 | .pipe( |
| 130 | inject( |
| 131 | gulp.src(options.src + 'css/*.css'), |
| 132 | { |
| 133 | ignorePath: [options.src] |
| 134 | } |
| 135 | ) |
| 136 | ) |
| 137 | .pipe(gulp.dest(options.src)); |
| 138 | }); |
| 139 | |
| 140 | // inject bower dependencies with wiredep |
| 141 | gulp.task('bower', function () { |
| 142 | return gulp.src(options.src + 'index.html') |
| 143 | .pipe(wiredep({devDependencies: true})) |
| 144 | .pipe(gulp.dest(options.src)); |
| 145 | }); |
| 146 | |
| 147 | gulp.task('js-watch', ['injectScript'], function(){ |
| 148 | browserSync.reload(); |
| 149 | }); |
| 150 | |
| 151 | gulp.task('cleanTmp', function(){ |
| 152 | return del([options.tmp + '**/*']); |
| 153 | }); |
| 154 | |
| 155 | gulp.task('serve', function() { |
| 156 | runSequence( |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 157 | 'sass', |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 158 | 'bower', |
| 159 | 'injectScript', |
| 160 | 'injectCss', |
| 161 | ['browser'] |
| 162 | ); |
| 163 | }); |
| 164 | }; |