blob: bc8e9c0df8fa2d4f51abfe730fb97d2f30214b92 [file] [log] [blame]
Matteo Scandolode76a452016-04-12 09:00:04 -07001'use strict';
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var inject = require('gulp-inject');
Matteo Scandolo1dab3e02016-04-14 11:46:30 -07006var es = require('event-stream');
Matteo Scandolode76a452016-04-12 09:00:04 -07007var runSequence = require('run-sequence');
8var angularFilesort = require('gulp-angular-filesort');
9var babel = require('gulp-babel');
10var wiredep = require('wiredep').stream;
11var httpProxy = require('http-proxy');
12var del = require('del');
13var sass = require('gulp-sass');
Matteo Scandolo1dab3e02016-04-14 11:46:30 -070014var debug = require('gulp-debug');
Matteo Scandolode76a452016-04-12 09:00:04 -070015
16const environment = process.env.NODE_ENV;
17
18if (environment){
19 var conf = require(`../env/${environment}.js`);
20}
21else{
22 var conf = require('../env/default.js')
23}
24
25var proxy = httpProxy.createProxyServer({
26 target: conf.host || 'http://0.0.0.0:9999'
27});
28
29
30proxy.on('error', function(error, req, res) {
31 res.writeHead(500, {
32 'Content-Type': 'text/plain'
33 });
34
35 console.error('[Proxy]', error);
36});
37
38module.exports = function(options){
Matteo Scandolode76a452016-04-12 09:00:04 -070039 // open in browser with sync and proxy to 0.0.0.0
40 gulp.task('browser', function() {
Matteo Scandolo21540aa2016-04-14 10:53:30 -070041 console.log(options.helpers);
Matteo Scandolode76a452016-04-12 09:00:04 -070042 browserSync.init({
43 // reloadDelay: 500,
44 // logLevel: 'debug',
45 // logConnections: true,
46 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
Matteo Scandolo21540aa2016-04-14 10:53:30 -070055 // '/xosHelpers/src': options.helpers,
Matteo Scandoloe53ee382016-04-25 10:11:56 -070056 '/xos/core/xoslib/static/js/vendor': options.helpers,
57 '/xos/core/static': options.static + '../../static/'
Matteo Scandolode76a452016-04-12 09:00:04 -070058 },
59 middleware: function(req, res, next){
Matteo Scandolode76a452016-04-12 09:00:04 -070060 if(
Matteo Scandolo21540aa2016-04-14 10:53:30 -070061 // req.url.indexOf('/xos/') !== -1 ||
62 // req.url.indexOf('/xoslib/') !== -1 ||
63 // req.url.indexOf('/hpcapi/') !== -1 ||
Matteo Scandolo2ef0cd72016-04-12 11:59:29 -070064 req.url.indexOf('/api/') !== -1
Matteo Scandolode76a452016-04-12 09:00:04 -070065 ){
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 });
Matteo Scandolo1dab3e02016-04-14 11:46:30 -070089
Matteo Scandoloc88bb9f2016-04-25 10:31:22 -070090 gulp.watch([
91 options.helpers + 'ngXosHelpers.js',
92 options.static + '../../static/xosNgLib.css'
93 ], function(){
Matteo Scandolo1dab3e02016-04-14 11:46:30 -070094 browserSync.reload();
95 });
96
Matteo Scandolode76a452016-04-12 09:00:04 -070097 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
98 browserSync.reload();
99 });
100 });
101
102 // compile sass
103 gulp.task('sass', function () {
104 return gulp.src(`${options.sass}/**/*.scss`)
105 .pipe(sass().on('error', sass.logError))
106 .pipe(gulp.dest(options.css));
107 });
108
109 // transpile js with sourceMaps
110 gulp.task('babel', function(){
111 return gulp.src(options.scripts + '**/*.js')
112 .pipe(babel({sourceMaps: true}))
113 .pipe(gulp.dest(options.tmp));
114 });
115
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700116 // // inject sourceMap
117 // gulp.task('injectMaps', function(){
118 // return gulp.src(options.src + 'index.html')
119 // .pipe(
120 // inject(
121 // gulp.src([
122 // options.helpersSourceMaps + '**/*.js.map'
123 // ], {read: false}).pipe(debug()),
124 // {
125 // starttag: '<!-- inject:maps -->',
126 // // ignorePath: [options.src, '/../../ngXosLib']
127 // }
128 // )
129 // )
130 // .pipe(gulp.dest(options.src));
131 // });
132
Matteo Scandolode76a452016-04-12 09:00:04 -0700133 // inject scripts
134 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700135
136 var appScripts = gulp.src([
137 options.tmp + '**/*.js',
138 options.helpers + 'ngXosHelpers.js'
139 ])
140 .pipe(angularFilesort()).pipe(debug());
141
142 var helpersSourceMaps = gulp.src([
143 options.helpersSourceMaps + '**/*.js.map'
144 ]).pipe(debug());
145
Matteo Scandolode76a452016-04-12 09:00:04 -0700146 return gulp.src(options.src + 'index.html')
147 .pipe(
148 inject(
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700149 es.merge(appScripts, helpersSourceMaps),
Matteo Scandolode76a452016-04-12 09:00:04 -0700150 {
151 ignorePath: [options.src, '/../../ngXosLib']
152 }
153 )
154 )
155 .pipe(gulp.dest(options.src));
156 });
157
158 // inject CSS
159 gulp.task('injectCss', function(){
160 return gulp.src(options.src + 'index.html')
161 .pipe(
162 inject(
Matteo Scandoloe53ee382016-04-25 10:11:56 -0700163 gulp.src([
164 options.src + 'css/*.css',
165 options.static + '../../static/xosNgLib.css'
166 ]),
Matteo Scandolode76a452016-04-12 09:00:04 -0700167 {
168 ignorePath: [options.src]
169 }
170 )
171 )
172 .pipe(gulp.dest(options.src));
173 });
174
175 // inject bower dependencies with wiredep
176 gulp.task('bower', function () {
177 return gulp.src(options.src + 'index.html')
178 .pipe(wiredep({devDependencies: true}))
179 .pipe(gulp.dest(options.src));
180 });
181
182 gulp.task('js-watch', ['injectScript'], function(){
183 browserSync.reload();
184 });
185
186 gulp.task('cleanTmp', function(){
187 return del([options.tmp + '**/*']);
188 });
189
190 gulp.task('serve', function() {
191 runSequence(
192 'sass',
193 'bower',
194 'injectScript',
195 'injectCss',
196 ['browser']
197 );
198 });
199};