arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -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'); |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 13 | var fs = require('fs'); |
| 14 | var path = require('path'); |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 15 | |
| 16 | const environment = process.env.NODE_ENV; |
| 17 | |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 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 | } |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 27 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
| 28 | |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 29 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 30 | target: conf.host |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 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 | module.exports = function(options){ |
| 43 | |
| 44 | gulp.task('browser', function() { |
| 45 | browserSync.init({ |
| 46 | startPath: '#/', |
| 47 | snippetOptions: { |
| 48 | rule: { |
| 49 | match: /<!-- browserSync -->/i |
| 50 | } |
| 51 | }, |
| 52 | server: { |
| 53 | baseDir: options.src, |
| 54 | routes: { |
| 55 | '/xos/core/xoslib/static/js/vendor': options.helpers, |
| 56 | '/xos/core/static': options.static + '../../static/' |
| 57 | }, |
| 58 | middleware: function(req, res, next){ |
| 59 | if( |
| 60 | // to be removed, deprecated API |
| 61 | // req.url.indexOf('/xos/') !== -1 || |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 62 | req.url.indexOf('/xoslib/tenant') !== -1 || |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 63 | // req.url.indexOf('/hpcapi/') !== -1 || |
| 64 | req.url.indexOf('/api/') !== -1 |
| 65 | ){ |
| 66 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 67 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 68 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 69 | } |
| 70 | proxy.web(req, res); |
| 71 | } |
| 72 | else{ |
| 73 | next(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 80 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 81 | browserSync.reload(); |
| 82 | }); |
| 83 | gulp.watch(options.src + '**/*.html', function(){ |
| 84 | browserSync.reload(); |
| 85 | }); |
| 86 | gulp.watch(options.css + '**/*.css', function(){ |
| 87 | browserSync.reload(); |
| 88 | }); |
| 89 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 90 | browserSync.reload(); |
| 91 | }); |
| 92 | |
| 93 | gulp.watch([ |
| 94 | options.helpers + 'ngXosHelpers.js', |
| 95 | options.static + '../../static/xosNgLib.css' |
| 96 | ], function(){ |
| 97 | browserSync.reload(); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | // compile sass |
| 102 | gulp.task('sass', function () { |
| 103 | return gulp.src(`${options.sass}/**/*.scss`) |
| 104 | .pipe(sass().on('error', sass.logError)) |
| 105 | .pipe(gulp.dest(options.css)); |
| 106 | }); |
| 107 | |
| 108 | // transpile js with sourceMaps |
| 109 | gulp.task('babel', function(){ |
| 110 | return gulp.src(options.scripts + '**/*.js') |
| 111 | .pipe(babel({sourceMaps: true})) |
| 112 | .pipe(gulp.dest(options.tmp)); |
| 113 | }); |
| 114 | |
| 115 | // inject scripts |
| 116 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 117 | return gulp.src(options.src + 'index.html') |
| 118 | .pipe( |
| 119 | inject( |
| 120 | gulp.src([ |
| 121 | options.tmp + '**/*.js', |
| 122 | options.helpers + 'ngXosHelpers.js' |
| 123 | ]) |
| 124 | .pipe(angularFilesort()), |
| 125 | { |
| 126 | ignorePath: [options.src, '/../../ngXosLib'] |
| 127 | } |
| 128 | ) |
| 129 | ) |
| 130 | .pipe(gulp.dest(options.src)); |
| 131 | }); |
| 132 | |
| 133 | // inject CSS |
| 134 | gulp.task('injectCss', function(){ |
| 135 | return gulp.src(options.src + 'index.html') |
| 136 | .pipe( |
| 137 | inject( |
| 138 | gulp.src([ |
| 139 | options.src + 'css/*.css', |
| 140 | options.static + '../../static/xosNgLib.css' |
| 141 | ]), |
| 142 | { |
| 143 | ignorePath: [options.src] |
| 144 | } |
| 145 | ) |
| 146 | ) |
| 147 | .pipe(gulp.dest(options.src)); |
| 148 | }); |
| 149 | |
| 150 | // inject bower dependencies with wiredep |
| 151 | gulp.task('bower', function () { |
| 152 | return gulp.src(options.src + 'index.html') |
| 153 | .pipe(wiredep({devDependencies: true})) |
| 154 | .pipe(gulp.dest(options.src)); |
| 155 | }); |
| 156 | |
| 157 | gulp.task('js-watch', ['injectScript'], function(){ |
| 158 | browserSync.reload(); |
| 159 | }); |
| 160 | |
| 161 | gulp.task('cleanTmp', function(){ |
| 162 | return del([options.tmp + '**/*']); |
| 163 | }); |
| 164 | |
| 165 | gulp.task('serve', function() { |
| 166 | runSequence( |
| 167 | 'sass', |
| 168 | 'bower', |
| 169 | 'injectScript', |
| 170 | 'injectCss', |
| 171 | ['browser'] |
| 172 | ); |
| 173 | }); |
| 174 | }; |