blob: 4d64442fbced1e55349f15b46aa1e24dbc968ca8 [file] [log] [blame]
arpiagariud4f6db12016-06-06 15:25:28 -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');
Matteo Scandolo29194952016-06-17 11:57:05 -070013var fs = require('fs');
14var path = require('path');
arpiagariud4f6db12016-06-06 15:25:28 -070015
16const environment = process.env.NODE_ENV;
17
Matteo Scandolo29194952016-06-17 11:57:05 -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 }
arpiagariud4f6db12016-06-06 15:25:28 -070025}
26
Matteo Scandolo29194952016-06-17 11:57:05 -070027var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
arpiagariud4f6db12016-06-06 15:25:28 -070029var proxy = httpProxy.createProxyServer({
Matteo Scandolo29194952016-06-17 11:57:05 -070030 target: conf.host
arpiagariud4f6db12016-06-06 15:25:28 -070031});
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: {
55 '/xos/core/xoslib/static/js/vendor': options.helpers,
56 '/xos/core/static': options.static + '../../static/'
57 },
58 middleware: function(req, res, next){
59 if(
60 // to be removed, deprecated API
61 // req.url.indexOf('/xos/') !== -1 ||
arpiagariu4a872ad2016-06-10 13:13:36 -070062 req.url.indexOf('/xoslib/tenant') !== -1 ||
arpiagariud4f6db12016-06-06 15:25:28 -070063 // req.url.indexOf('/hpcapi/') !== -1 ||
64 req.url.indexOf('/api/') !== -1
65 ){
66 if(conf.xoscsrftoken && conf.xossessionid){
67 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
68 req.headers['x-csrftoken'] = conf.xoscsrftoken;
69 }
70 proxy.web(req, res);
71 }
72 else{
73 next();
74 }
75 }
76 }
77 });
78
79 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
80 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
81 browserSync.reload();
82 });
83 gulp.watch(options.src + '**/*.html', function(){
84 browserSync.reload();
85 });
86 gulp.watch(options.css + '**/*.css', function(){
87 browserSync.reload();
88 });
89 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
90 browserSync.reload();
91 });
92
93 gulp.watch([
94 options.helpers + 'ngXosHelpers.js',
95 options.static + '../../static/xosNgLib.css'
96 ], function(){
97 browserSync.reload();
98 });
99 });
100
101 // compile sass
102 gulp.task('sass', function () {
103 return gulp.src(`${options.sass}/**/*.scss`)
104 .pipe(sass().on('error', sass.logError))
105 .pipe(gulp.dest(options.css));
106 });
107
108 // transpile js with sourceMaps
109 gulp.task('babel', function(){
110 return gulp.src(options.scripts + '**/*.js')
111 .pipe(babel({sourceMaps: true}))
112 .pipe(gulp.dest(options.tmp));
113 });
114
115 // inject scripts
116 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
117 return gulp.src(options.src + 'index.html')
118 .pipe(
119 inject(
120 gulp.src([
121 options.tmp + '**/*.js',
122 options.helpers + 'ngXosHelpers.js'
123 ])
124 .pipe(angularFilesort()),
125 {
126 ignorePath: [options.src, '/../../ngXosLib']
127 }
128 )
129 )
130 .pipe(gulp.dest(options.src));
131 });
132
133 // inject CSS
134 gulp.task('injectCss', function(){
135 return gulp.src(options.src + 'index.html')
136 .pipe(
137 inject(
138 gulp.src([
139 options.src + 'css/*.css',
140 options.static + '../../static/xosNgLib.css'
141 ]),
142 {
143 ignorePath: [options.src]
144 }
145 )
146 )
147 .pipe(gulp.dest(options.src));
148 });
149
150 // inject bower dependencies with wiredep
151 gulp.task('bower', function () {
152 return gulp.src(options.src + 'index.html')
153 .pipe(wiredep({devDependencies: true}))
154 .pipe(gulp.dest(options.src));
155 });
156
157 gulp.task('js-watch', ['injectScript'], function(){
158 browserSync.reload();
159 });
160
161 gulp.task('cleanTmp', function(){
162 return del([options.tmp + '**/*']);
163 });
164
165 gulp.task('serve', function() {
166 runSequence(
167 'sass',
168 'bower',
169 'injectScript',
170 'injectCss',
171 ['browser']
172 );
173 });
174};