blob: d31c1150d05fefabf808e79a02c8b5b842ed732b [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 Scandolodf35ca92016-02-25 09:19:41 -080019'use strict';
20
21var gulp = require('gulp');
22var browserSync = require('browser-sync').create();
23var inject = require('gulp-inject');
24var runSequence = require('run-sequence');
25var angularFilesort = require('gulp-angular-filesort');
26var babel = require('gulp-babel');
27var wiredep = require('wiredep').stream;
28var httpProxy = require('http-proxy');
29var del = require('del');
Matteo Scandolo1778b742016-04-22 09:47:50 -070030var sass = require('gulp-sass');
Matteo Scandolo195dde92016-07-25 16:43:16 -070031var fs = require('fs');
32var path = require('path');
Matteo Scandolodf35ca92016-02-25 09:19:41 -080033
34const environment = process.env.NODE_ENV;
35
Matteo Scandolo195dde92016-07-25 16:43:16 -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 Scandolodf35ca92016-02-25 09:19:41 -080043}
44
Matteo Scandolo195dde92016-07-25 16:43:16 -070045var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
46
Matteo Scandolodf35ca92016-02-25 09:19:41 -080047var proxy = httpProxy.createProxyServer({
Matteo Scandolo195dde92016-07-25 16:43:16 -070048 target: conf.host
Matteo Scandolodf35ca92016-02-25 09:19:41 -080049});
50
51
52proxy.on('error', function(error, req, res) {
53 res.writeHead(500, {
54 'Content-Type': 'text/plain'
55 });
56
57 console.error('[Proxy]', error);
58});
59
60module.exports = function(options){
61
Matteo Scandolodf35ca92016-02-25 09:19:41 -080062 gulp.task('browser', function() {
63 browserSync.init({
Matteo Scandolodf35ca92016-02-25 09:19:41 -080064 startPath: '#/',
65 snippetOptions: {
66 rule: {
67 match: /<!-- browserSync -->/i
68 }
69 },
70 server: {
71 baseDir: options.src,
72 routes: {
Matteo Scandolo195dde92016-07-25 16:43:16 -070073 '/xos/core/static': options.static + '../../static/'
Matteo Scandolodf35ca92016-02-25 09:19:41 -080074 },
75 middleware: function(req, res, next){
76 if(
Matteo Scandolo1778b742016-04-22 09:47:50 -070077 req.url.indexOf('/api/') !== -1
Matteo Scandolodf35ca92016-02-25 09:19:41 -080078 ){
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 Scandolo1778b742016-04-22 09:47:50 -070085 else if(req.url.indexOf('/videoLocal.txt') !== -1){
86 let a = (Math.random() * 10).toString();
87 res.end(a)
Matteo Scandolo97646d62016-03-07 14:14:00 -080088 }
Matteo Scandolodf35ca92016-02-25 09:19:41 -080089 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 Scandolo1778b742016-04-22 09:47:50 -0700103 gulp.watch(options.css + '**/*.css', function(){
104 browserSync.reload();
105 });
106 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
107 browserSync.reload();
108 });
Matteo Scandolo195dde92016-07-25 16:43:16 -0700109
110 gulp.watch([
111 options.helpers + 'ngXosHelpers.js',
112 options.static + '../../static/xosNgLib.css'
113 ], function(){
114 browserSync.reload();
115 });
Matteo Scandolo1778b742016-04-22 09:47:50 -0700116 });
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 Scandolodf35ca92016-02-25 09:19:41 -0800123 });
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 Scandolo195dde92016-07-25 16:43:16 -0700139 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800140 ])
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 Scandolo195dde92016-07-25 16:43:16 -0700155 gulp.src([
156 options.src + 'css/*.css',
157 options.static + '../../static/xosNgLib.css'
158 ]),
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800159 {
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 Scandolo1778b742016-04-22 09:47:50 -0700184 'sass',
Matteo Scandolodf35ca92016-02-25 09:19:41 -0800185 'bower',
186 'injectScript',
187 'injectCss',
188 ['browser']
189 );
190 });
191};