Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [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 del = require('del'); |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 11 | var httpProxy = require('http-proxy'); |
| 12 | |
| 13 | const environment = process.env.NODE_ENV; |
| 14 | |
| 15 | if (environment){ |
| 16 | var conf = require(`../env/${environment}.js`); |
| 17 | } |
| 18 | else{ |
| 19 | var conf = require('../env/default.js') |
| 20 | } |
| 21 | |
| 22 | console.log(conf); |
| 23 | |
| 24 | var proxy = httpProxy.createProxyServer({ |
| 25 | target: conf.host || 'http://0.0.0.0:9999' |
| 26 | }); |
| 27 | |
| 28 | proxy.on('error', function(error, req, res) { |
| 29 | res.writeHead(500, { |
| 30 | 'Content-Type': 'text/plain' |
| 31 | }); |
| 32 | |
| 33 | console.error('[Proxy]', error); |
| 34 | }); |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 35 | |
| 36 | module.exports = function(options){ |
| 37 | |
| 38 | // open in browser with sync and proxy to 0.0.0.0 |
| 39 | gulp.task('browser', function() { |
| 40 | browserSync.init({ |
| 41 | // reloadDelay: 500, |
| 42 | // logLevel: 'debug', |
| 43 | // logConnections: true, |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 44 | // directory: true, |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 45 | startPath: '#/', |
| 46 | snippetOptions: { |
| 47 | rule: { |
| 48 | match: /<!-- browserSync -->/i |
| 49 | } |
| 50 | }, |
| 51 | server: { |
| 52 | baseDir: options.src, |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 53 | middleware: function(req, res, next){ |
| 54 | if( |
| 55 | req.url.indexOf('autoscaledata') !== -1 |
| 56 | ){ |
| 57 | proxy.web(req, res); |
| 58 | } |
| 59 | else{ |
| 60 | next(); |
| 61 | } |
| 62 | } |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 63 | } |
| 64 | }); |
| 65 | |
| 66 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 67 | |
| 68 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 69 | browserSync.reload(); |
| 70 | }); |
| 71 | gulp.watch(options.src + '**/*.html', function(){ |
| 72 | browserSync.reload(); |
| 73 | }); |
| 74 | gulp.watch(options.src + '**/*.css', function(){ |
| 75 | browserSync.reload(); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | // transpile js with sourceMaps |
| 80 | gulp.task('babel', function(){ |
| 81 | return gulp.src([options.scripts + '**/*.js']) |
| 82 | .pipe(babel({sourceMaps: true})) |
| 83 | .pipe(gulp.dest(options.tmp)); |
| 84 | }); |
| 85 | |
| 86 | // inject scripts |
| 87 | gulp.task('injectScript', function(){ |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 88 | runSequence( |
| 89 | 'cleanTmp', |
| 90 | 'babel', |
| 91 | function() { |
| 92 | return gulp.src(options.src + 'index.html') |
| 93 | .pipe( |
| 94 | inject( |
| 95 | gulp.src([ |
| 96 | options.tmp + '**/*.js', |
| 97 | options.api + '*.js', |
| 98 | options.helpers + '**/*.js' |
| 99 | ]) |
| 100 | // .pipe(debug({title: 'unicorn:'})) |
| 101 | .pipe(angularFilesort()), |
| 102 | { |
| 103 | ignorePath: [options.src, '/../../ngXosLib'] |
| 104 | } |
| 105 | ) |
| 106 | ) |
| 107 | .pipe(gulp.dest(options.src)); |
| 108 | } |
| 109 | ); |
| 110 | }); |
| 111 | |
| 112 | // inject CSS |
| 113 | gulp.task('injectCss', function(){ |
| 114 | return gulp.src(options.src + 'index.html') |
| 115 | .pipe( |
| 116 | inject( |
| 117 | gulp.src(options.src + 'css/*.css'), |
| 118 | { |
| 119 | ignorePath: [options.src] |
| 120 | } |
| 121 | ) |
| 122 | ) |
| 123 | .pipe(gulp.dest(options.src)); |
| 124 | }); |
| 125 | |
| 126 | // inject bower dependencies with wiredep |
| 127 | gulp.task('bower', function () { |
| 128 | return gulp.src(options.src + 'index.html') |
| 129 | .pipe(wiredep({devDependencies: true})) |
| 130 | .pipe(gulp.dest(options.src)); |
| 131 | }); |
| 132 | |
| 133 | gulp.task('js-watch', ['injectScript'], function(){ |
| 134 | browserSync.reload(); |
| 135 | }); |
| 136 | |
| 137 | gulp.task('cleanTmp', function(){ |
| 138 | return del([options.tmp + '**/*']); |
| 139 | }); |
| 140 | |
| 141 | gulp.task('serve', function() { |
| 142 | runSequence( |
| 143 | 'bower', |
| 144 | 'injectScript', |
| 145 | 'injectCss', |
| 146 | ['browser'] |
| 147 | ); |
| 148 | }); |
| 149 | }; |