blob: 162bc97b73c94f146d5fee711b531f98d7560e34 [file] [log] [blame]
Matteo Scandolodf35ca92016-02-25 09:19:41 -08001'use strict';
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var inject = require('gulp-inject');
6var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
9var wiredep = require('wiredep').stream;
10var httpProxy = require('http-proxy');
11var del = require('del');
12
13const 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
22var proxy = httpProxy.createProxyServer({
23 target: conf.host || 'http://0.0.0.0:9999'
24});
25
Matteo Scandolo97646d62016-03-07 14:14:00 -080026var traffic = httpProxy.createProxyServer({
27 target: 'http://10.128.13.3'
28});
29
Matteo Scandolodf35ca92016-02-25 09:19:41 -080030
31proxy.on('error', function(error, req, res) {
32 res.writeHead(500, {
33 'Content-Type': 'text/plain'
34 });
35
36 console.error('[Proxy]', error);
37});
38
39module.exports = function(options){
40
41 // open in browser with sync and proxy to 0.0.0.0
42 gulp.task('browser', function() {
43 browserSync.init({
44 // reloadDelay: 500,
45 // logLevel: 'debug',
46 // logConnections: true,
47 startPath: '#/',
48 snippetOptions: {
49 rule: {
50 match: /<!-- browserSync -->/i
51 }
52 },
53 server: {
54 baseDir: options.src,
55 routes: {
56 '/api': options.api,
57 '/xosHelpers/src': options.helpers
58 },
59 middleware: function(req, res, next){
60 if(
61 req.url.indexOf('/xos/') !== -1 ||
62 req.url.indexOf('/xoslib/') !== -1 ||
63 req.url.indexOf('/hpcapi/') !== -1
64 ){
65 if(conf.xoscsrftoken && conf.xossessionid){
66 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
67 req.headers['x-csrftoken'] = conf.xoscsrftoken;
68 }
69 proxy.web(req, res);
70 }
Matteo Scandolo97646d62016-03-07 14:14:00 -080071 else if(req.url.indexOf('videoLocal') !== -1){
72 console.log('traffic: ', req.url);
73 traffic.web(req, res);
74 }
Matteo Scandolodf35ca92016-02-25 09:19:41 -080075 else{
76 next();
77 }
78 }
79 }
80 });
81
82 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
83 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
84 browserSync.reload();
85 });
86 gulp.watch(options.src + '**/*.html', function(){
87 browserSync.reload();
88 });
89 });
90
91 // transpile js with sourceMaps
92 gulp.task('babel', function(){
93 return gulp.src(options.scripts + '**/*.js')
94 .pipe(babel({sourceMaps: true}))
95 .pipe(gulp.dest(options.tmp));
96 });
97
98 // inject scripts
99 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
100 return gulp.src(options.src + 'index.html')
101 .pipe(
102 inject(
103 gulp.src([
104 options.tmp + '**/*.js',
105 options.api + '*.js',
106 options.helpers + '**/*.js'
107 ])
108 .pipe(angularFilesort()),
109 {
110 ignorePath: [options.src, '/../../ngXosLib']
111 }
112 )
113 )
114 .pipe(gulp.dest(options.src));
115 });
116
117 // inject CSS
118 gulp.task('injectCss', function(){
119 return gulp.src(options.src + 'index.html')
120 .pipe(
121 inject(
122 gulp.src(options.src + 'css/*.css'),
123 {
124 ignorePath: [options.src]
125 }
126 )
127 )
128 .pipe(gulp.dest(options.src));
129 });
130
131 // inject bower dependencies with wiredep
132 gulp.task('bower', function () {
133 return gulp.src(options.src + 'index.html')
134 .pipe(wiredep({devDependencies: true}))
135 .pipe(gulp.dest(options.src));
136 });
137
138 gulp.task('js-watch', ['injectScript'], function(){
139 browserSync.reload();
140 });
141
142 gulp.task('cleanTmp', function(){
143 return del([options.tmp + '**/*']);
144 });
145
146 gulp.task('serve', function() {
147 runSequence(
148 'bower',
149 'injectScript',
150 'injectCss',
151 ['browser']
152 );
153 });
154};