blob: 194bd45851d6f0a6fd92a2a1ff9bd51695fcc261 [file] [log] [blame]
Matteo Scandolobe9b13d2016-01-21 11:21:03 -08001'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');
11var del = require('del');
Matteo Scandolo871f9cd2016-03-29 14:06:13 -070012var sass = require('gulp-sass');
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080013
14const 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
23var proxy = httpProxy.createProxyServer({
24 target: conf.host || 'http://0.0.0.0:9999'
25});
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({
41 // reloadDelay: 500,
42 // logLevel: 'debug',
43 // logConnections: true,
44 startPath: '#/',
45 snippetOptions: {
46 rule: {
47 match: /<!-- browserSync -->/i
48 }
49 },
50 server: {
51 baseDir: options.src,
52 routes: {
53 '/api': options.api,
54 '/xosHelpers/src': options.helpers
55 },
56 middleware: function(req, res, next){
57 if(
58 req.url.indexOf('/xos/') !== -1 ||
59 req.url.indexOf('/xoslib/') !== -1 ||
60 req.url.indexOf('/hpcapi/') !== -1
61 ){
62 if(conf.xoscsrftoken && conf.xossessionid){
63 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
64 req.headers['x-csrftoken'] = conf.xoscsrftoken;
65 }
66 proxy.web(req, res);
67 }
68 else{
69 next();
70 }
71 }
72 }
73 });
74
75 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 Scandolo871f9cd2016-03-29 14:06:13 -070082 gulp.watch(options.css + '**/*.css', function(){
83 browserSync.reload();
84 });
85 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
86 browserSync.reload();
87 });
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080088 });
89
Matteo Scandolo871f9cd2016-03-29 14:06:13 -070090 gulp.task('sass', function () {
91 return gulp.src(`${options.sass}/**/*.scss`)
92 .pipe(sass().on('error', sass.logError))
93 .pipe(gulp.dest(options.css));
94 });
95
96 // gulp.task('sass:watch', function () {
97 // gulp.watch('./sass/**/*.scss', ['sass']);
98 // });
99
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800100 // transpile js with sourceMaps
101 gulp.task('babel', function(){
102 return gulp.src(options.scripts + '**/*.js')
103 .pipe(babel({sourceMaps: true}))
104 .pipe(gulp.dest(options.tmp));
105 });
106
107 // inject scripts
108 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
109 return gulp.src(options.src + 'index.html')
110 .pipe(
111 inject(
112 gulp.src([
113 options.tmp + '**/*.js',
114 options.api + '*.js',
115 options.helpers + '**/*.js'
116 ])
117 .pipe(angularFilesort()),
118 {
119 ignorePath: [options.src, '/../../ngXosLib']
120 }
121 )
122 )
123 .pipe(gulp.dest(options.src));
124 });
125
126 // inject CSS
127 gulp.task('injectCss', function(){
128 return gulp.src(options.src + 'index.html')
129 .pipe(
130 inject(
131 gulp.src(options.src + 'css/*.css'),
132 {
133 ignorePath: [options.src]
134 }
135 )
136 )
137 .pipe(gulp.dest(options.src));
138 });
139
140 // inject bower dependencies with wiredep
141 gulp.task('bower', function () {
142 return gulp.src(options.src + 'index.html')
143 .pipe(wiredep({devDependencies: true}))
144 .pipe(gulp.dest(options.src));
145 });
146
147 gulp.task('js-watch', ['injectScript'], function(){
148 browserSync.reload();
149 });
150
151 gulp.task('cleanTmp', function(){
152 return del([options.tmp + '**/*']);
153 });
154
155 gulp.task('serve', function() {
156 runSequence(
Matteo Scandolo871f9cd2016-03-29 14:06:13 -0700157 'sass',
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800158 'bower',
159 'injectScript',
160 'injectCss',
161 ['browser']
162 );
163 });
164};