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