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: { |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 42 | '/api': options.api, |
Matteo Scandolo | 6328d9b | 2015-11-05 16:02:11 +0100 | [diff] [blame] | 43 | '/xosHelpers/src': options.helpers |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 44 | }, |
| 45 | middleware: function(req, res, next){ |
Matteo Scandolo | c8b9532 | 2015-11-06 09:34:03 +0100 | [diff] [blame] | 46 | if( |
| 47 | req.url.indexOf('/xos/') !== -1 || |
| 48 | req.url.indexOf('/xoslib/') !== -1 || |
| 49 | req.url.indexOf('/hpcapi/') !== -1 |
| 50 | ){ |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 51 | proxy.web(req, res); |
| 52 | } |
| 53 | else{ |
| 54 | next(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | }); |
| 59 | |
Matteo Scandolo | 9348777 | 2015-11-04 15:15:17 +0100 | [diff] [blame] | 60 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 61 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 62 | browserSync.reload(); |
| 63 | }); |
Matteo Scandolo | 4ea1cdb | 2015-11-04 11:34:44 +0100 | [diff] [blame] | 64 | gulp.watch(options.src + '**/*.html', function(){ |
| 65 | browserSync.reload(); |
| 66 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 67 | }); |
| 68 | |
| 69 | // transpile js with sourceMaps |
| 70 | gulp.task('babel', function(){ |
| 71 | return gulp.src(options.scripts + '**/*.js') |
| 72 | .pipe(babel({sourceMaps: true})) |
| 73 | .pipe(gulp.dest(options.tmp)); |
| 74 | }); |
| 75 | |
| 76 | // inject scripts |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 77 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 78 | return gulp.src(options.src + 'index.html') |
| 79 | .pipe( |
| 80 | inject( |
| 81 | gulp.src([ |
| 82 | options.tmp + '**/*.js', |
Matteo Scandolo | b0238d3 | 2015-11-04 16:03:59 +0100 | [diff] [blame] | 83 | options.api + '*.js', |
| 84 | options.helpers + '**/*.js' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 85 | ]) |
| 86 | .pipe(angularFilesort()), |
| 87 | { |
| 88 | ignorePath: [options.src, '/../../ngXosLib'] |
| 89 | } |
| 90 | ) |
| 91 | ) |
| 92 | .pipe(gulp.dest(options.src)); |
| 93 | }); |
| 94 | |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 95 | // 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 Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 109 | // inject bower dependencies with wiredep |
| 110 | gulp.task('bower', function () { |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 111 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 112 | .pipe(wiredep({devDependencies: true})) |
| 113 | .pipe(gulp.dest(options.src)); |
| 114 | }); |
| 115 | |
Matteo Scandolo | ab91f43 | 2015-11-06 18:25:52 +0100 | [diff] [blame^] | 116 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | 8cbfa64 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 117 | browserSync.reload(); |
| 118 | }); |
| 119 | |
| 120 | gulp.task('cleanTmp', function(){ |
| 121 | return del([options.tmp + '**/*']); |
| 122 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 123 | |
| 124 | gulp.task('serve', function() { |
| 125 | runSequence( |
| 126 | 'bower', |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 127 | 'injectScript', |
| 128 | 'injectCss', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 129 | ['browser'] |
| 130 | ); |
| 131 | }); |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 132 | }; |