Matteo Scandolo | cb11e0c | 2015-10-29 15:50:15 +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'); |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 6 | var runSequence = require('run-sequence'); |
| 7 | var angularFilesort = require('gulp-angular-filesort'); |
| 8 | var babel = require('gulp-babel'); |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 9 | var wiredep = require('wiredep').stream; |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 10 | var httpProxy = require('http-proxy'); |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 11 | var del = require('del'); |
Matteo Scandolo | cb11e0c | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 12 | |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 13 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | 46485ac | 2015-11-03 10:48:01 +0100 | [diff] [blame] | 14 | target: 'http://0.0.0.0:9999' |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 15 | }); |
| 16 | |
| 17 | |
| 18 | proxy.on('error', function(error, req, res) { |
| 19 | res.writeHead(500, { |
| 20 | 'Content-Type': 'text/plain' |
Matteo Scandolo | cb11e0c | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 21 | }); |
| 22 | |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 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 | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 31 | // reloadDelay: 500, |
| 32 | // logLevel: 'debug', |
| 33 | // logConnections: true, |
Matteo Scandolo | 2d2484f | 2015-11-10 17:13:04 +0100 | [diff] [blame] | 34 | startPath: '#/', |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 35 | snippetOptions: { |
| 36 | rule: { |
| 37 | match: /<!-- browserSync -->/i |
| 38 | } |
| 39 | }, |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 40 | server: { |
| 41 | baseDir: options.src, |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 42 | routes: { |
| 43 | '/api': options.api, |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 44 | '/xosHelpers/src': options.helpers |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 45 | }, |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 46 | middleware: function(req, res, next){ |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 47 | if( |
| 48 | req.url.indexOf('/xos/') !== -1 || |
| 49 | req.url.indexOf('/xoslib/') !== -1 || |
| 50 | req.url.indexOf('/hpcapi/') !== -1 |
| 51 | ){ |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 52 | proxy.web(req, res); |
| 53 | } |
| 54 | else{ |
| 55 | next(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | }); |
| 60 | |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 61 | 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 Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 68 | }); |
| 69 | |
| 70 | // transpile js with sourceMaps |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 71 | 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 Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 77 | // inject scripts |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 78 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 79 | return gulp.src(options.src + 'index.html') |
| 80 | .pipe( |
| 81 | inject( |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 82 | gulp.src([ |
| 83 | options.tmp + '**/*.js', |
| 84 | options.api + '*.js', |
| 85 | options.helpers + '**/*.js' |
| 86 | ]) |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 87 | .pipe(angularFilesort()), |
| 88 | { |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 89 | ignorePath: [options.src, '/../../ngXosLib'] |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 90 | } |
| 91 | ) |
| 92 | ) |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 93 | .pipe(gulp.dest(options.src)); |
Matteo Scandolo | cb11e0c | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 94 | }); |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 95 | |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 96 | // 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 Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 110 | // inject bower dependencies with wiredep |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 111 | gulp.task('bower', function () { |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 112 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 113 | .pipe(wiredep({devDependencies: true})) |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 114 | .pipe(gulp.dest(options.src)); |
Matteo Scandolo | c952aa9 | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 115 | }); |
| 116 | |
Matteo Scandolo | 6f7f974 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 117 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 118 | browserSync.reload(); |
| 119 | }); |
| 120 | |
| 121 | gulp.task('cleanTmp', function(){ |
| 122 | return del([options.tmp + '**/*']); |
| 123 | }); |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 124 | |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 125 | gulp.task('serve', function() { |
| 126 | runSequence( |
Matteo Scandolo | 486f268 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 127 | 'bower', |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 128 | 'injectScript', |
| 129 | 'injectCss', |
Matteo Scandolo | 82697c0 | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 130 | ['browser'] |
Matteo Scandolo | 2729d08 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 131 | ); |
| 132 | }); |
Matteo Scandolo | c5ee7b5 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 133 | }; |