blob: 36fdded65b9648303506e310a9ea44258d2e1305 [file] [log] [blame]
Matteo Scandoloa8cbc9c2016-06-16 17:03:24 -07001'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');
12var sass = require('gulp-sass');
13var fs = require('fs');
14var path = require('path');
15
16const environment = process.env.NODE_ENV;
17
18if(!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 }
25}
26
27var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
29var proxy = httpProxy.createProxyServer({
30 target: conf.host
31});
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
44 gulp.task('browser', function() {
45 browserSync.init({
46 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
Matteo Scandoloa8cbc9c2016-06-16 17:03:24 -070055 '/xos/core/static': options.static + '../../static/'
56 },
57 middleware: function(req, res, next){
58 if(
Matteo Scandolo195dde92016-07-25 16:43:16 -070059 req.url.indexOf('/api/') !== -1
Matteo Scandoloa8cbc9c2016-06-16 17:03:24 -070060 ){
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 }
67 else{
68 next();
69 }
70 }
71 }
72 });
73
74 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
75 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
76 browserSync.reload();
77 });
78 gulp.watch(options.src + '**/*.html', function(){
79 browserSync.reload();
80 });
81 gulp.watch(options.css + '**/*.css', function(){
82 browserSync.reload();
83 });
84 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
85 browserSync.reload();
86 });
87
88 gulp.watch([
89 options.helpers + 'ngXosHelpers.js',
90 options.static + '../../static/xosNgLib.css'
91 ], function(){
92 browserSync.reload();
93 });
94 });
95
96 // compile sass
97 gulp.task('sass', function () {
98 return gulp.src(`${options.sass}/**/*.scss`)
99 .pipe(sass().on('error', sass.logError))
100 .pipe(gulp.dest(options.css));
101 });
102
103 // transpile js with sourceMaps
104 gulp.task('babel', function(){
105 return gulp.src(options.scripts + '**/*.js')
106 .pipe(babel({sourceMaps: true}))
107 .pipe(gulp.dest(options.tmp));
108 });
109
110 // inject scripts
111 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
112 return gulp.src(options.src + 'index.html')
113 .pipe(
114 inject(
115 gulp.src([
116 options.tmp + '**/*.js',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700117 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandoloa8cbc9c2016-06-16 17:03:24 -0700118 ])
119 .pipe(angularFilesort()),
120 {
121 ignorePath: [options.src, '/../../ngXosLib']
122 }
123 )
124 )
125 .pipe(gulp.dest(options.src));
126 });
127
128 // inject CSS
129 gulp.task('injectCss', function(){
130 return gulp.src(options.src + 'index.html')
131 .pipe(
132 inject(
133 gulp.src([
134 options.src + 'css/*.css',
135 options.static + '../../static/xosNgLib.css'
136 ]),
137 {
138 ignorePath: [options.src]
139 }
140 )
141 )
142 .pipe(gulp.dest(options.src));
143 });
144
145 // inject bower dependencies with wiredep
146 gulp.task('bower', function () {
147 return gulp.src(options.src + 'index.html')
148 .pipe(wiredep({devDependencies: true}))
149 .pipe(gulp.dest(options.src));
150 });
151
152 gulp.task('js-watch', ['injectScript'], function(){
153 browserSync.reload();
154 });
155
156 gulp.task('cleanTmp', function(){
157 return del([options.tmp + '**/*']);
158 });
159
160 gulp.task('serve', function() {
161 runSequence(
162 'sass',
163 'bower',
164 'injectScript',
165 'injectCss',
166 ['browser']
167 );
168 });
169};