Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [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'); |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -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 | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 15 | |
| 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 | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 25 | } |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 26 | |
| 27 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 28 | |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [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 | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 31 | }); |
| 32 | |
Matteo Scandolo | cdb3e84 | 2016-07-25 16:43:16 -0700 | [diff] [blame^] | 33 | |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 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 | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 44 | gulp.task('browser', function() { |
| 45 | browserSync.init({ |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 46 | startPath: '#/', |
| 47 | snippetOptions: { |
| 48 | rule: { |
| 49 | match: /<!-- browserSync -->/i |
| 50 | } |
| 51 | }, |
| 52 | server: { |
| 53 | baseDir: options.src, |
| 54 | routes: { |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 55 | '/xos/core/static': options.static + '../../static/' |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 56 | }, |
| 57 | middleware: function(req, res, next){ |
| 58 | if( |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 59 | req.url.indexOf('?no_hyperlinks=1') !== -1 || |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 60 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 61 | ){ |
| 62 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 63 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 64 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 65 | } |
| 66 | proxy.web(req, res); |
| 67 | } |
| 68 | else{ |
| 69 | next(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 76 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 77 | browserSync.reload(); |
| 78 | }); |
| 79 | gulp.watch(options.src + '**/*.html', function(){ |
| 80 | browserSync.reload(); |
| 81 | }); |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -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 | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | // transpile js with sourceMaps |
| 105 | gulp.task('babel', function(){ |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 106 | return gulp.src(options.scripts + '**/*.js') |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 107 | .pipe(babel({sourceMaps: true})) |
| 108 | .pipe(gulp.dest(options.tmp)); |
| 109 | }); |
| 110 | |
| 111 | // inject scripts |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 112 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 113 | return gulp.src(options.src + 'index.html') |
| 114 | .pipe( |
| 115 | inject( |
| 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 | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 119 | ]) |
| 120 | .pipe(angularFilesort()), |
| 121 | { |
| 122 | ignorePath: [options.src, '/../../ngXosLib'] |
| 123 | } |
| 124 | ) |
| 125 | ) |
| 126 | .pipe(gulp.dest(options.src)); |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 127 | }); |
| 128 | |
| 129 | // inject CSS |
| 130 | gulp.task('injectCss', function(){ |
| 131 | return gulp.src(options.src + 'index.html') |
| 132 | .pipe( |
| 133 | inject( |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 134 | gulp.src([ |
| 135 | options.src + 'css/*.css', |
| 136 | options.static + '../../static/xosNgLib.css' |
| 137 | ]), |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 138 | { |
| 139 | ignorePath: [options.src] |
| 140 | } |
| 141 | ) |
| 142 | ) |
| 143 | .pipe(gulp.dest(options.src)); |
| 144 | }); |
| 145 | |
| 146 | // inject bower dependencies with wiredep |
| 147 | gulp.task('bower', function () { |
| 148 | return gulp.src(options.src + 'index.html') |
| 149 | .pipe(wiredep({devDependencies: true})) |
| 150 | .pipe(gulp.dest(options.src)); |
| 151 | }); |
| 152 | |
| 153 | gulp.task('js-watch', ['injectScript'], function(){ |
| 154 | browserSync.reload(); |
| 155 | }); |
| 156 | |
| 157 | gulp.task('cleanTmp', function(){ |
| 158 | return del([options.tmp + '**/*']); |
| 159 | }); |
| 160 | |
| 161 | gulp.task('serve', function() { |
| 162 | runSequence( |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 163 | 'sass', |
Matteo Scandolo | 68c2e72 | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 164 | 'bower', |
| 165 | 'injectScript', |
| 166 | 'injectCss', |
| 167 | ['browser'] |
| 168 | ); |
| 169 | }); |
| 170 | }; |