blob: 27791f5adc95534fcbe5fef6418d9bf4e8d224be [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/static': options.static + '../../static/'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010056 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010057 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010058 if(
Matteo Scandolo195dde92016-07-25 16:43:16 -070059 req.url.indexOf('/hpcapi/') !== -1 ||
60 req.url.indexOf('/api/') !== -1
Matteo Scandolod44ceb42015-11-06 17:14:28 +010061 ){
Matteo Scandolo7dea2432016-03-24 15:11:29 -070062 if(conf.xoscsrftoken && conf.xossessionid){
63 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
64 req.headers['x-csrftoken'] = conf.xoscsrftoken;
65 }
Matteo Scandolod4f501c2015-10-29 17:24:18 +010066 proxy.web(req, res);
67 }
68 else{
69 next();
70 }
71 }
72 }
73 });
74
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010075 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
76 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
77 browserSync.reload();
78 });
79 gulp.watch(options.src + '**/*.html', function(){
80 browserSync.reload();
81 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -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 gulp.watch([
90 options.helpers + 'ngXosHelpers.js',
91 options.static + '../../static/xosNgLib.css'
92 ], function(){
93 browserSync.reload();
94 });
95 });
96
97 // compile sass
98 gulp.task('sass', function () {
99 return gulp.src(`${options.sass}/**/*.scss`)
100 .pipe(sass().on('error', sass.logError))
101 .pipe(gulp.dest(options.css));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100102 });
103
104 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100105 gulp.task('babel', function(){
106 return gulp.src(options.scripts + '**/*.js')
107 .pipe(babel({sourceMaps: true}))
108 .pipe(gulp.dest(options.tmp));
109 });
110
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100111 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100112 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100113 return gulp.src(options.src + 'index.html')
114 .pipe(
115 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100116 gulp.src([
117 options.tmp + '**/*.js',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700118 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100119 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100120 .pipe(angularFilesort()),
121 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100122 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100123 }
124 )
125 )
Matteo Scandolo45778962015-10-29 16:17:59 +0100126 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +0100127 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100128
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100129 // inject CSS
130 gulp.task('injectCss', function(){
131 return gulp.src(options.src + 'index.html')
132 .pipe(
133 inject(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700134 gulp.src([
135 options.src + 'css/*.css',
136 options.static + '../../static/xosNgLib.css'
137 ]),
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100138 {
139 ignorePath: [options.src]
140 }
141 )
142 )
143 .pipe(gulp.dest(options.src));
144 });
145
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100146 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100147 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100148 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100149 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100150 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100151 });
152
Matteo Scandolo7db08432015-11-06 18:49:33 +0100153 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100154 browserSync.reload();
155 });
156
157 gulp.task('cleanTmp', function(){
158 return del([options.tmp + '**/*']);
159 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100160
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100161 gulp.task('serve', function() {
162 runSequence(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700163 'sass',
Matteo Scandolo45778962015-10-29 16:17:59 +0100164 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100165 'injectScript',
166 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100167 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100168 );
169 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700170};