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