Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 6bc31bf | 2016-08-29 10:17:31 -0700 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | var gulp = require('gulp'); |
| 22 | var browserSync = require('browser-sync').create(); |
| 23 | var inject = require('gulp-inject'); |
| 24 | var runSequence = require('run-sequence'); |
| 25 | var angularFilesort = require('gulp-angular-filesort'); |
| 26 | var babel = require('gulp-babel'); |
| 27 | var wiredep = require('wiredep').stream; |
| 28 | var httpProxy = require('http-proxy'); |
| 29 | var del = require('del'); |
| 30 | var sass = require('gulp-sass'); |
| 31 | var fs = require('fs'); |
| 32 | var path = require('path'); |
| 33 | |
| 34 | const environment = process.env.NODE_ENV; |
| 35 | |
| 36 | if(!fs.existsSync(path.join(__dirname, `../../../env/${environment || 'default'}.js`))){ |
| 37 | if(!environment){ |
| 38 | throw new Error('You should define a default.js config in /views/env folder.'); |
| 39 | } |
| 40 | else{ |
| 41 | throw new Error(`Since you are loading a custom environment, you should define a ${environment}.js config in /views/env folder.`); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
| 46 | |
| 47 | var proxy = httpProxy.createProxyServer({ |
| 48 | target: conf.host |
| 49 | }); |
| 50 | |
| 51 | |
| 52 | proxy.on('error', function(error, req, res) { |
| 53 | res.writeHead(500, { |
| 54 | 'Content-Type': 'text/plain' |
| 55 | }); |
| 56 | |
| 57 | console.error('[Proxy]', error); |
| 58 | }); |
| 59 | |
| 60 | proxy.on('proxyRes', function (proxyRes, req, res) { |
| 61 | // console.log(JSON.stringify(res, true, 2)); |
| 62 | // console.log(Object.keys(res), res.output); |
| 63 | // console.log(JSON.stringify(proxyRes, true, 2)); |
| 64 | // console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2)); |
| 65 | }); |
| 66 | |
| 67 | module.exports = function(options){ |
| 68 | |
| 69 | gulp.task('browser', function() { |
| 70 | browserSync.init({ |
| 71 | startPath: '#/', |
| 72 | snippetOptions: { |
| 73 | rule: { |
| 74 | match: /<!-- browserSync -->/i |
| 75 | } |
| 76 | }, |
| 77 | server: { |
| 78 | baseDir: options.src, |
| 79 | routes: { |
| 80 | '/xos/core/static': options.static + '../../static/' |
| 81 | }, |
| 82 | middleware: function(req, res, next){ |
| 83 | if( |
| 84 | req.url.indexOf('/api/') !== -1 |
| 85 | ){ |
| 86 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 87 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 88 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 89 | } |
| 90 | proxy.web(req, res); |
| 91 | } |
| 92 | else{ |
| 93 | next(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 100 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 101 | browserSync.reload(); |
| 102 | }); |
| 103 | gulp.watch(options.src + '**/*.html', function(){ |
| 104 | browserSync.reload(); |
| 105 | }); |
| 106 | gulp.watch(options.css + '**/*.css', function(){ |
| 107 | browserSync.reload(); |
| 108 | }); |
| 109 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 110 | browserSync.reload(); |
| 111 | }); |
| 112 | |
| 113 | gulp.watch([ |
| 114 | options.helpers + 'ngXosHelpers.js', |
| 115 | options.static + '../../static/xosNgLib.css' |
| 116 | ], function(){ |
| 117 | browserSync.reload(); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | // compile sass |
| 122 | gulp.task('sass', function () { |
| 123 | return gulp.src(`${options.sass}/**/*.scss`) |
| 124 | .pipe(sass().on('error', sass.logError)) |
| 125 | .pipe(gulp.dest(options.css)); |
| 126 | }); |
| 127 | |
| 128 | // transpile js with sourceMaps |
| 129 | gulp.task('babel', function(){ |
| 130 | return gulp.src(options.scripts + '**/*.js') |
| 131 | .pipe(babel({sourceMaps: true})) |
| 132 | .pipe(gulp.dest(options.tmp)); |
| 133 | }); |
| 134 | |
| 135 | // inject scripts |
| 136 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 137 | return gulp.src(options.src + 'index.html') |
| 138 | .pipe( |
| 139 | inject( |
| 140 | gulp.src([ |
| 141 | options.tmp + '**/*.js', |
| 142 | options.helpers + 'ngXosHelpers.min.js' |
| 143 | ]) |
| 144 | .pipe(angularFilesort()), |
| 145 | { |
| 146 | ignorePath: [options.src, '/../../ngXosLib'] |
| 147 | } |
| 148 | ) |
| 149 | ) |
| 150 | .pipe(gulp.dest(options.src)); |
| 151 | }); |
| 152 | |
| 153 | // inject CSS |
| 154 | gulp.task('injectCss', function(){ |
| 155 | return gulp.src(options.src + 'index.html') |
| 156 | .pipe( |
| 157 | inject( |
| 158 | gulp.src([ |
| 159 | options.src + 'css/*.css', |
| 160 | options.static + '../../static/xosNgLib.css' |
| 161 | ]), |
| 162 | { |
| 163 | ignorePath: [options.src] |
| 164 | } |
| 165 | ) |
| 166 | ) |
| 167 | .pipe(gulp.dest(options.src)); |
| 168 | }); |
| 169 | |
| 170 | // inject bower dependencies with wiredep |
| 171 | gulp.task('bower', function () { |
| 172 | return gulp.src(options.src + 'index.html') |
| 173 | .pipe(wiredep({devDependencies: true})) |
| 174 | .pipe(gulp.dest(options.src)); |
| 175 | }); |
| 176 | |
| 177 | gulp.task('js-watch', ['injectScript'], function(){ |
| 178 | browserSync.reload(); |
| 179 | }); |
| 180 | |
| 181 | gulp.task('cleanTmp', function(){ |
| 182 | return del([options.tmp + '**/*']); |
| 183 | }); |
| 184 | |
| 185 | gulp.task('serve', function() { |
| 186 | runSequence( |
| 187 | 'sass', |
| 188 | 'bower', |
| 189 | 'injectScript', |
| 190 | 'injectCss', |
| 191 | ['browser'] |
| 192 | ); |
| 193 | }); |
| 194 | }; |