blob: c0678d9fb6cc23f1feb763243e73cbb71ef7643d [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
Matteo Scandoloe53ee052015-11-03 14:32:00 +010038 gulp.task('browser', function() {
39 browserSync.init({
Matteo Scandolof1072ed2015-11-09 11:25:40 +010040 startPath: '#/',
Matteo Scandoloce954e52015-11-04 11:31:05 +010041 snippetOptions: {
42 rule: {
43 match: /<!-- browserSync -->/i
44 }
45 },
Matteo Scandoloe53ee052015-11-03 14:32:00 +010046 server: {
47 baseDir: options.src,
48 routes: {
Matteo Scandoloa3839e32016-04-28 16:20:53 -070049 '/xos/core/xoslib/static/js/vendor': options.helpers,
50 '/xos/core/static': options.static + '../../static/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010051 },
52 middleware: function(req, res, next){
Matteo Scandolo07760222015-11-06 09:34:03 +010053 if(
Matteo Scandoloa3839e32016-04-28 16:20:53 -070054 // to be removed, deprecated API
55 // req.url.indexOf('/xos/') !== -1 ||
56 // req.url.indexOf('/xoslib/') !== -1 ||
57 // req.url.indexOf('/hpcapi/') !== -1 ||
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070058 req.url.indexOf('/api/') !== -1
Matteo Scandolo07760222015-11-06 09:34:03 +010059 ){
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080060 if(conf.xoscsrftoken && conf.xossessionid){
61 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
62 req.headers['x-csrftoken'] = conf.xoscsrftoken;
63 }
Matteo Scandoloe53ee052015-11-03 14:32:00 +010064 proxy.web(req, res);
65 }
66 else{
67 next();
68 }
69 }
70 }
71 });
72
Matteo Scandolo0a0f5f32015-11-04 15:15:17 +010073 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
74 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
75 browserSync.reload();
76 });
Matteo Scandolo73a3c2b2015-11-04 11:34:44 +010077 gulp.watch(options.src + '**/*.html', function(){
78 browserSync.reload();
79 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070080 gulp.watch(options.css + '**/*.css', function(){
81 browserSync.reload();
82 });
83 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
84 browserSync.reload();
85 });
Matteo Scandoloa3839e32016-04-28 16:20:53 -070086
87 gulp.watch([
88 options.helpers + 'ngXosHelpers.js',
89 options.static + '../../static/xosNgLib.css'
90 ], function(){
91 browserSync.reload();
92 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070093 });
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 Scandoloe53ee052015-11-03 14:32:00 +0100100 });
101
102 // transpile js with sourceMaps
103 gulp.task('babel', function(){
104 return gulp.src(options.scripts + '**/*.js')
105 .pipe(babel({sourceMaps: true}))
106 .pipe(gulp.dest(options.tmp));
107 });
108
109 // inject scripts
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100110 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100111 return gulp.src(options.src + 'index.html')
112 .pipe(
113 inject(
114 gulp.src([
115 options.tmp + '**/*.js',
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700116 options.helpers + 'ngXosHelpers.js'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100117 ])
118 .pipe(angularFilesort()),
119 {
120 ignorePath: [options.src, '/../../ngXosLib']
121 }
122 )
123 )
124 .pipe(gulp.dest(options.src));
125 });
126
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100127 // inject CSS
128 gulp.task('injectCss', function(){
129 return gulp.src(options.src + 'index.html')
130 .pipe(
131 inject(
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700132 gulp.src([
133 options.src + 'css/*.css',
134 options.static + '../../static/xosNgLib.css'
135 ]),
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100136 {
137 ignorePath: [options.src]
138 }
139 )
140 )
141 .pipe(gulp.dest(options.src));
142 });
143
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100144 // inject bower dependencies with wiredep
145 gulp.task('bower', function () {
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100146 return gulp.src(options.src + 'index.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100147 .pipe(wiredep({devDependencies: true}))
148 .pipe(gulp.dest(options.src));
149 });
150
Matteo Scandoloba5e19d2015-11-06 18:25:52 +0100151 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandoloce954e52015-11-04 11:31:05 +0100152 browserSync.reload();
153 });
154
155 gulp.task('cleanTmp', function(){
156 return del([options.tmp + '**/*']);
157 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100158
159 gulp.task('serve', function() {
160 runSequence(
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700161 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100162 'bower',
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100163 'injectScript',
164 'injectCss',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100165 ['browser']
166 );
167 });
Matteo Scandolo9323b1b2015-11-25 12:08:41 -0800168};