blob: 33f87688db7e33da187e3a321a38870f0ddae360 [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 Scandolob7c91cf2016-03-29 14:23:20 -070012var sass = require('gulp-sass');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010013
Matteo Scandolo7ed6c762015-11-30 17:25:39 -080014const environment = process.env.NODE_ENV;
15
16if (environment){
17 var conf = require(`../env/${environment}.js`);
18}
19else{
20 var conf = require('../env/default.js')
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080021}
22
Matteo Scandoloe53ee052015-11-03 14:32:00 +010023var proxy = httpProxy.createProxyServer({
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080024 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010025});
26
27
28proxy.on('error', function(error, req, res) {
29 res.writeHead(500, {
30 'Content-Type': 'text/plain'
31 });
32
33 console.error('[Proxy]', error);
34});
35
36module.exports = function(options){
37
38 // open in browser with sync and proxy to 0.0.0.0
39 gulp.task('browser', function() {
40 browserSync.init({
Matteo Scandoloce954e52015-11-04 11:31:05 +010041 // reloadDelay: 500,
42 // logLevel: 'debug',
43 // logConnections: true,
Matteo Scandolof1072ed2015-11-09 11:25:40 +010044 startPath: '#/',
Matteo Scandoloce954e52015-11-04 11:31:05 +010045 snippetOptions: {
46 rule: {
47 match: /<!-- browserSync -->/i
48 }
49 },
Matteo Scandoloe53ee052015-11-03 14:32:00 +010050 server: {
51 baseDir: options.src,
52 routes: {
Matteo Scandolo2b626742015-11-04 16:03:59 +010053 '/api': options.api,
Matteo Scandolo3c217ad2015-11-05 16:02:11 +010054 '/xosHelpers/src': options.helpers
Matteo Scandoloe53ee052015-11-03 14:32:00 +010055 },
56 middleware: function(req, res, next){
Matteo Scandolo07760222015-11-06 09:34:03 +010057 if(
58 req.url.indexOf('/xos/') !== -1 ||
59 req.url.indexOf('/xoslib/') !== -1 ||
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070060 req.url.indexOf('/hpcapi/') !== -1 ||
61 req.url.indexOf('/api/') !== -1
Matteo Scandolo07760222015-11-06 09:34:03 +010062 ){
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080063 if(conf.xoscsrftoken && conf.xossessionid){
64 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
65 req.headers['x-csrftoken'] = conf.xoscsrftoken;
66 }
Matteo Scandoloe53ee052015-11-03 14:32:00 +010067 proxy.web(req, res);
68 }
69 else{
70 next();
71 }
72 }
73 }
74 });
75
Matteo Scandolo0a0f5f32015-11-04 15:15:17 +010076 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
77 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
78 browserSync.reload();
79 });
Matteo Scandolo73a3c2b2015-11-04 11:34:44 +010080 gulp.watch(options.src + '**/*.html', function(){
81 browserSync.reload();
82 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070083 gulp.watch(options.css + '**/*.css', function(){
84 browserSync.reload();
85 });
86 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
87 browserSync.reload();
88 });
89 });
90
91 // compile sass
92 gulp.task('sass', function () {
93 return gulp.src(`${options.sass}/**/*.scss`)
94 .pipe(sass().on('error', sass.logError))
95 .pipe(gulp.dest(options.css));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010096 });
97
98 // transpile js with sourceMaps
99 gulp.task('babel', function(){
100 return gulp.src(options.scripts + '**/*.js')
101 .pipe(babel({sourceMaps: true}))
102 .pipe(gulp.dest(options.tmp));
103 });
104
105 // inject scripts
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100106 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100107 return gulp.src(options.src + 'index.html')
108 .pipe(
109 inject(
110 gulp.src([
111 options.tmp + '**/*.js',
Matteo Scandolo2b626742015-11-04 16:03:59 +0100112 options.api + '*.js',
113 options.helpers + '**/*.js'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100114 ])
115 .pipe(angularFilesort()),
116 {
117 ignorePath: [options.src, '/../../ngXosLib']
118 }
119 )
120 )
121 .pipe(gulp.dest(options.src));
122 });
123
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100124 // inject CSS
125 gulp.task('injectCss', function(){
126 return gulp.src(options.src + 'index.html')
127 .pipe(
128 inject(
129 gulp.src(options.src + 'css/*.css'),
130 {
131 ignorePath: [options.src]
132 }
133 )
134 )
135 .pipe(gulp.dest(options.src));
136 });
137
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100138 // inject bower dependencies with wiredep
139 gulp.task('bower', function () {
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100140 return gulp.src(options.src + 'index.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100141 .pipe(wiredep({devDependencies: true}))
142 .pipe(gulp.dest(options.src));
143 });
144
Matteo Scandoloba5e19d2015-11-06 18:25:52 +0100145 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandoloce954e52015-11-04 11:31:05 +0100146 browserSync.reload();
147 });
148
149 gulp.task('cleanTmp', function(){
150 return del([options.tmp + '**/*']);
151 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100152
153 gulp.task('serve', function() {
154 runSequence(
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700155 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100156 'bower',
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100157 'injectScript',
158 'injectCss',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100159 ['browser']
160 );
161 });
Matteo Scandolo9323b1b2015-11-25 12:08:41 -0800162};