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'); |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 6 | var es = require('event-stream'); |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 7 | var runSequence = require('run-sequence'); |
| 8 | var angularFilesort = require('gulp-angular-filesort'); |
| 9 | var babel = require('gulp-babel'); |
| 10 | var wiredep = require('wiredep').stream; |
| 11 | var httpProxy = require('http-proxy'); |
| 12 | var del = require('del'); |
| 13 | var sass = require('gulp-sass'); |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 14 | var debug = require('gulp-debug'); |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 15 | |
| 16 | const environment = process.env.NODE_ENV; |
| 17 | |
| 18 | if (environment){ |
| 19 | var conf = require(`../env/${environment}.js`); |
| 20 | } |
| 21 | else{ |
| 22 | var conf = require('../env/default.js') |
| 23 | } |
| 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){ |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 39 | // open in browser with sync and proxy to 0.0.0.0 |
| 40 | gulp.task('browser', function() { |
Matteo Scandolo | 1ba8b30 | 2016-04-14 10:53:30 -0700 | [diff] [blame] | 41 | console.log(options.helpers); |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 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: { |
Matteo Scandolo | 1ba8b30 | 2016-04-14 10:53:30 -0700 | [diff] [blame] | 55 | // '/xosHelpers/src': options.helpers, |
| 56 | '/xos/core/xoslib/static/js/vendor': options.helpers |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 57 | }, |
| 58 | middleware: function(req, res, next){ |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 59 | if( |
Matteo Scandolo | 1ba8b30 | 2016-04-14 10:53:30 -0700 | [diff] [blame] | 60 | // req.url.indexOf('/xos/') !== -1 || |
| 61 | // req.url.indexOf('/xoslib/') !== -1 || |
| 62 | // req.url.indexOf('/hpcapi/') !== -1 || |
Matteo Scandolo | bd2e5cd | 2016-04-12 11:59:29 -0700 | [diff] [blame] | 63 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 64 | ){ |
| 65 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 66 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 67 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 68 | } |
| 69 | proxy.web(req, res); |
| 70 | } |
| 71 | else{ |
| 72 | next(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 79 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 80 | browserSync.reload(); |
| 81 | }); |
| 82 | gulp.watch(options.src + '**/*.html', function(){ |
| 83 | browserSync.reload(); |
| 84 | }); |
| 85 | gulp.watch(options.css + '**/*.css', function(){ |
| 86 | browserSync.reload(); |
| 87 | }); |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 88 | |
| 89 | gulp.watch(options.helpers + 'ngXosHelpers.js', function(){ |
| 90 | browserSync.reload(); |
| 91 | }); |
| 92 | |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 93 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 94 | browserSync.reload(); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | // compile sass |
| 99 | gulp.task('sass', function () { |
| 100 | return gulp.src(`${options.sass}/**/*.scss`) |
| 101 | .pipe(sass().on('error', sass.logError)) |
| 102 | .pipe(gulp.dest(options.css)); |
| 103 | }); |
| 104 | |
| 105 | // transpile js with sourceMaps |
| 106 | gulp.task('babel', function(){ |
| 107 | return gulp.src(options.scripts + '**/*.js') |
| 108 | .pipe(babel({sourceMaps: true})) |
| 109 | .pipe(gulp.dest(options.tmp)); |
| 110 | }); |
| 111 | |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 112 | // // inject sourceMap |
| 113 | // gulp.task('injectMaps', function(){ |
| 114 | // return gulp.src(options.src + 'index.html') |
| 115 | // .pipe( |
| 116 | // inject( |
| 117 | // gulp.src([ |
| 118 | // options.helpersSourceMaps + '**/*.js.map' |
| 119 | // ], {read: false}).pipe(debug()), |
| 120 | // { |
| 121 | // starttag: '<!-- inject:maps -->', |
| 122 | // // ignorePath: [options.src, '/../../ngXosLib'] |
| 123 | // } |
| 124 | // ) |
| 125 | // ) |
| 126 | // .pipe(gulp.dest(options.src)); |
| 127 | // }); |
| 128 | |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 129 | // inject scripts |
| 130 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 131 | |
| 132 | var appScripts = gulp.src([ |
| 133 | options.tmp + '**/*.js', |
| 134 | options.helpers + 'ngXosHelpers.js' |
| 135 | ]) |
| 136 | .pipe(angularFilesort()).pipe(debug()); |
| 137 | |
| 138 | var helpersSourceMaps = gulp.src([ |
| 139 | options.helpersSourceMaps + '**/*.js.map' |
| 140 | ]).pipe(debug()); |
| 141 | |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 142 | return gulp.src(options.src + 'index.html') |
| 143 | .pipe( |
| 144 | inject( |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 145 | es.merge(appScripts, helpersSourceMaps), |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 146 | { |
| 147 | ignorePath: [options.src, '/../../ngXosLib'] |
| 148 | } |
| 149 | ) |
| 150 | ) |
| 151 | .pipe(gulp.dest(options.src)); |
| 152 | }); |
| 153 | |
| 154 | // inject CSS |
| 155 | gulp.task('injectCss', function(){ |
| 156 | return gulp.src(options.src + 'index.html') |
| 157 | .pipe( |
| 158 | inject( |
| 159 | gulp.src(options.src + 'css/*.css'), |
| 160 | { |
| 161 | ignorePath: [options.src] |
| 162 | } |
| 163 | ) |
| 164 | ) |
| 165 | .pipe(gulp.dest(options.src)); |
| 166 | }); |
| 167 | |
| 168 | // inject bower dependencies with wiredep |
| 169 | gulp.task('bower', function () { |
| 170 | return gulp.src(options.src + 'index.html') |
| 171 | .pipe(wiredep({devDependencies: true})) |
| 172 | .pipe(gulp.dest(options.src)); |
| 173 | }); |
| 174 | |
| 175 | gulp.task('js-watch', ['injectScript'], function(){ |
| 176 | browserSync.reload(); |
| 177 | }); |
| 178 | |
| 179 | gulp.task('cleanTmp', function(){ |
| 180 | return del([options.tmp + '**/*']); |
| 181 | }); |
| 182 | |
| 183 | gulp.task('serve', function() { |
| 184 | runSequence( |
| 185 | 'sass', |
| 186 | 'bower', |
| 187 | 'injectScript', |
| 188 | 'injectCss', |
| 189 | ['browser'] |
| 190 | ); |
| 191 | }); |
| 192 | }; |