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 | 7d539d3 | 2016-05-25 08:53:11 -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 | module.exports = function(options){ |
| 61 | |
| 62 | gulp.task('browser', function() { |
| 63 | browserSync.init({ |
| 64 | startPath: '#/', |
| 65 | snippetOptions: { |
| 66 | rule: { |
| 67 | match: /<!-- browserSync -->/i |
| 68 | } |
| 69 | }, |
| 70 | server: { |
| 71 | baseDir: options.src, |
| 72 | routes: { |
Matteo Scandolo | 7d539d3 | 2016-05-25 08:53:11 -0700 | [diff] [blame] | 73 | '/xos/core/static': options.static + '../../static/' |
| 74 | }, |
| 75 | middleware: function(req, res, next){ |
| 76 | if( |
| 77 | req.url.indexOf('/api/') !== -1 |
| 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 | } |
| 85 | else{ |
| 86 | next(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | gulp.watch(options.src + 'js/**/*.js', ['js-watch']); |
| 93 | gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){ |
| 94 | browserSync.reload(); |
| 95 | }); |
| 96 | gulp.watch(options.src + '**/*.html', function(){ |
| 97 | browserSync.reload(); |
| 98 | }); |
| 99 | gulp.watch(options.css + '**/*.css', function(){ |
| 100 | browserSync.reload(); |
| 101 | }); |
| 102 | gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){ |
| 103 | browserSync.reload(); |
| 104 | }); |
| 105 | |
| 106 | gulp.watch([ |
| 107 | options.helpers + 'ngXosHelpers.js', |
| 108 | options.static + '../../static/xosNgLib.css' |
| 109 | ], function(){ |
| 110 | browserSync.reload(); |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | // compile sass |
| 115 | gulp.task('sass', function () { |
| 116 | return gulp.src(`${options.sass}/**/*.scss`) |
| 117 | .pipe(sass().on('error', sass.logError)) |
| 118 | .pipe(gulp.dest(options.css)); |
| 119 | }); |
| 120 | |
| 121 | // transpile js with sourceMaps |
| 122 | gulp.task('babel', function(){ |
| 123 | return gulp.src(options.scripts + '**/*.js') |
| 124 | .pipe(babel({sourceMaps: true})) |
| 125 | .pipe(gulp.dest(options.tmp)); |
| 126 | }); |
| 127 | |
| 128 | // inject scripts |
| 129 | gulp.task('injectScript', ['cleanTmp', 'babel'], function(){ |
| 130 | return gulp.src(options.src + 'index.html') |
| 131 | .pipe( |
| 132 | inject( |
| 133 | gulp.src([ |
| 134 | options.tmp + '**/*.js', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 135 | options.helpers + 'ngXosHelpers.min.js' |
Matteo Scandolo | 7d539d3 | 2016-05-25 08:53:11 -0700 | [diff] [blame] | 136 | ]) |
| 137 | .pipe(angularFilesort()), |
| 138 | { |
| 139 | ignorePath: [options.src, '/../../ngXosLib'] |
| 140 | } |
| 141 | ) |
| 142 | ) |
| 143 | .pipe(gulp.dest(options.src)); |
| 144 | }); |
| 145 | |
| 146 | // inject CSS |
| 147 | gulp.task('injectCss', function(){ |
| 148 | return gulp.src(options.src + 'index.html') |
| 149 | .pipe( |
| 150 | inject( |
| 151 | gulp.src([ |
| 152 | options.src + 'css/*.css', |
| 153 | options.static + '../../static/xosNgLib.css' |
| 154 | ]), |
| 155 | { |
| 156 | ignorePath: [options.src] |
| 157 | } |
| 158 | ) |
| 159 | ) |
| 160 | .pipe(gulp.dest(options.src)); |
| 161 | }); |
| 162 | |
| 163 | // inject bower dependencies with wiredep |
| 164 | gulp.task('bower', function () { |
| 165 | return gulp.src(options.src + 'index.html') |
| 166 | .pipe(wiredep({devDependencies: true})) |
| 167 | .pipe(gulp.dest(options.src)); |
| 168 | }); |
| 169 | |
| 170 | gulp.task('js-watch', ['injectScript'], function(){ |
| 171 | browserSync.reload(); |
| 172 | }); |
| 173 | |
| 174 | gulp.task('cleanTmp', function(){ |
| 175 | return del([options.tmp + '**/*']); |
| 176 | }); |
| 177 | |
| 178 | gulp.task('serve', function() { |
| 179 | runSequence( |
| 180 | 'sass', |
| 181 | 'bower', |
| 182 | 'injectScript', |
| 183 | 'injectCss', |
| 184 | ['browser'] |
| 185 | ); |
| 186 | }); |
| 187 | }; |