blob: 13659c3bd868b363da8d4dc810dd02fe5cc82f6b [file] [log] [blame]
Matteo Scandolof813b6a2015-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 Scandolo8cbfa642015-11-04 11:31:05 +010011var del = require('del');
Matteo Scandoloba1ed912016-03-29 14:23:20 -070012var sass = require('gulp-sass');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010013
Matteo Scandoloaf977a32015-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 Scandolod3de3902015-11-25 12:08:41 -080021}
22
Matteo Scandolof813b6a2015-11-03 14:32:00 +010023var proxy = httpProxy.createProxyServer({
Matteo Scandolod3de3902015-11-25 12:08:41 -080024 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandolof813b6a2015-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 Scandolo8cbfa642015-11-04 11:31:05 +010041 // reloadDelay: 500,
42 // logLevel: 'debug',
43 // logConnections: true,
Matteo Scandolo807fb012015-11-09 11:25:40 +010044 startPath: '#/',
Matteo Scandolo8cbfa642015-11-04 11:31:05 +010045 snippetOptions: {
46 rule: {
47 match: /<!-- browserSync -->/i
48 }
49 },
Matteo Scandolof813b6a2015-11-03 14:32:00 +010050 server: {
51 baseDir: options.src,
52 routes: {
Matteo Scandolo6328d9b2015-11-05 16:02:11 +010053 '/xosHelpers/src': options.helpers
Matteo Scandolof813b6a2015-11-03 14:32:00 +010054 },
55 middleware: function(req, res, next){
Matteo Scandoloc8b95322015-11-06 09:34:03 +010056 if(
57 req.url.indexOf('/xos/') !== -1 ||
58 req.url.indexOf('/xoslib/') !== -1 ||
Matteo Scandolode76a452016-04-12 09:00:04 -070059 req.url.indexOf('/hpcapi/') !== -1 ||
60 req.url.indexOf('/api/') !== -1
Matteo Scandoloc8b95322015-11-06 09:34:03 +010061 ){
Matteo Scandolod3de3902015-11-25 12:08:41 -080062 if(conf.xoscsrftoken && conf.xossessionid){
63 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
64 req.headers['x-csrftoken'] = conf.xoscsrftoken;
65 }
Matteo Scandolof813b6a2015-11-03 14:32:00 +010066 proxy.web(req, res);
67 }
68 else{
69 next();
70 }
71 }
72 }
73 });
74
Matteo Scandolo93487772015-11-04 15:15:17 +010075 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
76 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
77 browserSync.reload();
78 });
Matteo Scandolo4ea1cdb2015-11-04 11:34:44 +010079 gulp.watch(options.src + '**/*.html', function(){
80 browserSync.reload();
81 });
Matteo Scandoloba1ed912016-03-29 14:23:20 -070082 gulp.watch(options.css + '**/*.css', function(){
83 browserSync.reload();
84 });
85 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
86 browserSync.reload();
87 });
88 });
89
90 // compile sass
91 gulp.task('sass', function () {
92 return gulp.src(`${options.sass}/**/*.scss`)
93 .pipe(sass().on('error', sass.logError))
94 .pipe(gulp.dest(options.css));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010095 });
96
97 // transpile js with sourceMaps
98 gulp.task('babel', function(){
99 return gulp.src(options.scripts + '**/*.js')
100 .pipe(babel({sourceMaps: true}))
101 .pipe(gulp.dest(options.tmp));
102 });
103
104 // inject scripts
Matteo Scandoloc7794962015-11-06 10:35:20 +0100105 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100106 return gulp.src(options.src + 'index.html')
107 .pipe(
108 inject(
109 gulp.src([
110 options.tmp + '**/*.js',
Matteo Scandolo2ef0cd72016-04-12 11:59:29 -0700111 options.helpers + '**/*.js' // todo add Babel here
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100112 ])
113 .pipe(angularFilesort()),
114 {
115 ignorePath: [options.src, '/../../ngXosLib']
116 }
117 )
118 )
119 .pipe(gulp.dest(options.src));
120 });
121
Matteo Scandoloc7794962015-11-06 10:35:20 +0100122 // inject CSS
123 gulp.task('injectCss', function(){
124 return gulp.src(options.src + 'index.html')
125 .pipe(
126 inject(
127 gulp.src(options.src + 'css/*.css'),
128 {
129 ignorePath: [options.src]
130 }
131 )
132 )
133 .pipe(gulp.dest(options.src));
134 });
135
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100136 // inject bower dependencies with wiredep
137 gulp.task('bower', function () {
Matteo Scandolo352b6012015-11-04 15:07:36 +0100138 return gulp.src(options.src + 'index.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100139 .pipe(wiredep({devDependencies: true}))
140 .pipe(gulp.dest(options.src));
141 });
142
Matteo Scandoloab91f432015-11-06 18:25:52 +0100143 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolo8cbfa642015-11-04 11:31:05 +0100144 browserSync.reload();
145 });
146
147 gulp.task('cleanTmp', function(){
148 return del([options.tmp + '**/*']);
149 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100150
151 gulp.task('serve', function() {
152 runSequence(
Matteo Scandoloba1ed912016-03-29 14:23:20 -0700153 'sass',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100154 'bower',
Matteo Scandoloc7794962015-11-06 10:35:20 +0100155 'injectScript',
156 'injectCss',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100157 ['browser']
158 );
159 });
Matteo Scandolod3de3902015-11-25 12:08:41 -0800160};