blob: f16d6f27a142f90b25e8311c3b099d308b14ee98 [file] [log] [blame]
Matteo Scandolocb11e0c2015-10-29 15:50:15 +01001'use strict';
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var inject = require('gulp-inject');
Matteo Scandolo2729d082015-10-29 16:13:35 +01006var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
Matteo Scandolo486f2682015-10-29 16:17:59 +01009var wiredep = require('wiredep').stream;
Matteo Scandoloc952aa92015-10-29 17:24:18 +010010var httpProxy = require('http-proxy');
Matteo Scandolo82697c02015-11-04 16:30:43 +010011var del = require('del');
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010012
Matteo Scandolo6297a262016-03-24 15:11:29 -070013const environment = process.env.NODE_ENV;
14
15if (environment){
16 var conf = require(`../env/${environment}.js`);
17}
18else{
19 var conf = require('../env/default.js')
20}
21
Matteo Scandoloc952aa92015-10-29 17:24:18 +010022var proxy = httpProxy.createProxyServer({
Matteo Scandolo6297a262016-03-24 15:11:29 -070023 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandoloc952aa92015-10-29 17:24:18 +010024});
25
26
27proxy.on('error', function(error, req, res) {
28 res.writeHead(500, {
29 'Content-Type': 'text/plain'
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010030 });
31
Matteo Scandoloc952aa92015-10-29 17:24:18 +010032 console.error('[Proxy]', error);
33});
34
35module.exports = function(options){
36
37 // open in browser with sync and proxy to 0.0.0.0
38 gulp.task('browser', function() {
39 browserSync.init({
Matteo Scandolo82697c02015-11-04 16:30:43 +010040 // reloadDelay: 500,
41 // logLevel: 'debug',
42 // logConnections: true,
Matteo Scandolo2d2484f2015-11-10 17:13:04 +010043 startPath: '#/',
Matteo Scandolo82697c02015-11-04 16:30:43 +010044 snippetOptions: {
45 rule: {
46 match: /<!-- browserSync -->/i
47 }
48 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010049 server: {
50 baseDir: options.src,
Matteo Scandolo82697c02015-11-04 16:30:43 +010051 routes: {
52 '/api': options.api,
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010053 '/xosHelpers/src': options.helpers
Matteo Scandolo82697c02015-11-04 16:30:43 +010054 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010055 middleware: function(req, res, next){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010056 if(
57 req.url.indexOf('/xos/') !== -1 ||
58 req.url.indexOf('/xoslib/') !== -1 ||
59 req.url.indexOf('/hpcapi/') !== -1
60 ){
Matteo Scandolo6297a262016-03-24 15:11:29 -070061 if(conf.xoscsrftoken && conf.xossessionid){
62 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
63 req.headers['x-csrftoken'] = conf.xoscsrftoken;
64 }
Matteo Scandoloc952aa92015-10-29 17:24:18 +010065 proxy.web(req, res);
66 }
67 else{
68 next();
69 }
70 }
71 }
72 });
73
Matteo Scandolo82697c02015-11-04 16:30:43 +010074 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
75 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
76 browserSync.reload();
77 });
78 gulp.watch(options.src + '**/*.html', function(){
79 browserSync.reload();
80 });
Matteo Scandoloc952aa92015-10-29 17:24:18 +010081 });
82
83 // transpile js with sourceMaps
Matteo Scandolo2729d082015-10-29 16:13:35 +010084 gulp.task('babel', function(){
85 return gulp.src(options.scripts + '**/*.js')
86 .pipe(babel({sourceMaps: true}))
87 .pipe(gulp.dest(options.tmp));
88 });
89
Matteo Scandoloc952aa92015-10-29 17:24:18 +010090 // inject scripts
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010091 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolo2729d082015-10-29 16:13:35 +010092 return gulp.src(options.src + 'index.html')
93 .pipe(
94 inject(
Matteo Scandolo82697c02015-11-04 16:30:43 +010095 gulp.src([
96 options.tmp + '**/*.js',
97 options.api + '*.js',
98 options.helpers + '**/*.js'
99 ])
Matteo Scandolo2729d082015-10-29 16:13:35 +0100100 .pipe(angularFilesort()),
101 {
Matteo Scandolo82697c02015-11-04 16:30:43 +0100102 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandolo2729d082015-10-29 16:13:35 +0100103 }
104 )
105 )
Matteo Scandolo486f2682015-10-29 16:17:59 +0100106 .pipe(gulp.dest(options.src));
Matteo Scandolocb11e0c2015-10-29 15:50:15 +0100107 });
Matteo Scandolo2729d082015-10-29 16:13:35 +0100108
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100109 // inject CSS
110 gulp.task('injectCss', function(){
111 return gulp.src(options.src + 'index.html')
112 .pipe(
113 inject(
114 gulp.src(options.src + 'css/*.css'),
115 {
116 ignorePath: [options.src]
117 }
118 )
119 )
120 .pipe(gulp.dest(options.src));
121 });
122
Matteo Scandoloc952aa92015-10-29 17:24:18 +0100123 // inject bower dependencies with wiredep
Matteo Scandolo486f2682015-10-29 16:17:59 +0100124 gulp.task('bower', function () {
Matteo Scandolo82697c02015-11-04 16:30:43 +0100125 return gulp.src(options.src + 'index.html')
Matteo Scandoloc952aa92015-10-29 17:24:18 +0100126 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo486f2682015-10-29 16:17:59 +0100127 .pipe(gulp.dest(options.src));
Matteo Scandoloc952aa92015-10-29 17:24:18 +0100128 });
129
Matteo Scandolo6f7f9742015-11-06 18:49:33 +0100130 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolo82697c02015-11-04 16:30:43 +0100131 browserSync.reload();
132 });
133
134 gulp.task('cleanTmp', function(){
135 return del([options.tmp + '**/*']);
136 });
Matteo Scandolo486f2682015-10-29 16:17:59 +0100137
Matteo Scandolo2729d082015-10-29 16:13:35 +0100138 gulp.task('serve', function() {
139 runSequence(
Matteo Scandolo486f2682015-10-29 16:17:59 +0100140 'bower',
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100141 'injectScript',
142 'injectCss',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100143 ['browser']
Matteo Scandolo2729d082015-10-29 16:13:35 +0100144 );
145 });
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100146};