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