Matteo Scandolo | 6bc31bf | 2016-08-29 10:17:31 -0700 | [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'); |
| 11 | var del = require('del'); |
| 12 | var sass = require('gulp-sass'); |
| 13 | var fs = require('fs'); |
| 14 | var path = require('path'); |
| 15 | |
| 16 | const environment = process.env.NODE_ENV; |
| 17 | |
| 18 | if(!fs.existsSync(path.join(__dirname, `../../../env/${environment || 'default'}.js`))){ |
| 19 | if(!environment){ |
| 20 | throw new Error('You should define a default.js config in /views/env folder.'); |
| 21 | } |
| 22 | else{ |
| 23 | throw new Error(`Since you are loading a custom environment, you should define a ${environment}.js config in /views/env folder.`); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
| 28 | |
| 29 | var proxy = httpProxy.createProxyServer({ |
| 30 | target: conf.host |
| 31 | }); |
| 32 | |
| 33 | |
| 34 | proxy.on('error', function(error, req, res) { |
| 35 | res.writeHead(500, { |
| 36 | 'Content-Type': 'text/plain' |
| 37 | }); |
| 38 | |
| 39 | console.error('[Proxy]', error); |
| 40 | }); |
| 41 | |
| 42 | proxy.on('proxyRes', function (proxyRes, req, res) { |
| 43 | // console.log(JSON.stringify(res, true, 2)); |
| 44 | // console.log(Object.keys(res), res.output); |
| 45 | // console.log(JSON.stringify(proxyRes, true, 2)); |
| 46 | // console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2)); |
| 47 | }); |
| 48 | |
| 49 | module.exports = function(options){ |
| 50 | |
| 51 | gulp.task('browser', function() { |
| 52 | browserSync.init({ |
| 53 | startPath: '#/', |
| 54 | snippetOptions: { |
| 55 | rule: { |
| 56 | match: /<!-- browserSync -->/i |
| 57 | } |
| 58 | }, |
| 59 | server: { |
| 60 | baseDir: options.src, |
| 61 | routes: { |
| 62 | '/xos/core/static': options.static + '../../static/' |
| 63 | }, |
| 64 | middleware: function(req, res, next){ |
| 65 | if( |
| 66 | req.url.indexOf('/api/') !== -1 |
| 67 | ){ |
| 68 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 69 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 70 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 71 | } |
| 72 | proxy.web(req, res); |
| 73 | } |
| 74 | else{ |
| 75 | next(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 82 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 83 | browserSync.reload(); |
| 84 | }); |
| 85 | gulp.watch(options.src + '**/*.html', function(){ |
| 86 | browserSync.reload(); |
| 87 | }); |
| 88 | gulp.watch(options.css + '**/*.css', function(){ |
| 89 | browserSync.reload(); |
| 90 | }); |
| 91 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 92 | browserSync.reload(); |
| 93 | }); |
| 94 | |
| 95 | gulp.watch([ |
| 96 | options.helpers + 'ngXosHelpers.js', |
| 97 | options.static + '../../static/xosNgLib.css' |
| 98 | ], function(){ |
| 99 | browserSync.reload(); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | // compile sass |
| 104 | gulp.task('sass', function () { |
| 105 | return gulp.src(`${options.sass}/**/*.scss`) |
| 106 | .pipe(sass().on('error', sass.logError)) |
| 107 | .pipe(gulp.dest(options.css)); |
| 108 | }); |
| 109 | |
| 110 | // transpile js with sourceMaps |
| 111 | gulp.task('babel', function(){ |
| 112 | return gulp.src(options.scripts + '**/*.js') |
| 113 | .pipe(babel({sourceMaps: true})) |
| 114 | .pipe(gulp.dest(options.tmp)); |
| 115 | }); |
| 116 | |
| 117 | // inject scripts |
| 118 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 119 | return gulp.src(options.src + 'index.html') |
| 120 | .pipe( |
| 121 | inject( |
| 122 | gulp.src([ |
| 123 | options.tmp + '**/*.js', |
| 124 | options.helpers + 'ngXosHelpers.min.js' |
| 125 | ]) |
| 126 | .pipe(angularFilesort()), |
| 127 | { |
| 128 | ignorePath: [options.src, '/../../ngXosLib'] |
| 129 | } |
| 130 | ) |
| 131 | ) |
| 132 | .pipe(gulp.dest(options.src)); |
| 133 | }); |
| 134 | |
| 135 | // inject CSS |
| 136 | gulp.task('injectCss', function(){ |
| 137 | return gulp.src(options.src + 'index.html') |
| 138 | .pipe( |
| 139 | inject( |
| 140 | gulp.src([ |
| 141 | options.src + 'css/*.css', |
| 142 | options.static + '../../static/xosNgLib.css' |
| 143 | ]), |
| 144 | { |
| 145 | ignorePath: [options.src] |
| 146 | } |
| 147 | ) |
| 148 | ) |
| 149 | .pipe(gulp.dest(options.src)); |
| 150 | }); |
| 151 | |
| 152 | // inject bower dependencies with wiredep |
| 153 | gulp.task('bower', function () { |
| 154 | return gulp.src(options.src + 'index.html') |
| 155 | .pipe(wiredep({devDependencies: true})) |
| 156 | .pipe(gulp.dest(options.src)); |
| 157 | }); |
| 158 | |
| 159 | gulp.task('js-watch', ['injectScript'], function(){ |
| 160 | browserSync.reload(); |
| 161 | }); |
| 162 | |
| 163 | gulp.task('cleanTmp', function(){ |
| 164 | return del([options.tmp + '**/*']); |
| 165 | }); |
| 166 | |
| 167 | gulp.task('serve', function() { |
| 168 | runSequence( |
| 169 | 'sass', |
| 170 | 'bower', |
| 171 | 'injectScript', |
| 172 | 'injectCss', |
| 173 | ['browser'] |
| 174 | ); |
| 175 | }); |
| 176 | }; |