Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +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'); |
| 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'); |
Matteo Scandolo | ce954e5 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 11 | var del = require('del'); |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -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 | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 15 | |
Matteo Scandolo | 7ed6c76 | 2015-11-30 17:25:39 -0800 | [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 | 9323b1b | 2015-11-25 12:08:41 -0800 | [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 | e53ee05 | 2015-11-03 14:32:00 +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 | e53ee05 | 2015-11-03 14:32:00 +0100 | [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 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 44 | gulp.task('browser', function() { |
| 45 | browserSync.init({ |
Matteo Scandolo | f1072ed | 2015-11-09 11:25:40 +0100 | [diff] [blame] | 46 | startPath: '#/', |
Matteo Scandolo | ce954e5 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 47 | snippetOptions: { |
| 48 | rule: { |
| 49 | match: /<!-- browserSync -->/i |
| 50 | } |
| 51 | }, |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 52 | server: { |
| 53 | baseDir: options.src, |
| 54 | routes: { |
Matteo Scandolo | a3839e3 | 2016-04-28 16:20:53 -0700 | [diff] [blame] | 55 | '/xos/core/static': options.static + '../../static/' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 56 | }, |
| 57 | middleware: function(req, res, next){ |
Matteo Scandolo | 0776022 | 2015-11-06 09:34:03 +0100 | [diff] [blame] | 58 | if( |
Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 59 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | 0776022 | 2015-11-06 09:34:03 +0100 | [diff] [blame] | 60 | ){ |
Matteo Scandolo | 9323b1b | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 61 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 62 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 63 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 64 | } |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 65 | proxy.web(req, res); |
| 66 | } |
| 67 | else{ |
| 68 | next(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | |
Matteo Scandolo | 0a0f5f3 | 2015-11-04 15:15:17 +0100 | [diff] [blame] | 74 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 75 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 76 | browserSync.reload(); |
| 77 | }); |
Matteo Scandolo | 73a3c2b | 2015-11-04 11:34:44 +0100 | [diff] [blame] | 78 | gulp.watch(options.src + '**/*.html', function(){ |
| 79 | browserSync.reload(); |
| 80 | }); |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 81 | gulp.watch(options.css + '**/*.css', function(){ |
| 82 | browserSync.reload(); |
| 83 | }); |
| 84 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 85 | browserSync.reload(); |
| 86 | }); |
Matteo Scandolo | a3839e3 | 2016-04-28 16:20:53 -0700 | [diff] [blame] | 87 | |
| 88 | gulp.watch([ |
| 89 | options.helpers + 'ngXosHelpers.js', |
| 90 | options.static + '../../static/xosNgLib.css' |
| 91 | ], function(){ |
| 92 | browserSync.reload(); |
| 93 | }); |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 94 | }); |
| 95 | |
| 96 | // compile sass |
| 97 | gulp.task('sass', function () { |
| 98 | return gulp.src(`${options.sass}/**/*.scss`) |
| 99 | .pipe(sass().on('error', sass.logError)) |
| 100 | .pipe(gulp.dest(options.css)); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 101 | }); |
| 102 | |
| 103 | // transpile js with sourceMaps |
| 104 | gulp.task('babel', function(){ |
| 105 | return gulp.src(options.scripts + '**/*.js') |
| 106 | .pipe(babel({sourceMaps: true})) |
| 107 | .pipe(gulp.dest(options.tmp)); |
| 108 | }); |
| 109 | |
| 110 | // inject scripts |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 111 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 112 | return gulp.src(options.src + 'index.html') |
| 113 | .pipe( |
| 114 | inject( |
| 115 | gulp.src([ |
| 116 | options.tmp + '**/*.js', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 117 | options.helpers + 'ngXosHelpers.min.js' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 118 | ]) |
| 119 | .pipe(angularFilesort()), |
| 120 | { |
| 121 | ignorePath: [options.src, '/../../ngXosLib'] |
| 122 | } |
| 123 | ) |
| 124 | ) |
| 125 | .pipe(gulp.dest(options.src)); |
| 126 | }); |
| 127 | |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 128 | // inject CSS |
| 129 | gulp.task('injectCss', function(){ |
| 130 | return gulp.src(options.src + 'index.html') |
| 131 | .pipe( |
| 132 | inject( |
Matteo Scandolo | a3839e3 | 2016-04-28 16:20:53 -0700 | [diff] [blame] | 133 | gulp.src([ |
| 134 | options.src + 'css/*.css', |
| 135 | options.static + '../../static/xosNgLib.css' |
| 136 | ]), |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 137 | { |
| 138 | ignorePath: [options.src] |
| 139 | } |
| 140 | ) |
| 141 | ) |
| 142 | .pipe(gulp.dest(options.src)); |
| 143 | }); |
| 144 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 145 | // inject bower dependencies with wiredep |
| 146 | gulp.task('bower', function () { |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 147 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 148 | .pipe(wiredep({devDependencies: true})) |
| 149 | .pipe(gulp.dest(options.src)); |
| 150 | }); |
| 151 | |
Matteo Scandolo | ba5e19d | 2015-11-06 18:25:52 +0100 | [diff] [blame] | 152 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | ce954e5 | 2015-11-04 11:31:05 +0100 | [diff] [blame] | 153 | browserSync.reload(); |
| 154 | }); |
| 155 | |
| 156 | gulp.task('cleanTmp', function(){ |
| 157 | return del([options.tmp + '**/*']); |
| 158 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 159 | |
| 160 | gulp.task('serve', function() { |
| 161 | runSequence( |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 162 | 'sass', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 163 | 'bower', |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 164 | 'injectScript', |
| 165 | 'injectCss', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 166 | ['browser'] |
| 167 | ); |
| 168 | }); |
Matteo Scandolo | 9323b1b | 2015-11-25 12:08:41 -0800 | [diff] [blame] | 169 | }; |