blob: 3c6a8e39d795a717660ce91b07ae5b1b5e50d232 [file] [log] [blame]
Matteo Scandolo8cc22e42016-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 Scandolob7a86dc2016-04-14 11:46:30 -07006var es = require('event-stream');
Matteo Scandolo8cc22e42016-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 Scandolob7a86dc2016-04-14 11:46:30 -070014var debug = require('gulp-debug');
Matteo Scandolo8cc22e42016-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 Scandolo8cc22e42016-04-12 09:00:04 -070039 // open in browser with sync and proxy to 0.0.0.0
40 gulp.task('browser', function() {
Matteo Scandolo1ba8b302016-04-14 10:53:30 -070041 console.log(options.helpers);
Matteo Scandolo8cc22e42016-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 Scandolo1ba8b302016-04-14 10:53:30 -070055 // '/xosHelpers/src': options.helpers,
56 '/xos/core/xoslib/static/js/vendor': options.helpers
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070057 },
58 middleware: function(req, res, next){
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070059 if(
Matteo Scandolo1ba8b302016-04-14 10:53:30 -070060 // req.url.indexOf('/xos/') !== -1 ||
61 // req.url.indexOf('/xoslib/') !== -1 ||
62 // req.url.indexOf('/hpcapi/') !== -1 ||
Matteo Scandolobd2e5cd2016-04-12 11:59:29 -070063 req.url.indexOf('/api/') !== -1
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070064 ){
65 if(conf.xoscsrftoken && conf.xossessionid){
66 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
67 req.headers['x-csrftoken'] = conf.xoscsrftoken;
68 }
69 proxy.web(req, res);
70 }
71 else{
72 next();
73 }
74 }
75 }
76 });
77
78 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
79 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
80 browserSync.reload();
81 });
82 gulp.watch(options.src + '**/*.html', function(){
83 browserSync.reload();
84 });
85 gulp.watch(options.css + '**/*.css', function(){
86 browserSync.reload();
87 });
Matteo Scandolob7a86dc2016-04-14 11:46:30 -070088
89 gulp.watch(options.helpers + 'ngXosHelpers.js', function(){
90 browserSync.reload();
91 });
92
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070093 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], 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
Matteo Scandolob7a86dc2016-04-14 11:46:30 -0700112 // // inject sourceMap
113 // gulp.task('injectMaps', function(){
114 // return gulp.src(options.src + 'index.html')
115 // .pipe(
116 // inject(
117 // gulp.src([
118 // options.helpersSourceMaps + '**/*.js.map'
119 // ], {read: false}).pipe(debug()),
120 // {
121 // starttag: '<!-- inject:maps -->',
122 // // ignorePath: [options.src, '/../../ngXosLib']
123 // }
124 // )
125 // )
126 // .pipe(gulp.dest(options.src));
127 // });
128
Matteo Scandolo8cc22e42016-04-12 09:00:04 -0700129 // inject scripts
130 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolob7a86dc2016-04-14 11:46:30 -0700131
132 var appScripts = gulp.src([
133 options.tmp + '**/*.js',
134 options.helpers + 'ngXosHelpers.js'
135 ])
136 .pipe(angularFilesort()).pipe(debug());
137
138 var helpersSourceMaps = gulp.src([
139 options.helpersSourceMaps + '**/*.js.map'
140 ]).pipe(debug());
141
Matteo Scandolo8cc22e42016-04-12 09:00:04 -0700142 return gulp.src(options.src + 'index.html')
143 .pipe(
144 inject(
Matteo Scandolob7a86dc2016-04-14 11:46:30 -0700145 es.merge(appScripts, helpersSourceMaps),
Matteo Scandolo8cc22e42016-04-12 09:00:04 -0700146 {
147 ignorePath: [options.src, '/../../ngXosLib']
148 }
149 )
150 )
151 .pipe(gulp.dest(options.src));
152 });
153
154 // inject CSS
155 gulp.task('injectCss', function(){
156 return gulp.src(options.src + 'index.html')
157 .pipe(
158 inject(
159 gulp.src(options.src + 'css/*.css'),
160 {
161 ignorePath: [options.src]
162 }
163 )
164 )
165 .pipe(gulp.dest(options.src));
166 });
167
168 // inject bower dependencies with wiredep
169 gulp.task('bower', function () {
170 return gulp.src(options.src + 'index.html')
171 .pipe(wiredep({devDependencies: true}))
172 .pipe(gulp.dest(options.src));
173 });
174
175 gulp.task('js-watch', ['injectScript'], function(){
176 browserSync.reload();
177 });
178
179 gulp.task('cleanTmp', function(){
180 return del([options.tmp + '**/*']);
181 });
182
183 gulp.task('serve', function() {
184 runSequence(
185 'sass',
186 'bower',
187 'injectScript',
188 'injectCss',
189 ['browser']
190 );
191 });
192};