blob: c0678d9fb6cc23f1feb763243e73cbb71ef7643d [file] [log] [blame]
Matteo Scandoloe3de73d2015-12-04 10:14:40 -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 Scandolod24b8712016-05-18 14:06:45 -070012var sass = require('gulp-sass');
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080013
14const environment = process.env.NODE_ENV;
15
16if (environment){
17 var conf = require(`../env/${environment}.js`);
18}
19else{
20 var conf = require('../env/default.js')
21}
22
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080023var proxy = httpProxy.createProxyServer({
24 target: conf.host || 'http://0.0.0.0:9999'
25});
26
27
28proxy.on('error', function(error, req, res) {
29 res.writeHead(500, {
30 'Content-Type': 'text/plain'
31 });
32
33 console.error('[Proxy]', error);
34});
35
36module.exports = function(options){
37
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080038 gulp.task('browser', function() {
39 browserSync.init({
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080040 startPath: '#/',
41 snippetOptions: {
42 rule: {
43 match: /<!-- browserSync -->/i
44 }
45 },
46 server: {
47 baseDir: options.src,
48 routes: {
Matteo Scandolod24b8712016-05-18 14:06:45 -070049 '/xos/core/xoslib/static/js/vendor': options.helpers,
50 '/xos/core/static': options.static + '../../static/'
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080051 },
52 middleware: function(req, res, next){
53 if(
Matteo Scandolod24b8712016-05-18 14:06:45 -070054 // to be removed, deprecated API
55 // req.url.indexOf('/xos/') !== -1 ||
56 // req.url.indexOf('/xoslib/') !== -1 ||
57 // req.url.indexOf('/hpcapi/') !== -1 ||
58 req.url.indexOf('/api/') !== -1
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080059 ){
60 if(conf.xoscsrftoken && conf.xossessionid){
61 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
62 req.headers['x-csrftoken'] = conf.xoscsrftoken;
63 }
64 proxy.web(req, res);
65 }
66 else{
67 next();
68 }
69 }
70 }
71 });
72
73 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080074 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080075 browserSync.reload();
76 });
77 gulp.watch(options.src + '**/*.html', function(){
78 browserSync.reload();
79 });
Matteo Scandolod24b8712016-05-18 14:06:45 -070080 gulp.watch(options.css + '**/*.css', function(){
81 browserSync.reload();
82 });
83 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
84 browserSync.reload();
85 });
86
87 gulp.watch([
88 options.helpers + 'ngXosHelpers.js',
89 options.static + '../../static/xosNgLib.css'
90 ], function(){
91 browserSync.reload();
92 });
93 });
94
95 // compile sass
96 gulp.task('sass', function () {
97 return gulp.src(`${options.sass}/**/*.scss`)
98 .pipe(sass().on('error', sass.logError))
99 .pipe(gulp.dest(options.css));
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800100 });
101
102 // transpile js with sourceMaps
103 gulp.task('babel', function(){
Matteo Scandolod24b8712016-05-18 14:06:45 -0700104 return gulp.src(options.scripts + '**/*.js')
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800105 .pipe(babel({sourceMaps: true}))
106 .pipe(gulp.dest(options.tmp));
107 });
108
109 // inject scripts
Matteo Scandolod24b8712016-05-18 14:06:45 -0700110 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
111 return gulp.src(options.src + 'index.html')
112 .pipe(
113 inject(
114 gulp.src([
115 options.tmp + '**/*.js',
116 options.helpers + 'ngXosHelpers.js'
117 ])
118 .pipe(angularFilesort()),
119 {
120 ignorePath: [options.src, '/../../ngXosLib']
121 }
122 )
123 )
124 .pipe(gulp.dest(options.src));
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800125 });
126
127 // inject CSS
128 gulp.task('injectCss', function(){
129 return gulp.src(options.src + 'index.html')
130 .pipe(
131 inject(
Matteo Scandolod24b8712016-05-18 14:06:45 -0700132 gulp.src([
133 options.src + 'css/*.css',
134 options.static + '../../static/xosNgLib.css'
135 ]),
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800136 {
137 ignorePath: [options.src]
138 }
139 )
140 )
141 .pipe(gulp.dest(options.src));
142 });
143
144 // inject bower dependencies with wiredep
145 gulp.task('bower', function () {
146 return gulp.src(options.src + 'index.html')
147 .pipe(wiredep({devDependencies: true}))
148 .pipe(gulp.dest(options.src));
149 });
150
151 gulp.task('js-watch', ['injectScript'], function(){
152 browserSync.reload();
153 });
154
155 gulp.task('cleanTmp', function(){
156 return del([options.tmp + '**/*']);
157 });
158
159 gulp.task('serve', function() {
160 runSequence(
Matteo Scandolod24b8712016-05-18 14:06:45 -0700161 'sass',
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800162 'bower',
163 'injectScript',
164 'injectCss',
165 ['browser']
166 );
167 });
168};