blob: 04b8e83dcfcad438ade7e290f25720d3b31084bf [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo6d972642015-10-29 15:50:15 +010019'use strict';
20
21var gulp = require('gulp');
22var browserSync = require('browser-sync').create();
23var inject = require('gulp-inject');
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010024var runSequence = require('run-sequence');
25var angularFilesort = require('gulp-angular-filesort');
26var babel = require('gulp-babel');
Matteo Scandolo45778962015-10-29 16:17:59 +010027var wiredep = require('wiredep').stream;
Matteo Scandolod4f501c2015-10-29 17:24:18 +010028var httpProxy = require('http-proxy');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010029var del = require('del');
Matteo Scandolo97532ef2016-05-17 17:12:03 -070030var sass = require('gulp-sass');
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070031var fs = require('fs');
32var path = require('path');
Matteo Scandolo6d972642015-10-29 15:50:15 +010033
Matteo Scandolo7dea2432016-03-24 15:11:29 -070034const environment = process.env.NODE_ENV;
35
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070036if(!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 Scandolo7dea2432016-03-24 15:11:29 -070043}
44
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070045var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
46
Matteo Scandolod4f501c2015-10-29 17:24:18 +010047var proxy = httpProxy.createProxyServer({
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070048 target: conf.host
Matteo Scandolod4f501c2015-10-29 17:24:18 +010049});
50
51
52proxy.on('error', function(error, req, res) {
53 res.writeHead(500, {
54 'Content-Type': 'text/plain'
Matteo Scandolo6d972642015-10-29 15:50:15 +010055 });
56
Matteo Scandolod4f501c2015-10-29 17:24:18 +010057 console.error('[Proxy]', error);
58});
59
60module.exports = function(options){
61
Matteo Scandolod4f501c2015-10-29 17:24:18 +010062 gulp.task('browser', function() {
63 browserSync.init({
Matteo Scandolo9a607262015-11-10 17:13:04 +010064 startPath: '#/',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010065 snippetOptions: {
66 rule: {
67 match: /<!-- browserSync -->/i
68 }
69 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010070 server: {
71 baseDir: options.src,
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010072 routes: {
Matteo Scandolo97532ef2016-05-17 17:12:03 -070073 '/xos/core/static': options.static + '../../static/'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010074 },
Matteo Scandolod4f501c2015-10-29 17:24:18 +010075 middleware: function(req, res, next){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010076 if(
Matteo Scandolo195dde92016-07-25 16:43:16 -070077 req.url.indexOf('/hpcapi/') !== -1 ||
78 req.url.indexOf('/api/') !== -1
Matteo Scandolod44ceb42015-11-06 17:14:28 +010079 ){
Matteo Scandolo7dea2432016-03-24 15:11:29 -070080 if(conf.xoscsrftoken && conf.xossessionid){
81 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
82 req.headers['x-csrftoken'] = conf.xoscsrftoken;
83 }
Matteo Scandolod4f501c2015-10-29 17:24:18 +010084 proxy.web(req, res);
85 }
86 else{
87 next();
88 }
89 }
90 }
91 });
92
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010093 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 Scandolo97532ef2016-05-17 17:12:03 -0700100 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 Scandolod4f501c2015-10-29 17:24:18 +0100120 });
121
122 // transpile js with sourceMaps
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100123 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 Scandolod4f501c2015-10-29 17:24:18 +0100129 // inject scripts
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100130 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100131 return gulp.src(options.src + 'index.html')
132 .pipe(
133 inject(
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100134 gulp.src([
135 options.tmp + '**/*.js',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700136 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100137 ])
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100138 .pipe(angularFilesort()),
139 {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100140 ignorePath: [options.src, '/../../ngXosLib']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100141 }
142 )
143 )
Matteo Scandolo45778962015-10-29 16:17:59 +0100144 .pipe(gulp.dest(options.src));
Matteo Scandolo6d972642015-10-29 15:50:15 +0100145 });
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100146
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100147 // inject CSS
148 gulp.task('injectCss', function(){
149 return gulp.src(options.src + 'index.html')
150 .pipe(
151 inject(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700152 gulp.src([
153 options.src + 'css/*.css',
154 options.static + '../../static/xosNgLib.css'
155 ]),
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100156 {
157 ignorePath: [options.src]
158 }
159 )
160 )
161 .pipe(gulp.dest(options.src));
162 });
163
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100164 // inject bower dependencies with wiredep
Matteo Scandolo45778962015-10-29 16:17:59 +0100165 gulp.task('bower', function () {
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100166 return gulp.src(options.src + 'index.html')
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100167 .pipe(wiredep({devDependencies: true}))
Matteo Scandolo45778962015-10-29 16:17:59 +0100168 .pipe(gulp.dest(options.src));
Matteo Scandolod4f501c2015-10-29 17:24:18 +0100169 });
170
Matteo Scandolo7db08432015-11-06 18:49:33 +0100171 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100172 browserSync.reload();
173 });
174
175 gulp.task('cleanTmp', function(){
176 return del([options.tmp + '**/*']);
177 });
Matteo Scandolo45778962015-10-29 16:17:59 +0100178
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100179 gulp.task('serve', function() {
180 runSequence(
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700181 'sass',
Matteo Scandolo45778962015-10-29 16:17:59 +0100182 'bower',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100183 'injectScript',
184 'injectCss',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100185 ['browser']
Matteo Scandoloc2d0f532015-10-29 16:13:35 +0100186 );
187 });
Matteo Scandolo97532ef2016-05-17 17:12:03 -0700188};