Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -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 | 871f9cd | 2016-03-29 14:06:13 -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 | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 15 | |
| 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 | be9b13d | 2016-01-21 11:21:03 -0800 | [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 | be9b13d | 2016-01-21 11:21:03 -0800 | [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 | be9b13d | 2016-01-21 11:21:03 -0800 | [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 | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 44 | gulp.task('browser', function() { |
| 45 | browserSync.init({ |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -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 | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 55 | '/xos/core/xoslib/static/js/vendor': options.helpers, |
| 56 | '/xos/core/static': options.static + '../../static/' |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 57 | }, |
| 58 | middleware: function(req, res, next){ |
| 59 | if( |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 60 | req.url.indexOf('/?no_hyperlinks') !== -1 || |
| 61 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 62 | ){ |
| 63 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 64 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 65 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 66 | } |
| 67 | proxy.web(req, res); |
| 68 | } |
| 69 | else{ |
| 70 | next(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 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 | 871f9cd | 2016-03-29 14:06:13 -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 | }); |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 89 | |
| 90 | gulp.watch([ |
| 91 | options.helpers + 'ngXosHelpers.js', |
| 92 | options.static + '../../static/xosNgLib.css' |
| 93 | ], function(){ |
| 94 | browserSync.reload(); |
| 95 | }); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 98 | // compile sass |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 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)); |
| 103 | }); |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 104 | |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 105 | // transpile js with sourceMaps |
| 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 | |
| 112 | // inject scripts |
| 113 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 114 | return gulp.src(options.src + 'index.html') |
| 115 | .pipe( |
| 116 | inject( |
| 117 | gulp.src([ |
| 118 | options.tmp + '**/*.js', |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 119 | options.helpers + 'ngXosHelpers.js' |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 120 | ]) |
| 121 | .pipe(angularFilesort()), |
| 122 | { |
| 123 | ignorePath: [options.src, '/../../ngXosLib'] |
| 124 | } |
| 125 | ) |
| 126 | ) |
| 127 | .pipe(gulp.dest(options.src)); |
| 128 | }); |
| 129 | |
| 130 | // inject CSS |
| 131 | gulp.task('injectCss', function(){ |
| 132 | return gulp.src(options.src + 'index.html') |
| 133 | .pipe( |
| 134 | inject( |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame^] | 135 | gulp.src([ |
| 136 | options.src + 'css/*.css', |
| 137 | options.static + '../../static/xosNgLib.css' |
| 138 | ]), |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 139 | { |
| 140 | ignorePath: [options.src] |
| 141 | } |
| 142 | ) |
| 143 | ) |
| 144 | .pipe(gulp.dest(options.src)); |
| 145 | }); |
| 146 | |
| 147 | // inject bower dependencies with wiredep |
| 148 | gulp.task('bower', function () { |
| 149 | return gulp.src(options.src + 'index.html') |
| 150 | .pipe(wiredep({devDependencies: true})) |
| 151 | .pipe(gulp.dest(options.src)); |
| 152 | }); |
| 153 | |
| 154 | gulp.task('js-watch', ['injectScript'], function(){ |
| 155 | browserSync.reload(); |
| 156 | }); |
| 157 | |
| 158 | gulp.task('cleanTmp', function(){ |
| 159 | return del([options.tmp + '**/*']); |
| 160 | }); |
| 161 | |
| 162 | gulp.task('serve', function() { |
| 163 | runSequence( |
Matteo Scandolo | 871f9cd | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 164 | 'sass', |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 165 | 'bower', |
| 166 | 'injectScript', |
| 167 | 'injectCss', |
| 168 | ['browser'] |
| 169 | ); |
| 170 | }); |
| 171 | }; |