blob: 0bc2bd383e8bb4d4df3691124c39e2ce1d929f79 [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 Scandolo97532ef2016-05-17 17:12:03 -070012var sass = require('gulp-sass');
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070013var fs = require('fs');
14var path = require('path');
Matteo Scandolo6d972642015-10-29 15:50:15 +010015
Matteo Scandolo7dea2432016-03-24 15:11:29 -070016const environment = process.env.NODE_ENV;
17
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070018if(!fs.existsSync(path.join(__dirname, `../../../env/${environment || 'default'}.js`))){
19 if(!environment){
20 throw new Error('You should define a default.js config in /views/env folder.');
21 }
22 else{
23 throw new Error(`Since you are loading a custom environment, you should define a ${environment}.js config in /views/env folder.`);
24 }
Matteo Scandolo7dea2432016-03-24 15:11:29 -070025}
26
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070027var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
Matteo Scandolod4f501c2015-10-29 17:24:18 +010029var proxy = httpProxy.createProxyServer({
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070030 target: conf.host
Matteo Scandolod4f501c2015-10-29 17:24:18 +010031});
32
33
34proxy.on('error', function(error, req, res) {
35 res.writeHead(500, {
36 'Content-Type': 'text/plain'
Matteo Scandolo6d972642015-10-29 15:50:15 +010037 });
38
Matteo Scandolod4f501c2015-10-29 17:24:18 +010039 console.error('[Proxy]', error);
40});
41
42module.exports = function(options){
43
Matteo Scandolod4f501c2015-10-29 17:24:18 +010044 gulp.task('browser', function() {
45 browserSync.init({
Matteo Scandolo9a607262015-11-10 17:13:04 +010046 startPath: '#/',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010047 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010052 server: {
53 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010054 routes: {
Matteo Scandolo97532ef2016-05-17 17:12:03 -070055 '/xos/core/xoslib/static/js/vendor': options.helpers,
56 '/xos/core/static': options.static + '../../static/'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010057 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010058 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010059 if(
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070060 req.url.indexOf('/api/') !== -1 ||
61 req.url.indexOf('/hpcapi/') !== -1
Matteo Scandolod44ceb42015-11-06 17:14:28 +010062 ){
Matteo Scandolo7dea2432016-03-24 15:11:29 -070063 if(conf.xoscsrftoken && conf.xossessionid){
64 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
65 req.headers['x-csrftoken'] = conf.xoscsrftoken;
66 }
Matteo Scandolod4f501c2015-10-29 17:24:18 +010067 proxy.web(req, res);
68 }
69 else{
70 next();
71 }
72 }
73 }
74 });
75
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010076 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
77 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
78 browserSync.reload();
79 });
80 gulp.watch(options.src + '**/*.html', function(){
81 browserSync.reload();
82 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -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 gulp.watch([
91 options.helpers + 'ngXosHelpers.js',
92 options.static + '../../static/xosNgLib.css'
93 ], function(){
94 browserSync.reload();
95 });
96 });
97
98 // compile sass
99 gulp.task('sass', function () {
100 return gulp.src(`${options.sass}/**/*.scss`)
101 .pipe(sass().on('error', sass.logError))
102 .pipe(gulp.dest(options.css));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100103 });
104
105 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100106 gulp.task('babel', function(){
107 return gulp.src(options.scripts + '**/*.js')
108 .pipe(babel({sourceMaps: true}))
109 .pipe(gulp.dest(options.tmp));
110 });
111
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100112 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100113 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100114 return gulp.src(options.src + 'index.html')
115 .pipe(
116 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100117 gulp.src([
118 options.tmp + '**/*.js',
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700119 options.helpers + 'ngXosHelpers.js'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100120 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100121 .pipe(angularFilesort()),
122 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100123 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100124 }
125 )
126 )
Matteo Scandolo45778962015-10-29 16:17:59 +0100127 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +0100128 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100129
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100130 // inject CSS
131 gulp.task('injectCss', function(){
132 return gulp.src(options.src + 'index.html')
133 .pipe(
134 inject(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700135 gulp.src([
136 options.src + 'css/*.css',
137 options.static + '../../static/xosNgLib.css'
138 ]),
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100139 {
140 ignorePath: [options.src]
141 }
142 )
143 )
144 .pipe(gulp.dest(options.src));
145 });
146
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100147 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100148 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100149 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100150 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100151 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100152 });
153
Matteo Scandolo7db08432015-11-06 18:49:33 +0100154 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100155 browserSync.reload();
156 });
157
158 gulp.task('cleanTmp', function(){
159 return del([options.tmp + '**/*']);
160 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100161
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100162 gulp.task('serve', function() {
163 runSequence(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700164 'sass',
Matteo Scandolo45778962015-10-29 16:17:59 +0100165 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100166 'injectScript',
167 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100168 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100169 );
170 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700171};