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