blob: 167a5a9fb4f06746ccbe436b0040e85a17aa3dbe [file] [log] [blame]
Matteo Scandolo74802522016-02-25 09:19:41 -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 Scandolof6ba6662016-04-22 09:47:50 -070012var sass = require('gulp-sass');
Matteo Scandolo74802522016-02-25 09:19:41 -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: {
Matteo Scandolof6ba6662016-04-22 09:47:50 -070053 '/xos/core/xoslib/static/js/vendor': options.helpers
Matteo Scandolo74802522016-02-25 09:19:41 -080054 },
55 middleware: function(req, res, next){
56 if(
Matteo Scandolof6ba6662016-04-22 09:47:50 -070057 req.url.indexOf('/api/') !== -1
Matteo Scandolo74802522016-02-25 09:19:41 -080058 ){
59 if(conf.xoscsrftoken && conf.xossessionid){
60 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
61 req.headers['x-csrftoken'] = conf.xoscsrftoken;
62 }
63 proxy.web(req, res);
64 }
Matteo Scandolof6ba6662016-04-22 09:47:50 -070065 else if(req.url.indexOf('/videoLocal.txt') !== -1){
66 let a = (Math.random() * 10).toString();
67 res.end(a)
Matteo Scandolof3866162016-03-07 14:14:00 -080068 }
Matteo Scandolo74802522016-02-25 09:19:41 -080069 else{
70 next();
71 }
72 }
73 }
74 });
75
76 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
77 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
78 browserSync.reload();
79 });
80 gulp.watch(options.src + '**/*.html', function(){
81 browserSync.reload();
82 });
Matteo Scandolof6ba6662016-04-22 09:47:50 -070083 gulp.watch(options.css + '**/*.css', function(){
84 browserSync.reload();
85 });
86 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
87 browserSync.reload();
88 });
89 });
90
91 // compile sass
92 gulp.task('sass', function () {
93 return gulp.src(`${options.sass}/**/*.scss`)
94 .pipe(sass().on('error', sass.logError))
95 .pipe(gulp.dest(options.css));
Matteo Scandolo74802522016-02-25 09:19:41 -080096 });
97
98 // transpile js with sourceMaps
99 gulp.task('babel', function(){
100 return gulp.src(options.scripts + '**/*.js')
101 .pipe(babel({sourceMaps: true}))
102 .pipe(gulp.dest(options.tmp));
103 });
104
105 // inject scripts
106 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
107 return gulp.src(options.src + 'index.html')
108 .pipe(
109 inject(
110 gulp.src([
111 options.tmp + '**/*.js',
Matteo Scandolof6ba6662016-04-22 09:47:50 -0700112 options.helpers + 'ngXosHelpers.js'
Matteo Scandolo74802522016-02-25 09:19:41 -0800113 ])
114 .pipe(angularFilesort()),
115 {
116 ignorePath: [options.src, '/../../ngXosLib']
117 }
118 )
119 )
120 .pipe(gulp.dest(options.src));
121 });
122
123 // inject CSS
124 gulp.task('injectCss', function(){
125 return gulp.src(options.src + 'index.html')
126 .pipe(
127 inject(
128 gulp.src(options.src + 'css/*.css'),
129 {
130 ignorePath: [options.src]
131 }
132 )
133 )
134 .pipe(gulp.dest(options.src));
135 });
136
137 // inject bower dependencies with wiredep
138 gulp.task('bower', function () {
139 return gulp.src(options.src + 'index.html')
140 .pipe(wiredep({devDependencies: true}))
141 .pipe(gulp.dest(options.src));
142 });
143
144 gulp.task('js-watch', ['injectScript'], function(){
145 browserSync.reload();
146 });
147
148 gulp.task('cleanTmp', function(){
149 return del([options.tmp + '**/*']);
150 });
151
152 gulp.task('serve', function() {
153 runSequence(
Matteo Scandolof6ba6662016-04-22 09:47:50 -0700154 'sass',
Matteo Scandolo74802522016-02-25 09:19:41 -0800155 'bower',
156 'injectScript',
157 'injectCss',
158 ['browser']
159 );
160 });
161};