blob: 0ffef8941f81e705fae89dde8930974fb27d7a91 [file] [log] [blame]
Matteo Scandolodf35ca92016-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 Scandolo1778b742016-04-22 09:47:50 -070012var sass = require('gulp-sass');
Matteo Scandolo195dde92016-07-25 16:43:16 -070013var fs = require('fs');
14var path = require('path');
Matteo Scandolodf35ca92016-02-25 09:19:41 -080015
16const environment = process.env.NODE_ENV;
17
Matteo Scandolo195dde92016-07-25 16:43:16 -070018if(!fs.existsSync(path.join(__dirname, `../../../env/${environment || 'default'}.js`))){
19 if(!environment){
20 throw new Error('You should define a default.js config in /views/env folder.');
21 }
22 else{
23 throw new Error(`Since you are loading a custom environment, you should define a ${environment}.js config in /views/env folder.`);
24 }
Matteo Scandolodf35ca92016-02-25 09:19:41 -080025}
26
Matteo Scandolo195dde92016-07-25 16:43:16 -070027var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
Matteo Scandolodf35ca92016-02-25 09:19:41 -080029var proxy = httpProxy.createProxyServer({
Matteo Scandolo195dde92016-07-25 16:43:16 -070030 target: conf.host
Matteo Scandolodf35ca92016-02-25 09:19:41 -080031});
32
33
34proxy.on('error', function(error, req, res) {
35 res.writeHead(500, {
36 'Content-Type': 'text/plain'
37 });
38
39 console.error('[Proxy]', error);
40});
41
42module.exports = function(options){
43
Matteo Scandolodf35ca92016-02-25 09:19:41 -080044 gulp.task('browser', function() {
45 browserSync.init({
Matteo Scandolodf35ca92016-02-25 09:19:41 -080046 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
Matteo Scandolo195dde92016-07-25 16:43:16 -070055 '/xos/core/static': options.static + '../../static/'
Matteo Scandolodf35ca92016-02-25 09:19:41 -080056 },
57 middleware: function(req, res, next){
58 if(
Matteo Scandolo1778b742016-04-22 09:47:50 -070059 req.url.indexOf('/api/') !== -1
Matteo Scandolodf35ca92016-02-25 09:19:41 -080060 ){
61 if(conf.xoscsrftoken && conf.xossessionid){
62 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
63 req.headers['x-csrftoken'] = conf.xoscsrftoken;
64 }
65 proxy.web(req, res);
66 }
Matteo Scandolo1778b742016-04-22 09:47:50 -070067 else if(req.url.indexOf('/videoLocal.txt') !== -1){
68 let a = (Math.random() * 10).toString();
69 res.end(a)
Matteo Scandolo97646d62016-03-07 14:14:00 -080070 }
Matteo Scandolodf35ca92016-02-25 09:19:41 -080071 else{
72 next();
73 }
74 }
75 }
76 });
77
78 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
79 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
80 browserSync.reload();
81 });
82 gulp.watch(options.src + '**/*.html', function(){
83 browserSync.reload();
84 });
Matteo Scandolo1778b742016-04-22 09:47:50 -070085 gulp.watch(options.css + '**/*.css', function(){
86 browserSync.reload();
87 });
88 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
89 browserSync.reload();
90 });
Matteo Scandolo195dde92016-07-25 16:43:16 -070091
92 gulp.watch([
93 options.helpers + 'ngXosHelpers.js',
94 options.static + '../../static/xosNgLib.css'
95 ], function(){
96 browserSync.reload();
97 });
Matteo Scandolo1778b742016-04-22 09:47:50 -070098 });
99
100 // compile sass
101 gulp.task('sass', function () {
102 return gulp.src(`${options.sass}/**/*.scss`)
103 .pipe(sass().on('error', sass.logError))
104 .pipe(gulp.dest(options.css));
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800105 });
106
107 // transpile js with sourceMaps
108 gulp.task('babel', function(){
109 return gulp.src(options.scripts + '**/*.js')
110 .pipe(babel({sourceMaps: true}))
111 .pipe(gulp.dest(options.tmp));
112 });
113
114 // inject scripts
115 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
116 return gulp.src(options.src + 'index.html')
117 .pipe(
118 inject(
119 gulp.src([
120 options.tmp + '**/*.js',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700121 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800122 ])
123 .pipe(angularFilesort()),
124 {
125 ignorePath: [options.src, '/../../ngXosLib']
126 }
127 )
128 )
129 .pipe(gulp.dest(options.src));
130 });
131
132 // inject CSS
133 gulp.task('injectCss', function(){
134 return gulp.src(options.src + 'index.html')
135 .pipe(
136 inject(
Matteo Scandolo195dde92016-07-25 16:43:16 -0700137 gulp.src([
138 options.src + 'css/*.css',
139 options.static + '../../static/xosNgLib.css'
140 ]),
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800141 {
142 ignorePath: [options.src]
143 }
144 )
145 )
146 .pipe(gulp.dest(options.src));
147 });
148
149 // inject bower dependencies with wiredep
150 gulp.task('bower', function () {
151 return gulp.src(options.src + 'index.html')
152 .pipe(wiredep({devDependencies: true}))
153 .pipe(gulp.dest(options.src));
154 });
155
156 gulp.task('js-watch', ['injectScript'], function(){
157 browserSync.reload();
158 });
159
160 gulp.task('cleanTmp', function(){
161 return del([options.tmp + '**/*']);
162 });
163
164 gulp.task('serve', function() {
165 runSequence(
Matteo Scandolo1778b742016-04-22 09:47:50 -0700166 'sass',
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800167 'bower',
168 'injectScript',
169 'injectCss',
170 ['browser']
171 );
172 });
173};