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