blob: 78c1620b65da1367eef61f30ee6a4ccfb1bbaef4 [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 Scandolo6d972642015-10-29 15:50:15 +010013
Matteo Scandolo7dea2432016-03-24 15:11:29 -070014const environment = process.env.NODE_ENV;
15
16if (environment){
17 var conf = require(`../env/${environment}.js`);
18}
19else{
20 var conf = require('../env/default.js')
21}
22
Matteo Scandolod4f501c2015-10-29 17:24:18 +010023var proxy = httpProxy.createProxyServer({
Matteo Scandolo7dea2432016-03-24 15:11:29 -070024 target: conf.host || 'http://0.0.0.0:9999'
Matteo Scandolod4f501c2015-10-29 17:24:18 +010025});
26
27
28proxy.on('error', function(error, req, res) {
29 res.writeHead(500, {
30 'Content-Type': 'text/plain'
Matteo Scandolo6d972642015-10-29 15:50:15 +010031 });
32
Matteo Scandolod4f501c2015-10-29 17:24:18 +010033 console.error('[Proxy]', error);
34});
35
36module.exports = function(options){
37
Matteo Scandolod4f501c2015-10-29 17:24:18 +010038 gulp.task('browser', function() {
39 browserSync.init({
Matteo Scandolo9a607262015-11-10 17:13:04 +010040 startPath: '#/',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010041 snippetOptions: {
42 rule: {
43 match: /<!-- browserSync -->/i
44 }
45 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010046 server: {
47 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010048 routes: {
Matteo Scandolo97532ef2016-05-17 17:12:03 -070049 '/xos/core/xoslib/static/js/vendor': options.helpers,
50 '/xos/core/static': options.static + '../../static/'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010051 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010052 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010053 if(
Matteo Scandolo97532ef2016-05-17 17:12:03 -070054 // to be removed, deprecated API
55 // req.url.indexOf('/xos/') !== -1 ||
56 // req.url.indexOf('/xoslib/') !== -1 ||
57 req.url.indexOf('/hpcapi/') !== -1 ||
58 req.url.indexOf('/api/') !== -1
Matteo Scandolod44ceb42015-11-06 17:14:28 +010059 ){
Matteo Scandolo7dea2432016-03-24 15:11:29 -070060 if(conf.xoscsrftoken && conf.xossessionid){
61 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
62 req.headers['x-csrftoken'] = conf.xoscsrftoken;
63 }
Matteo Scandolod4f501c2015-10-29 17:24:18 +010064 proxy.web(req, res);
65 }
66 else{
67 next();
68 }
69 }
70 }
71 });
72
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010073 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
74 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
75 browserSync.reload();
76 });
77 gulp.watch(options.src + '**/*.html', function(){
78 browserSync.reload();
79 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -070080 gulp.watch(options.css + '**/*.css', function(){
81 browserSync.reload();
82 });
83 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
84 browserSync.reload();
85 });
86
87 gulp.watch([
88 options.helpers + 'ngXosHelpers.js',
89 options.static + '../../static/xosNgLib.css'
90 ], function(){
91 browserSync.reload();
92 });
93 });
94
95 // compile sass
96 gulp.task('sass', function () {
97 return gulp.src(`${options.sass}/**/*.scss`)
98 .pipe(sass().on('error', sass.logError))
99 .pipe(gulp.dest(options.css));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100100 });
101
102 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100103 gulp.task('babel', function(){
104 return gulp.src(options.scripts + '**/*.js')
105 .pipe(babel({sourceMaps: true}))
106 .pipe(gulp.dest(options.tmp));
107 });
108
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100109 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100110 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100111 return gulp.src(options.src + 'index.html')
112 .pipe(
113 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100114 gulp.src([
115 options.tmp + '**/*.js',
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700116 options.helpers + 'ngXosHelpers.js'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100117 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100118 .pipe(angularFilesort()),
119 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100120 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100121 }
122 )
123 )
Matteo Scandolo45778962015-10-29 16:17:59 +0100124 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +0100125 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100126
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100127 // inject CSS
128 gulp.task('injectCss', function(){
129 return gulp.src(options.src + 'index.html')
130 .pipe(
131 inject(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700132 gulp.src([
133 options.src + 'css/*.css',
134 options.static + '../../static/xosNgLib.css'
135 ]),
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100136 {
137 ignorePath: [options.src]
138 }
139 )
140 )
141 .pipe(gulp.dest(options.src));
142 });
143
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100144 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100145 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100146 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100147 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100148 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100149 });
150
Matteo Scandolo7db08432015-11-06 18:49:33 +0100151 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100152 browserSync.reload();
153 });
154
155 gulp.task('cleanTmp', function(){
156 return del([options.tmp + '**/*']);
157 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100158
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100159 gulp.task('serve', function() {
160 runSequence(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700161 'sass',
Matteo Scandolo45778962015-10-29 16:17:59 +0100162 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100163 'injectScript',
164 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100165 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100166 );
167 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700168};