blob: 7268b13c13782980334979ec574c081682ea91b6 [file] [log] [blame]
Matteo Scandoloe3de73d2015-12-04 10:14:40 -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 Scandoloa918ea82015-12-14 14:26:20 -080012var debug = require('gulp-debug');
Matteo Scandoloe3de73d2015-12-04 10:14:40 -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
23console.log(conf);
24
25var proxy = httpProxy.createProxyServer({
26 target: conf.host || 'http://0.0.0.0:9999'
27});
28
29
30proxy.on('error', function(error, req, res) {
31 res.writeHead(500, {
32 'Content-Type': 'text/plain'
33 });
34
35 console.error('[Proxy]', error);
36});
37
38module.exports = function(options){
39
40 // open in browser with sync and proxy to 0.0.0.0
41 gulp.task('browser', function() {
42 browserSync.init({
43 // reloadDelay: 500,
44 // logLevel: 'debug',
45 // logConnections: true,
46 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
55 '/api': options.api,
56 '/xosHelpers/src': options.helpers
57 },
58 middleware: function(req, res, next){
59 if(
60 req.url.indexOf('/xos/') !== -1 ||
61 req.url.indexOf('/xoslib/') !== -1 ||
62 req.url.indexOf('/hpcapi/') !== -1
63 ){
64 if(conf.xoscsrftoken && conf.xossessionid){
65 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
66 req.headers['x-csrftoken'] = conf.xoscsrftoken;
67 }
68 proxy.web(req, res);
69 }
70 else{
71 next();
72 }
73 }
74 }
75 });
76
77 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
78
79 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
80 console.log('Bower Package added!');
81 browserSync.reload();
82 });
83 gulp.watch(options.src + '**/*.html', function(){
84 browserSync.reload();
85 });
86 });
87
88 // transpile js with sourceMaps
89 gulp.task('babel', function(){
Matteo Scandoloa918ea82015-12-14 14:26:20 -080090 return gulp.src([options.scripts + '**/*.js'])
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080091 .pipe(babel({sourceMaps: true}))
92 .pipe(gulp.dest(options.tmp));
93 });
94
95 // inject scripts
Matteo Scandoloa918ea82015-12-14 14:26:20 -080096 gulp.task('injectScript', function(){
97 console.log(options.tmp);
98 runSequence(
99 'cleanTmp',
100 'babel',
101 function() {
102 return gulp.src(options.src + 'index.html')
103 .pipe(
104 inject(
105 gulp.src([
106 options.tmp + '**/*.js',
107 options.api + '*.js',
108 options.helpers + '**/*.js'
109 ])
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800110 // .pipe(debug({title: 'unicorn:'}))
Matteo Scandoloa918ea82015-12-14 14:26:20 -0800111 .pipe(angularFilesort()),
112 {
113 ignorePath: [options.src, '/../../ngXosLib']
114 }
115 )
116 )
117 .pipe(gulp.dest(options.src));
118 }
119 );
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800120 });
121
122 // inject CSS
123 gulp.task('injectCss', function(){
124 return gulp.src(options.src + 'index.html')
125 .pipe(
126 inject(
127 gulp.src(options.src + 'css/*.css'),
128 {
129 ignorePath: [options.src]
130 }
131 )
132 )
133 .pipe(gulp.dest(options.src));
134 });
135
136 // inject bower dependencies with wiredep
137 gulp.task('bower', function () {
138 return gulp.src(options.src + 'index.html')
139 .pipe(wiredep({devDependencies: true}))
140 .pipe(gulp.dest(options.src));
141 });
142
143 gulp.task('js-watch', ['injectScript'], function(){
144 browserSync.reload();
145 });
146
147 gulp.task('cleanTmp', function(){
148 return del([options.tmp + '**/*']);
149 });
150
151 gulp.task('serve', function() {
152 runSequence(
153 'bower',
154 'injectScript',
155 'injectCss',
156 ['browser']
157 );
158 });
159};