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 | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | var gulp = require('gulp'); |
| 22 | var browserSync = require('browser-sync').create(); |
| 23 | var inject = require('gulp-inject'); |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 24 | var runSequence = require('run-sequence'); |
| 25 | var angularFilesort = require('gulp-angular-filesort'); |
| 26 | var babel = require('gulp-babel'); |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 27 | var wiredep = require('wiredep').stream; |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 28 | var httpProxy = require('http-proxy'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 29 | var del = require('del'); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 30 | var sass = require('gulp-sass'); |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 31 | var fs = require('fs'); |
| 32 | var path = require('path'); |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 33 | |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 34 | const environment = process.env.NODE_ENV; |
| 35 | |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 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 | } |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 45 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
| 46 | |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 47 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 48 | target: conf.host |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | |
| 52 | proxy.on('error', function(error, req, res) { |
| 53 | res.writeHead(500, { |
| 54 | 'Content-Type': 'text/plain' |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 55 | }); |
| 56 | |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 57 | console.error('[Proxy]', error); |
| 58 | }); |
| 59 | |
| 60 | module.exports = function(options){ |
| 61 | |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 62 | gulp.task('browser', function() { |
| 63 | browserSync.init({ |
Matteo Scandolo | 9a60726 | 2015-11-10 17:13:04 +0100 | [diff] [blame] | 64 | startPath: '#/', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 65 | snippetOptions: { |
| 66 | rule: { |
| 67 | match: /<!-- browserSync -->/i |
| 68 | } |
| 69 | }, |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 70 | server: { |
| 71 | baseDir: options.src, |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 72 | routes: { |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 73 | '/xos/core/static': options.static + '../../static/' |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 74 | }, |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 75 | middleware: function(req, res, next){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 76 | if( |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 77 | req.url.indexOf('/hpcapi/') !== -1 || |
| 78 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 79 | ){ |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 80 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 81 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 82 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 83 | } |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 84 | proxy.web(req, res); |
| 85 | } |
| 86 | else{ |
| 87 | next(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 93 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 94 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 95 | browserSync.reload(); |
| 96 | }); |
| 97 | gulp.watch(options.src + '**/*.html', function(){ |
| 98 | browserSync.reload(); |
| 99 | }); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 100 | gulp.watch(options.css + '**/*.css', function(){ |
| 101 | browserSync.reload(); |
| 102 | }); |
| 103 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 104 | browserSync.reload(); |
| 105 | }); |
| 106 | |
| 107 | gulp.watch([ |
| 108 | options.helpers + 'ngXosHelpers.js', |
| 109 | options.static + '../../static/xosNgLib.css' |
| 110 | ], function(){ |
| 111 | browserSync.reload(); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | // compile sass |
| 116 | gulp.task('sass', function () { |
| 117 | return gulp.src(`${options.sass}/**/*.scss`) |
| 118 | .pipe(sass().on('error', sass.logError)) |
| 119 | .pipe(gulp.dest(options.css)); |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 120 | }); |
| 121 | |
| 122 | // transpile js with sourceMaps |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 123 | gulp.task('babel', function(){ |
| 124 | return gulp.src(options.scripts + '**/*.js') |
| 125 | .pipe(babel({sourceMaps: true})) |
| 126 | .pipe(gulp.dest(options.tmp)); |
| 127 | }); |
| 128 | |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 129 | // inject scripts |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 130 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 131 | return gulp.src(options.src + 'index.html') |
| 132 | .pipe( |
| 133 | inject( |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 134 | gulp.src([ |
| 135 | options.tmp + '**/*.js', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 136 | options.helpers + 'ngXosHelpers.min.js' |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 137 | ]) |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 138 | .pipe(angularFilesort()), |
| 139 | { |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 140 | ignorePath: [options.src, '/../../ngXosLib'] |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 141 | } |
| 142 | ) |
| 143 | ) |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 144 | .pipe(gulp.dest(options.src)); |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 145 | }); |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 146 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 147 | // inject CSS |
| 148 | gulp.task('injectCss', function(){ |
| 149 | return gulp.src(options.src + 'index.html') |
| 150 | .pipe( |
| 151 | inject( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 152 | gulp.src([ |
| 153 | options.src + 'css/*.css', |
| 154 | options.static + '../../static/xosNgLib.css' |
| 155 | ]), |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 156 | { |
| 157 | ignorePath: [options.src] |
| 158 | } |
| 159 | ) |
| 160 | ) |
| 161 | .pipe(gulp.dest(options.src)); |
| 162 | }); |
| 163 | |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 164 | // inject bower dependencies with wiredep |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 165 | gulp.task('bower', function () { |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 166 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 167 | .pipe(wiredep({devDependencies: true})) |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 168 | .pipe(gulp.dest(options.src)); |
Matteo Scandolo | d4f501c | 2015-10-29 17:24:18 +0100 | [diff] [blame] | 169 | }); |
| 170 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 171 | gulp.task('js-watch', ['injectScript'], function(){ |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 172 | browserSync.reload(); |
| 173 | }); |
| 174 | |
| 175 | gulp.task('cleanTmp', function(){ |
| 176 | return del([options.tmp + '**/*']); |
| 177 | }); |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 178 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 179 | gulp.task('serve', function() { |
| 180 | runSequence( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 181 | 'sass', |
Matteo Scandolo | 4577896 | 2015-10-29 16:17:59 +0100 | [diff] [blame] | 182 | 'bower', |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 183 | 'injectScript', |
| 184 | 'injectCss', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 185 | ['browser'] |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 186 | ); |
| 187 | }); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 188 | }; |