blob: 1e40a34b42662da8739e9607fb209f74171cfe58 [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'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');
Matteo Scandoloce954e52015-11-04 11:31:05 +010011var del = require('del');
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070012var sass = require('gulp-sass');
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070013var fs = require('fs');
14var path = require('path');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010015
Matteo Scandolo7ed6c762015-11-30 17:25:39 -080016const environment = process.env.NODE_ENV;
17
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -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 Scandolo9323b1b2015-11-25 12:08:41 -080025}
26
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070027var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
Matteo Scandoloe53ee052015-11-03 14:32:00 +010029var proxy = httpProxy.createProxyServer({
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070030 target: conf.host
Matteo Scandoloe53ee052015-11-03 14:32:00 +010031});
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 Scandoloe53ee052015-11-03 14:32:00 +010044 gulp.task('browser', function() {
45 browserSync.init({
Matteo Scandolof1072ed2015-11-09 11:25:40 +010046 startPath: '#/',
Matteo Scandoloce954e52015-11-04 11:31:05 +010047 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
Matteo Scandoloe53ee052015-11-03 14:32:00 +010052 server: {
53 baseDir: options.src,
54 routes: {
Matteo Scandoloa3839e32016-04-28 16:20:53 -070055 '/xos/core/xoslib/static/js/vendor': options.helpers,
56 '/xos/core/static': options.static + '../../static/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010057 },
58 middleware: function(req, res, next){
Matteo Scandolo07760222015-11-06 09:34:03 +010059 if(
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070060 req.url.indexOf('/api/') !== -1
Matteo Scandolo07760222015-11-06 09:34:03 +010061 ){
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080062 if(conf.xoscsrftoken && conf.xossessionid){
63 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
64 req.headers['x-csrftoken'] = conf.xoscsrftoken;
65 }
Matteo Scandoloe53ee052015-11-03 14:32:00 +010066 proxy.web(req, res);
67 }
68 else{
69 next();
70 }
71 }
72 }
73 });
74
Matteo Scandolo0a0f5f32015-11-04 15:15:17 +010075 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
76 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
77 browserSync.reload();
78 });
Matteo Scandolo73a3c2b2015-11-04 11:34:44 +010079 gulp.watch(options.src + '**/*.html', function(){
80 browserSync.reload();
81 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070082 gulp.watch(options.css + '**/*.css', function(){
83 browserSync.reload();
84 });
85 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
86 browserSync.reload();
87 });
Matteo Scandoloa3839e32016-04-28 16:20:53 -070088
89 gulp.watch([
90 options.helpers + 'ngXosHelpers.js',
91 options.static + '../../static/xosNgLib.css'
92 ], function(){
93 browserSync.reload();
94 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070095 });
96
97 // compile sass
98 gulp.task('sass', function () {
99 return gulp.src(`${options.sass}/**/*.scss`)
100 .pipe(sass().on('error', sass.logError))
101 .pipe(gulp.dest(options.css));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100102 });
103
104 // transpile js with sourceMaps
105 gulp.task('babel', function(){
106 return gulp.src(options.scripts + '**/*.js')
107 .pipe(babel({sourceMaps: true}))
108 .pipe(gulp.dest(options.tmp));
109 });
110
111 // inject scripts
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100112 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100113 return gulp.src(options.src + 'index.html')
114 .pipe(
115 inject(
116 gulp.src([
117 options.tmp + '**/*.js',
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700118 options.helpers + 'ngXosHelpers.js'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100119 ])
120 .pipe(angularFilesort()),
121 {
122 ignorePath: [options.src, '/../../ngXosLib']
123 }
124 )
125 )
126 .pipe(gulp.dest(options.src));
127 });
128
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100129 // inject CSS
130 gulp.task('injectCss', function(){
131 return gulp.src(options.src + 'index.html')
132 .pipe(
133 inject(
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700134 gulp.src([
135 options.src + 'css/*.css',
136 options.static + '../../static/xosNgLib.css'
137 ]),
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100138 {
139 ignorePath: [options.src]
140 }
141 )
142 )
143 .pipe(gulp.dest(options.src));
144 });
145
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100146 // inject bower dependencies with wiredep
147 gulp.task('bower', function () {
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100148 return gulp.src(options.src + 'index.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100149 .pipe(wiredep({devDependencies: true}))
150 .pipe(gulp.dest(options.src));
151 });
152
Matteo Scandoloba5e19d2015-11-06 18:25:52 +0100153 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandoloce954e52015-11-04 11:31:05 +0100154 browserSync.reload();
155 });
156
157 gulp.task('cleanTmp', function(){
158 return del([options.tmp + '**/*']);
159 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100160
161 gulp.task('serve', function() {
162 runSequence(
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700163 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100164 'bower',
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100165 'injectScript',
166 'injectCss',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100167 ['browser']
168 );
169 });
Matteo Scandolo9323b1b2015-11-25 12:08:41 -0800170};