blob: 8eab1bf322fd6042c9a8f8e866ef2cb4cee7326c [file] [log] [blame]
Matteo Scandolo6d972642015-10-29 15:50:15 +01001'use strict';
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var inject = require('gulp-inject');
Matteo Scandoloc2d0f532015-10-29 16:13:35 +01006var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
Matteo Scandolo45778962015-10-29 16:17:59 +01009var wiredep = require('wiredep').stream;
Matteo Scandolod4f501c2015-10-29 17:24:18 +010010var httpProxy = require('http-proxy');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010011var del = require('del');
Matteo Scandolo6d972642015-10-29 15:50:15 +010012
Matteo Scandolod4f501c2015-10-29 17:24:18 +010013var proxy = httpProxy.createProxyServer({
Matteo Scandoloa0449cd2015-11-03 10:48:01 +010014 target: 'http://0.0.0.0:9999'
Matteo Scandolod4f501c2015-10-29 17:24:18 +010015});
16
17
18proxy.on('error', function(error, req, res) {
19 res.writeHead(500, {
20 'Content-Type': 'text/plain'
Matteo Scandolo6d972642015-10-29 15:50:15 +010021 });
22
Matteo Scandolod4f501c2015-10-29 17:24:18 +010023 console.error('[Proxy]', error);
24});
25
26module.exports = function(options){
27
28 // open in browser with sync and proxy to 0.0.0.0
29 gulp.task('browser', function() {
30 browserSync.init({
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010031 // reloadDelay: 500,
32 // logLevel: 'debug',
33 // logConnections: true,
Matteo Scandolo9a607262015-11-10 17:13:04 +010034 startPath: '#/',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010035 snippetOptions: {
36 rule: {
37 match: /<!-- browserSync -->/i
38 }
39 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010040 server: {
41 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010042 routes: {
43 '/api': options.api,
Matteo Scandolod44ceb42015-11-06 17:14:28 +010044 '/xosHelpers/src': options.helpers
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010045 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010046 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010047 if(
48 req.url.indexOf('/xos/') !== -1 ||
49 req.url.indexOf('/xoslib/') !== -1 ||
50 req.url.indexOf('/hpcapi/') !== -1
51 ){
Matteo Scandolod4f501c2015-10-29 17:24:18 +010052 proxy.web(req, res);
53 }
54 else{
55 next();
56 }
57 }
58 }
59 });
60
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010061 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
62 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
63 browserSync.reload();
64 });
65 gulp.watch(options.src + '**/*.html', function(){
66 browserSync.reload();
67 });
Matteo Scandolod4f501c2015-10-29 17:24:18 +010068 });
69
70 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010071 gulp.task('babel', function(){
72 return gulp.src(options.scripts + '**/*.js')
73 .pipe(babel({sourceMaps: true}))
74 .pipe(gulp.dest(options.tmp));
75 });
76
Matteo Scandolod4f501c2015-10-29 17:24:18 +010077 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +010078 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010079 return gulp.src(options.src + 'index.html')
80 .pipe(
81 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010082 gulp.src([
83 options.tmp + '**/*.js',
84 options.api + '*.js',
85 options.helpers + '**/*.js'
86 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010087 .pipe(angularFilesort()),
88 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010089 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010090 }
91 )
92 )
Matteo Scandolo45778962015-10-29 16:17:59 +010093 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +010094 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010095
Matteo Scandolod44ceb42015-11-06 17:14:28 +010096 // inject CSS
97 gulp.task('injectCss', function(){
98 return gulp.src(options.src + 'index.html')
99 .pipe(
100 inject(
101 gulp.src(options.src + 'css/*.css'),
102 {
103 ignorePath: [options.src]
104 }
105 )
106 )
107 .pipe(gulp.dest(options.src));
108 });
109
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100110 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100111 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100112 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100113 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100114 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100115 });
116
Matteo Scandolo7db08432015-11-06 18:49:33 +0100117 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100118 browserSync.reload();
119 });
120
121 gulp.task('cleanTmp', function(){
122 return del([options.tmp + '**/*']);
123 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100124
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100125 gulp.task('serve', function() {
126 runSequence(
Matteo Scandolo45778962015-10-29 16:17:59 +0100127 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100128 'injectScript',
129 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100130 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100131 );
132 });
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100133};