blob: f16d6f27a142f90b25e8311c3b099d308b14ee98 [file] [log] [blame]
Matteo Scandolo6d972642015-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 Scandoloc2d0f532015-10-29 16:13:35 +01006var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
Matteo Scandolo45778962015-10-29 16:17:59 +01009var wiredep = require('wiredep').stream;
Matteo Scandolod4f501c2015-10-29 17:24:18 +010010var httpProxy = require('http-proxy');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010011var del = require('del');
Matteo Scandolo6d972642015-10-29 15:50:15 +010012
Matteo Scandolo7dea2432016-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 Scandolod4f501c2015-10-29 17:24:18 +010022var proxy = httpProxy.createProxyServer({
Matteo Scandolo7dea2432016-03-24 15:11:29 -070023 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandolod4f501c2015-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 Scandolo6d972642015-10-29 15:50:15 +010030 });
31
Matteo Scandolod4f501c2015-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 Scandolobbcc01b2015-11-04 16:30:43 +010040 // reloadDelay: 500,
41 // logLevel: 'debug',
42 // logConnections: true,
Matteo Scandolo9a607262015-11-10 17:13:04 +010043 startPath: '#/',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010044 snippetOptions: {
45 rule: {
46 match: /<!-- browserSync -->/i
47 }
48 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010049 server: {
50 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010051 routes: {
52 '/api': options.api,
Matteo Scandolod44ceb42015-11-06 17:14:28 +010053 '/xosHelpers/src': options.helpers
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010054 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010055 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-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 Scandolo7dea2432016-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 Scandolod4f501c2015-10-29 17:24:18 +010065 proxy.web(req, res);
66 }
67 else{
68 next();
69 }
70 }
71 }
72 });
73
Matteo Scandolobbcc01b2015-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 Scandolod4f501c2015-10-29 17:24:18 +010081 });
82
83 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-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 Scandolod4f501c2015-10-29 17:24:18 +010090 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +010091 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010092 return gulp.src(options.src + 'index.html')
93 .pipe(
94 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010095 gulp.src([
96 options.tmp + '**/*.js',
97 options.api + '*.js',
98 options.helpers + '**/*.js'
99 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100100 .pipe(angularFilesort()),
101 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100102 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100103 }
104 )
105 )
Matteo Scandolo45778962015-10-29 16:17:59 +0100106 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +0100107 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100108
Matteo Scandolod44ceb42015-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 Scandolod4f501c2015-10-29 17:24:18 +0100123 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100124 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100125 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100126 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100127 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100128 });
129
Matteo Scandolo7db08432015-11-06 18:49:33 +0100130 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100131 browserSync.reload();
132 });
133
134 gulp.task('cleanTmp', function(){
135 return del([options.tmp + '**/*']);
136 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100137
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100138 gulp.task('serve', function() {
139 runSequence(
Matteo Scandolo45778962015-10-29 16:17:59 +0100140 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100141 'injectScript',
142 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100143 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100144 );
145 });
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100146};