Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [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'); |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 11 | var del = require('del'); |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 12 | var sass = require('gulp-sass'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 13 | |
Matteo Scandolo | af977a3 | 2015-11-30 17:25:39 -0800 | [diff] [blame] | 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') |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 21 | } |
| 22 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 23 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 24 | target: conf.host || 'http://0.0.0.0:9999' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 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({ |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 41 | // reloadDelay: 500, |
| 42 | // logLevel: 'debug', |
| 43 | // logConnections: true, |
Matteo Scandolo | 807fb01 | 2015-11-09 11:25:40 +0100 | [diff] [blame] | 44 | startPath: '#/', |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 45 | snippetOptions: { |
| 46 | rule: { |
| 47 | match: /<!-- browserSync -->/i |
| 48 | } |
| 49 | }, |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 50 | server: { |
| 51 | baseDir: options.src, |
| 52 | routes: { |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 53 | '/api': options.api, |
Matteo Scandolo | 6328d9b | 2015-11-05 16:02:11 +0100 | [diff] [blame] | 54 | '/xosHelpers/src': options.helpers |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 55 | }, |
| 56 | middleware: function(req, res, next){ |
Matteo Scandolo | c8b9532 | 2015-11-06 09:34:03 +0100 | [diff] [blame] | 57 | if( |
| 58 | req.url.indexOf('/xos/') !== -1 || |
| 59 | req.url.indexOf('/xoslib/') !== -1 || |
| 60 | req.url.indexOf('/hpcapi/') !== -1 |
| 61 | ){ |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 62 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 63 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 64 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 65 | } |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 66 | proxy.web(req, res); |
| 67 | } |
| 68 | else{ |
| 69 | next(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | |
Matteo Scandolo | 9348777 | 2015-11-04 15:15:17 +0100 | [diff] [blame] | 75 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 76 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 77 | browserSync.reload(); |
| 78 | }); |
Matteo Scandolo | 4ea1cdb | 2015-11-04 11:34:44 +0100 | [diff] [blame] | 79 | gulp.watch(options.src + '**/*.html', function(){ |
| 80 | browserSync.reload(); |
| 81 | }); |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -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 | }); |
| 88 | }); |
| 89 | |
| 90 | // compile sass |
| 91 | gulp.task('sass', function () { |
| 92 | return gulp.src(`${options.sass}/**/*.scss`) |
| 93 | .pipe(sass().on('error', sass.logError)) |
| 94 | .pipe(gulp.dest(options.css)); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | // transpile js with sourceMaps |
| 98 | gulp.task('babel', function(){ |
| 99 | return gulp.src(options.scripts + '**/*.js') |
| 100 | .pipe(babel({sourceMaps: true})) |
| 101 | .pipe(gulp.dest(options.tmp)); |
| 102 | }); |
| 103 | |
| 104 | // inject scripts |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 105 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 106 | return gulp.src(options.src + 'index.html') |
| 107 | .pipe( |
| 108 | inject( |
| 109 | gulp.src([ |
| 110 | options.tmp + '**/*.js', |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 111 | options.api + '*.js', |
| 112 | options.helpers + '**/*.js' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 113 | ]) |
| 114 | .pipe(angularFilesort()), |
| 115 | { |
| 116 | ignorePath: [options.src, '/../../ngXosLib'] |
| 117 | } |
| 118 | ) |
| 119 | ) |
| 120 | .pipe(gulp.dest(options.src)); |
| 121 | }); |
| 122 | |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 123 | // inject CSS |
| 124 | gulp.task('injectCss', function(){ |
| 125 | return gulp.src(options.src + 'index.html') |
| 126 | .pipe( |
| 127 | inject( |
| 128 | gulp.src(options.src + 'css/*.css'), |
| 129 | { |
| 130 | ignorePath: [options.src] |
| 131 | } |
| 132 | ) |
| 133 | ) |
| 134 | .pipe(gulp.dest(options.src)); |
| 135 | }); |
| 136 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 137 | // inject bower dependencies with wiredep |
| 138 | gulp.task('bower', function () { |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 139 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 140 | .pipe(wiredep({devDependencies: true})) |
| 141 | .pipe(gulp.dest(options.src)); |
| 142 | }); |
| 143 | |
Matteo Scandolo | ab91f43 | 2015-11-06 18:25:52 +0100 | [diff] [blame] | 144 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 145 | browserSync.reload(); |
| 146 | }); |
| 147 | |
| 148 | gulp.task('cleanTmp', function(){ |
| 149 | return del([options.tmp + '**/*']); |
| 150 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 151 | |
| 152 | gulp.task('serve', function() { |
| 153 | runSequence( |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 154 | 'sass', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 155 | 'bower', |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 156 | 'injectScript', |
| 157 | 'injectCss', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 158 | ['browser'] |
| 159 | ); |
| 160 | }); |
Matteo Scandolo | d3de390 | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 161 | }; |