blob: 907dd19bcb1113873f0c4992712e25e399da16f0 [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,
34 snippetOptions: {
35 rule: {
36 match: /<!-- browserSync -->/i
37 }
38 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010039 server: {
40 baseDir: options.src,
Matteo Scandolo82697c02015-11-04 16:30:43 +010041 routes: {
42 '/api': options.api,
43 '/xosHelpers': options.helpers
44 },
Matteo Scandoloc952aa92015-10-29 17:24:18 +010045 middleware: function(req, res, next){
Matteo Scandoloc952aa92015-10-29 17:24:18 +010046 if(req.url.indexOf('no_hyperlinks') !== -1){
47 proxy.web(req, res);
48 }
49 else{
50 next();
51 }
52 }
53 }
54 });
55
Matteo Scandolo82697c02015-11-04 16:30:43 +010056 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
57 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
58 browserSync.reload();
59 });
60 gulp.watch(options.src + '**/*.html', function(){
61 browserSync.reload();
62 });
Matteo Scandoloc952aa92015-10-29 17:24:18 +010063 });
64
65 // transpile js with sourceMaps
Matteo Scandolo2729d082015-10-29 16:13:35 +010066 gulp.task('babel', function(){
67 return gulp.src(options.scripts + '**/*.js')
68 .pipe(babel({sourceMaps: true}))
69 .pipe(gulp.dest(options.tmp));
70 });
71
Matteo Scandoloc952aa92015-10-29 17:24:18 +010072 // inject scripts
Matteo Scandolo82697c02015-11-04 16:30:43 +010073 gulp.task('inject', ['cleanTmp', 'babel'],function(){
Matteo Scandolo2729d082015-10-29 16:13:35 +010074 return gulp.src(options.src + 'index.html')
75 .pipe(
76 inject(
Matteo Scandolo82697c02015-11-04 16:30:43 +010077 gulp.src([
78 options.tmp + '**/*.js',
79 options.api + '*.js',
80 options.helpers + '**/*.js'
81 ])
Matteo Scandolo2729d082015-10-29 16:13:35 +010082 .pipe(angularFilesort()),
83 {
Matteo Scandolo82697c02015-11-04 16:30:43 +010084 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandolo2729d082015-10-29 16:13:35 +010085 }
86 )
87 )
Matteo Scandolo486f2682015-10-29 16:17:59 +010088 .pipe(gulp.dest(options.src));
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010089 });
Matteo Scandolo2729d082015-10-29 16:13:35 +010090
Matteo Scandoloc952aa92015-10-29 17:24:18 +010091 // inject bower dependencies with wiredep
Matteo Scandolo486f2682015-10-29 16:17:59 +010092 gulp.task('bower', function () {
Matteo Scandolo82697c02015-11-04 16:30:43 +010093 return gulp.src(options.src + 'index.html')
Matteo Scandoloc952aa92015-10-29 17:24:18 +010094 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo486f2682015-10-29 16:17:59 +010095 .pipe(gulp.dest(options.src));
Matteo Scandoloc952aa92015-10-29 17:24:18 +010096 });
97
Matteo Scandolo82697c02015-11-04 16:30:43 +010098 gulp.task('js-watch', ['inject'], function(){
99 browserSync.reload();
100 });
101
102 gulp.task('cleanTmp', function(){
103 return del([options.tmp + '**/*']);
104 });
Matteo Scandolo486f2682015-10-29 16:17:59 +0100105
Matteo Scandolo2729d082015-10-29 16:13:35 +0100106 gulp.task('serve', function() {
107 runSequence(
Matteo Scandolo486f2682015-10-29 16:17:59 +0100108 'bower',
Matteo Scandolo2729d082015-10-29 16:13:35 +0100109 'inject',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100110 ['browser']
Matteo Scandolo2729d082015-10-29 16:13:35 +0100111 );
112 });
Matteo Scandolocb11e0c2015-10-29 15:50:15 +0100113}