Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var gulp = require('gulp'); |
| 4 | var browserSync = require('browser-sync').create(); |
| 5 | var inject = require('gulp-inject'); |
| 6 | var runSequence = require('run-sequence'); |
| 7 | var angularFilesort = require('gulp-angular-filesort'); |
| 8 | var babel = require('gulp-babel'); |
| 9 | var wiredep = require('wiredep').stream; |
| 10 | var httpProxy = require('http-proxy'); |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 11 | var del = require('del'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 12 | |
| 13 | var proxy = httpProxy.createProxyServer({ |
| 14 | target: 'http://0.0.0.0:9999' |
| 15 | }); |
| 16 | |
| 17 | |
| 18 | proxy.on('error', function(error, req, res) { |
| 19 | res.writeHead(500, { |
| 20 | 'Content-Type': 'text/plain' |
| 21 | }); |
| 22 | |
| 23 | console.error('[Proxy]', error); |
| 24 | }); |
| 25 | |
| 26 | module.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 Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 31 | // reloadDelay: 500, |
| 32 | // logLevel: 'debug', |
| 33 | // logConnections: true, |
| 34 | snippetOptions: { |
| 35 | rule: { |
| 36 | match: /<!-- browserSync -->/i |
| 37 | } |
| 38 | }, |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 39 | server: { |
| 40 | baseDir: options.src, |
| 41 | routes: { |
| 42 | '/api': options.api |
| 43 | }, |
| 44 | middleware: function(req, res, next){ |
| 45 | if(req.url.indexOf('no_hyperlinks') !== -1){ |
| 46 | proxy.web(req, res); |
| 47 | } |
| 48 | else{ |
| 49 | next(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | }); |
| 54 | |
Matteo Scandolo | 4ea1cdb | 2015-11-04 11:34:44 +0100 | [diff] [blame^] | 55 | gulp.watch(options.src + '**/*.js', ['js-watch']); |
| 56 | gulp.watch(options.src + '**/*.html', function(){ |
| 57 | browserSync.reload(); |
| 58 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 59 | }); |
| 60 | |
| 61 | // transpile js with sourceMaps |
| 62 | gulp.task('babel', function(){ |
| 63 | return gulp.src(options.scripts + '**/*.js') |
| 64 | .pipe(babel({sourceMaps: true})) |
| 65 | .pipe(gulp.dest(options.tmp)); |
| 66 | }); |
| 67 | |
| 68 | // inject scripts |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 69 | gulp.task('inject', ['cleanTmp', 'babel'],function(){ |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 70 | return gulp.src(options.src + 'index.html') |
| 71 | .pipe( |
| 72 | inject( |
| 73 | gulp.src([ |
| 74 | options.tmp + '**/*.js', |
| 75 | options.api + '*.js' |
| 76 | ]) |
| 77 | .pipe(angularFilesort()), |
| 78 | { |
| 79 | ignorePath: [options.src, '/../../ngXosLib'] |
| 80 | } |
| 81 | ) |
| 82 | ) |
| 83 | .pipe(gulp.dest(options.src)); |
| 84 | }); |
| 85 | |
| 86 | // inject bower dependencies with wiredep |
| 87 | gulp.task('bower', function () { |
| 88 | gulp.src(options.src + 'index.html') |
| 89 | .pipe(wiredep({devDependencies: true})) |
| 90 | .pipe(gulp.dest(options.src)); |
| 91 | }); |
| 92 | |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 93 | gulp.task('js-watch', ['inject'], function(){ |
| 94 | browserSync.reload(); |
| 95 | }); |
| 96 | |
| 97 | gulp.task('cleanTmp', function(){ |
| 98 | return del([options.tmp + '**/*']); |
| 99 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 100 | |
| 101 | gulp.task('serve', function() { |
| 102 | runSequence( |
| 103 | 'bower', |
| 104 | 'inject', |
| 105 | ['browser'] |
| 106 | ); |
| 107 | }); |
| 108 | } |