blob: 7605294edcb0d09533c566e75dd4226ce11ff1d1 [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'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');
Matteo Scandoloce954e52015-11-04 11:31:05 +010011var del = require('del');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010012
Matteo Scandolo7ed6c762015-11-30 17:25:39 -080013const environment = process.env.NODE_ENV;
14
15if (environment){
16 var conf = require(`../env/${environment}.js`);
17}
18else{
19 var conf = require('../env/default.js')
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080020}
21
Matteo Scandoloe53ee052015-11-03 14:32:00 +010022var proxy = httpProxy.createProxyServer({
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080023 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010024});
25
26
27proxy.on('error', function(error, req, res) {
28 res.writeHead(500, {
29 'Content-Type': 'text/plain'
30 });
31
32 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 Scandoloce954e52015-11-04 11:31:05 +010040 // reloadDelay: 500,
41 // logLevel: 'debug',
42 // logConnections: true,
Matteo Scandolof1072ed2015-11-09 11:25:40 +010043 startPath: '#/',
Matteo Scandoloce954e52015-11-04 11:31:05 +010044 snippetOptions: {
45 rule: {
46 match: /<!-- browserSync -->/i
47 }
48 },
Matteo Scandoloe53ee052015-11-03 14:32:00 +010049 server: {
50 baseDir: options.src,
51 routes: {
Matteo Scandolo2b626742015-11-04 16:03:59 +010052 '/api': options.api,
Matteo Scandolo3c217ad2015-11-05 16:02:11 +010053 '/xosHelpers/src': options.helpers
Matteo Scandoloe53ee052015-11-03 14:32:00 +010054 },
55 middleware: function(req, res, next){
Matteo Scandolo07760222015-11-06 09:34:03 +010056 if(
57 req.url.indexOf('/xos/') !== -1 ||
58 req.url.indexOf('/xoslib/') !== -1 ||
59 req.url.indexOf('/hpcapi/') !== -1
60 ){
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080061 if(conf.xoscsrftoken && conf.xossessionid){
62 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
63 req.headers['x-csrftoken'] = conf.xoscsrftoken;
64 }
Matteo Scandoloe53ee052015-11-03 14:32:00 +010065 proxy.web(req, res);
66 }
67 else{
68 next();
69 }
70 }
71 }
72 });
73
Matteo Scandolo0a0f5f32015-11-04 15:15:17 +010074 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
75 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
76 browserSync.reload();
77 });
Matteo Scandolo73a3c2b2015-11-04 11:34:44 +010078 gulp.watch(options.src + '**/*.html', function(){
79 browserSync.reload();
80 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +010081 });
82
83 // transpile js with sourceMaps
84 gulp.task('babel', function(){
85 return gulp.src(options.scripts + '**/*.js')
86 .pipe(babel({sourceMaps: true}))
87 .pipe(gulp.dest(options.tmp));
88 });
89
90 // inject scripts
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010091 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloe53ee052015-11-03 14:32:00 +010092 return gulp.src(options.src + 'index.html')
93 .pipe(
94 inject(
95 gulp.src([
96 options.tmp + '**/*.js',
Matteo Scandolo2b626742015-11-04 16:03:59 +010097 options.api + '*.js',
98 options.helpers + '**/*.js'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010099 ])
100 .pipe(angularFilesort()),
101 {
102 ignorePath: [options.src, '/../../ngXosLib']
103 }
104 )
105 )
106 .pipe(gulp.dest(options.src));
107 });
108
Matteo Scandoloba2af3d2015-11-06 10:35:20 +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 Scandoloe53ee052015-11-03 14:32:00 +0100123 // inject bower dependencies with wiredep
124 gulp.task('bower', function () {
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100125 return gulp.src(options.src + 'index.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100126 .pipe(wiredep({devDependencies: true}))
127 .pipe(gulp.dest(options.src));
128 });
129
Matteo Scandoloba5e19d2015-11-06 18:25:52 +0100130 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandoloce954e52015-11-04 11:31:05 +0100131 browserSync.reload();
132 });
133
134 gulp.task('cleanTmp', function(){
135 return del([options.tmp + '**/*']);
136 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100137
138 gulp.task('serve', function() {
139 runSequence(
140 'bower',
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100141 'injectScript',
142 'injectCss',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100143 ['browser']
144 );
145 });
Matteo Scandolo9323b1b2015-11-25 12:08:41 -0800146};