blob: ad7c6817f864801720b60715c46b5267d52c8813 [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,
34 snippetOptions: {
35 rule: {
36 match: /<!-- browserSync -->/i
37 }
38 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010039 server: {
40 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010041 routes: {
42 '/api': options.api,
Matteo Scandolod44ceb42015-11-06 17:14:28 +010043 '/xosHelpers/src': options.helpers
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010044 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010045 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010046 if(
47 req.url.indexOf('/xos/') !== -1 ||
48 req.url.indexOf('/xoslib/') !== -1 ||
49 req.url.indexOf('/hpcapi/') !== -1
50 ){
Matteo Scandolod4f501c2015-10-29 17:24:18 +010051 proxy.web(req, res);
52 }
53 else{
54 next();
55 }
56 }
57 }
58 });
59
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010060 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
61 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
62 browserSync.reload();
63 });
64 gulp.watch(options.src + '**/*.html', function(){
65 browserSync.reload();
66 });
Matteo Scandolod4f501c2015-10-29 17:24:18 +010067 });
68
69 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010070 gulp.task('babel', function(){
71 return gulp.src(options.scripts + '**/*.js')
72 .pipe(babel({sourceMaps: true}))
73 .pipe(gulp.dest(options.tmp));
74 });
75
Matteo Scandolod4f501c2015-10-29 17:24:18 +010076 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +010077 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010078 return gulp.src(options.src + 'index.html')
79 .pipe(
80 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010081 gulp.src([
82 options.tmp + '**/*.js',
83 options.api + '*.js',
84 options.helpers + '**/*.js'
85 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010086 .pipe(angularFilesort()),
87 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010088 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010089 }
90 )
91 )
Matteo Scandolo45778962015-10-29 16:17:59 +010092 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +010093 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010094
Matteo Scandolod44ceb42015-11-06 17:14:28 +010095 // inject CSS
96 gulp.task('injectCss', function(){
97 return gulp.src(options.src + 'index.html')
98 .pipe(
99 inject(
100 gulp.src(options.src + 'css/*.css'),
101 {
102 ignorePath: [options.src]
103 }
104 )
105 )
106 .pipe(gulp.dest(options.src));
107 });
108
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100109 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100110 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100111 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100112 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100113 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100114 });
115
Matteo Scandolo7db08432015-11-06 18:49:33 +0100116 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100117 browserSync.reload();
118 });
119
120 gulp.task('cleanTmp', function(){
121 return del([options.tmp + '**/*']);
122 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100123
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100124 gulp.task('serve', function() {
125 runSequence(
Matteo Scandolo45778962015-10-29 16:17:59 +0100126 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100127 'injectScript',
128 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100129 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100130 );
131 });
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100132};