blob: 8eab1bf322fd6042c9a8f8e866ef2cb4cee7326c [file] [log] [blame]
Matteo Scandolocb11e0c2015-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 Scandolo2729d082015-10-29 16:13:35 +01006var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
Matteo Scandolo486f2682015-10-29 16:17:59 +01009var wiredep = require('wiredep').stream;
Matteo Scandoloc952aa92015-10-29 17:24:18 +010010var httpProxy = require('http-proxy');
Matteo Scandolo82697c02015-11-04 16:30:43 +010011var del = require('del');
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010012
Matteo Scandoloc952aa92015-10-29 17:24:18 +010013var proxy = httpProxy.createProxyServer({
Matteo Scandolo46485ac2015-11-03 10:48:01 +010014 target: 'http://0.0.0.0:9999'
Matteo Scandoloc952aa92015-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 Scandolocb11e0c2015-10-29 15:50:15 +010021 });
22
Matteo Scandoloc952aa92015-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 Scandolo82697c02015-11-04 16:30:43 +010031 // reloadDelay: 500,
32 // logLevel: 'debug',
33 // logConnections: true,
Matteo Scandolo2d2484f2015-11-10 17:13:04 +010034 startPath: '#/',
Matteo Scandolo82697c02015-11-04 16:30:43 +010035 snippetOptions: {
36 rule: {
37 match: /<!-- browserSync -->/i
38 }
39 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010040 server: {
41 baseDir: options.src,
Matteo Scandolo82697c02015-11-04 16:30:43 +010042 routes: {
43 '/api': options.api,
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010044 '/xosHelpers/src': options.helpers
Matteo Scandolo82697c02015-11-04 16:30:43 +010045 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010046 middleware: function(req, res, next){
Matteo Scandoloc5ee7b52015-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 Scandoloc952aa92015-10-29 17:24:18 +010052 proxy.web(req, res);
53 }
54 else{
55 next();
56 }
57 }
58 }
59 });
60
Matteo Scandolo82697c02015-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 Scandoloc952aa92015-10-29 17:24:18 +010068 });
69
70 // transpile js with sourceMaps
Matteo Scandolo2729d082015-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 Scandoloc952aa92015-10-29 17:24:18 +010077 // inject scripts
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010078 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolo2729d082015-10-29 16:13:35 +010079 return gulp.src(options.src + 'index.html')
80 .pipe(
81 inject(
Matteo Scandolo82697c02015-11-04 16:30:43 +010082 gulp.src([
83 options.tmp + '**/*.js',
84 options.api + '*.js',
85 options.helpers + '**/*.js'
86 ])
Matteo Scandolo2729d082015-10-29 16:13:35 +010087 .pipe(angularFilesort()),
88 {
Matteo Scandolo82697c02015-11-04 16:30:43 +010089 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandolo2729d082015-10-29 16:13:35 +010090 }
91 )
92 )
Matteo Scandolo486f2682015-10-29 16:17:59 +010093 .pipe(gulp.dest(options.src));
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010094 });
Matteo Scandolo2729d082015-10-29 16:13:35 +010095
Matteo Scandoloc5ee7b52015-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 Scandoloc952aa92015-10-29 17:24:18 +0100110 // inject bower dependencies with wiredep
Matteo Scandolo486f2682015-10-29 16:17:59 +0100111 gulp.task('bower', function () {
Matteo Scandolo82697c02015-11-04 16:30:43 +0100112 return gulp.src(options.src + 'index.html')
Matteo Scandoloc952aa92015-10-29 17:24:18 +0100113 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo486f2682015-10-29 16:17:59 +0100114 .pipe(gulp.dest(options.src));
Matteo Scandoloc952aa92015-10-29 17:24:18 +0100115 });
116
Matteo Scandolo6f7f9742015-11-06 18:49:33 +0100117 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolo82697c02015-11-04 16:30:43 +0100118 browserSync.reload();
119 });
120
121 gulp.task('cleanTmp', function(){
122 return del([options.tmp + '**/*']);
123 });
Matteo Scandolo486f2682015-10-29 16:17:59 +0100124
Matteo Scandolo2729d082015-10-29 16:13:35 +0100125 gulp.task('serve', function() {
126 runSequence(
Matteo Scandolo486f2682015-10-29 16:17:59 +0100127 'bower',
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100128 'injectScript',
129 'injectCss',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100130 ['browser']
Matteo Scandolo2729d082015-10-29 16:13:35 +0100131 );
132 });
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100133};