blob: c748eb6c067677a642033b3854ecc2df43bbe8b3 [file] [log] [blame]
Matteo Scandolo18347282016-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: {
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 req.url.indexOf('/api/') !== -1,
61 req.url.indexOf('/xoslib/hpcview') !== -1
62 ){
63 if(conf.xoscsrftoken && conf.xossessionid){
64 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
65 req.headers['x-csrftoken'] = conf.xoscsrftoken;
66 }
67 proxy.web(req, res);
68 }
69 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 });
83 gulp.watch(options.css + '**/*.css', function(){
84 browserSync.reload();
85 });
86 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
87 browserSync.reload();
88 });
89
90 gulp.watch([
91 options.helpers + 'ngXosHelpers.js',
92 options.static + '../../static/xosNgLib.css'
93 ], function(){
94 browserSync.reload();
95 });
96 });
97
98 // compile sass
99 gulp.task('sass', function () {
100 return gulp.src(`${options.sass}/**/*.scss`)
101 .pipe(sass().on('error', sass.logError))
102 .pipe(gulp.dest(options.css));
103 });
104
105 // transpile js with sourceMaps
106 gulp.task('babel', function(){
107 return gulp.src(options.scripts + '**/*.js')
108 .pipe(babel({sourceMaps: true}))
109 .pipe(gulp.dest(options.tmp));
110 });
111
112 // inject scripts
113 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
114 return gulp.src(options.src + 'index.html')
115 .pipe(
116 inject(
117 gulp.src([
118 options.tmp + '**/*.js',
119 options.helpers + 'ngXosHelpers.js'
120 ])
121 .pipe(angularFilesort()),
122 {
123 ignorePath: [options.src, '/../../ngXosLib']
124 }
125 )
126 )
127 .pipe(gulp.dest(options.src));
128 });
129
130 // inject CSS
131 gulp.task('injectCss', function(){
132 return gulp.src(options.src + 'index.html')
133 .pipe(
134 inject(
135 gulp.src([
136 options.src + 'css/*.css',
137 options.static + '../../static/xosNgLib.css'
138 ]),
139 {
140 ignorePath: [options.src]
141 }
142 )
143 )
144 .pipe(gulp.dest(options.src));
145 });
146
147 // inject bower dependencies with wiredep
148 gulp.task('bower', function () {
149 return gulp.src(options.src + 'index.html')
150 .pipe(wiredep({devDependencies: true}))
151 .pipe(gulp.dest(options.src));
152 });
153
154 gulp.task('js-watch', ['injectScript'], function(){
155 browserSync.reload();
156 });
157
158 gulp.task('cleanTmp', function(){
159 return del([options.tmp + '**/*']);
160 });
161
162 gulp.task('serve', function() {
163 runSequence(
164 'sass',
165 'bower',
166 'injectScript',
167 'injectCss',
168 ['browser']
169 );
170 });
171};