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