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 | df35ca9 | 2016-02-25 09:19:41 -0800 | [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'); |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 30 | var sass = require('gulp-sass'); |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 31 | var fs = require('fs'); |
| 32 | var path = require('path'); |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 33 | |
| 34 | const environment = process.env.NODE_ENV; |
| 35 | |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -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 | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 45 | var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`)); |
| 46 | |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 47 | var proxy = httpProxy.createProxyServer({ |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 48 | target: conf.host |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 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 | module.exports = function(options){ |
| 61 | |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 62 | gulp.task('browser', function() { |
| 63 | browserSync.init({ |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 64 | startPath: '#/', |
| 65 | snippetOptions: { |
| 66 | rule: { |
| 67 | match: /<!-- browserSync -->/i |
| 68 | } |
| 69 | }, |
| 70 | server: { |
| 71 | baseDir: options.src, |
| 72 | routes: { |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 73 | '/xos/core/static': options.static + '../../static/' |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 74 | }, |
| 75 | middleware: function(req, res, next){ |
| 76 | if( |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 77 | req.url.indexOf('/api/') !== -1 |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 78 | ){ |
| 79 | if(conf.xoscsrftoken && conf.xossessionid){ |
| 80 | req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`; |
| 81 | req.headers['x-csrftoken'] = conf.xoscsrftoken; |
| 82 | } |
| 83 | proxy.web(req, res); |
| 84 | } |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 85 | else if(req.url.indexOf('/videoLocal.txt') !== -1){ |
| 86 | let a = (Math.random() * 10).toString(); |
| 87 | res.end(a) |
Matteo Scandolo | 97646d6 | 2016-03-07 14:14:00 -0800 | [diff] [blame] | 88 | } |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 89 | else{ |
| 90 | next(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 97 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 98 | browserSync.reload(); |
| 99 | }); |
| 100 | gulp.watch(options.src + '**/*.html', function(){ |
| 101 | browserSync.reload(); |
| 102 | }); |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 103 | gulp.watch(options.css + '**/*.css', function(){ |
| 104 | browserSync.reload(); |
| 105 | }); |
| 106 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 107 | browserSync.reload(); |
| 108 | }); |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 109 | |
| 110 | gulp.watch([ |
| 111 | options.helpers + 'ngXosHelpers.js', |
| 112 | options.static + '../../static/xosNgLib.css' |
| 113 | ], function(){ |
| 114 | browserSync.reload(); |
| 115 | }); |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 116 | }); |
| 117 | |
| 118 | // compile sass |
| 119 | gulp.task('sass', function () { |
| 120 | return gulp.src(`${options.sass}/**/*.scss`) |
| 121 | .pipe(sass().on('error', sass.logError)) |
| 122 | .pipe(gulp.dest(options.css)); |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 123 | }); |
| 124 | |
| 125 | // transpile js with sourceMaps |
| 126 | gulp.task('babel', function(){ |
| 127 | return gulp.src(options.scripts + '**/*.js') |
| 128 | .pipe(babel({sourceMaps: true})) |
| 129 | .pipe(gulp.dest(options.tmp)); |
| 130 | }); |
| 131 | |
| 132 | // inject scripts |
| 133 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 134 | return gulp.src(options.src + 'index.html') |
| 135 | .pipe( |
| 136 | inject( |
| 137 | gulp.src([ |
| 138 | options.tmp + '**/*.js', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 139 | options.helpers + 'ngXosHelpers.min.js' |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 140 | ]) |
| 141 | .pipe(angularFilesort()), |
| 142 | { |
| 143 | ignorePath: [options.src, '/../../ngXosLib'] |
| 144 | } |
| 145 | ) |
| 146 | ) |
| 147 | .pipe(gulp.dest(options.src)); |
| 148 | }); |
| 149 | |
| 150 | // inject CSS |
| 151 | gulp.task('injectCss', function(){ |
| 152 | return gulp.src(options.src + 'index.html') |
| 153 | .pipe( |
| 154 | inject( |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 155 | gulp.src([ |
| 156 | options.src + 'css/*.css', |
| 157 | options.static + '../../static/xosNgLib.css' |
| 158 | ]), |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 159 | { |
| 160 | ignorePath: [options.src] |
| 161 | } |
| 162 | ) |
| 163 | ) |
| 164 | .pipe(gulp.dest(options.src)); |
| 165 | }); |
| 166 | |
| 167 | // inject bower dependencies with wiredep |
| 168 | gulp.task('bower', function () { |
| 169 | return gulp.src(options.src + 'index.html') |
| 170 | .pipe(wiredep({devDependencies: true})) |
| 171 | .pipe(gulp.dest(options.src)); |
| 172 | }); |
| 173 | |
| 174 | gulp.task('js-watch', ['injectScript'], function(){ |
| 175 | browserSync.reload(); |
| 176 | }); |
| 177 | |
| 178 | gulp.task('cleanTmp', function(){ |
| 179 | return del([options.tmp + '**/*']); |
| 180 | }); |
| 181 | |
| 182 | gulp.task('serve', function() { |
| 183 | runSequence( |
Matteo Scandolo | 1778b74 | 2016-04-22 09:47:50 -0700 | [diff] [blame] | 184 | 'sass', |
Matteo Scandolo | df35ca9 | 2016-02-25 09:19:41 -0800 | [diff] [blame] | 185 | 'bower', |
| 186 | 'injectScript', |
| 187 | 'injectCss', |
| 188 | ['browser'] |
| 189 | ); |
| 190 | }); |
| 191 | }; |