blob: a1e725b45b7047f9116a31ae3473c2327999d3ff [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 Scandoloe53ee052015-11-03 14:32:00 +010019'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');
Matteo Scandoloce954e52015-11-04 11:31:05 +010029var del = require('del');
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070030var sass = require('gulp-sass');
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070031var fs = require('fs');
32var path = require('path');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010033
Matteo Scandolo7ed6c762015-11-30 17:25:39 -080034const 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 Scandolo9323b1b2015-11-25 12:08:41 -080043}
44
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070045var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
46
Matteo Scandoloe53ee052015-11-03 14:32:00 +010047var proxy = httpProxy.createProxyServer({
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070048 target: conf.host
Matteo Scandoloe53ee052015-11-03 14:32:00 +010049});
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 Scandoloe53ee052015-11-03 14:32:00 +010062 gulp.task('browser', function() {
63 browserSync.init({
Matteo Scandolof1072ed2015-11-09 11:25:40 +010064 startPath: '#/',
Matteo Scandoloce954e52015-11-04 11:31:05 +010065 snippetOptions: {
66 rule: {
67 match: /<!-- browserSync -->/i
68 }
69 },
Matteo Scandoloe53ee052015-11-03 14:32:00 +010070 server: {
71 baseDir: options.src,
72 routes: {
Matteo Scandoloa3839e32016-04-28 16:20:53 -070073 '/xos/core/static': options.static + '../../static/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010074 },
75 middleware: function(req, res, next){
Matteo Scandolo07760222015-11-06 09:34:03 +010076 if(
Matteo Scandolo8cc22e42016-04-12 09:00:04 -070077 req.url.indexOf('/api/') !== -1
Matteo Scandolo07760222015-11-06 09:34:03 +010078 ){
Matteo Scandolo9323b1b2015-11-25 12:08:41 -080079 if(conf.xoscsrftoken && conf.xossessionid){
80 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
81 req.headers['x-csrftoken'] = conf.xoscsrftoken;
82 }
Matteo Scandoloe53ee052015-11-03 14:32:00 +010083 proxy.web(req, res);
84 }
85 else{
86 next();
87 }
88 }
89 }
90 });
91
Matteo Scandolo0a0f5f32015-11-04 15:15:17 +010092 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
93 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
94 browserSync.reload();
95 });
Matteo Scandolo73a3c2b2015-11-04 11:34:44 +010096 gulp.watch(options.src + '**/*.html', function(){
97 browserSync.reload();
98 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070099 gulp.watch(options.css + '**/*.css', function(){
100 browserSync.reload();
101 });
102 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
103 browserSync.reload();
104 });
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700105
106 gulp.watch([
107 options.helpers + 'ngXosHelpers.js',
108 options.static + '../../static/xosNgLib.css'
109 ], function(){
110 browserSync.reload();
111 });
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700112 });
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));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100119 });
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
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100129 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100130 return gulp.src(options.src + 'index.html')
131 .pipe(
132 inject(
133 gulp.src([
134 options.tmp + '**/*.js',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700135 options.helpers + 'ngXosHelpers.min.js'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100136 ])
137 .pipe(angularFilesort()),
138 {
139 ignorePath: [options.src, '/../../ngXosLib']
140 }
141 )
142 )
143 .pipe(gulp.dest(options.src));
144 });
145
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100146 // inject CSS
147 gulp.task('injectCss', function(){
148 return gulp.src(options.src + 'index.html')
149 .pipe(
150 inject(
Matteo Scandoloa3839e32016-04-28 16:20:53 -0700151 gulp.src([
152 options.src + 'css/*.css',
153 options.static + '../../static/xosNgLib.css'
154 ]),
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100155 {
156 ignorePath: [options.src]
157 }
158 )
159 )
160 .pipe(gulp.dest(options.src));
161 });
162
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100163 // inject bower dependencies with wiredep
164 gulp.task('bower', function () {
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100165 return gulp.src(options.src + 'index.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100166 .pipe(wiredep({devDependencies: true}))
167 .pipe(gulp.dest(options.src));
168 });
169
Matteo Scandoloba5e19d2015-11-06 18:25:52 +0100170 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandoloce954e52015-11-04 11:31:05 +0100171 browserSync.reload();
172 });
173
174 gulp.task('cleanTmp', function(){
175 return del([options.tmp + '**/*']);
176 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100177
178 gulp.task('serve', function() {
179 runSequence(
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700180 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100181 'bower',
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100182 'injectScript',
183 'injectCss',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100184 ['browser']
185 );
186 });
Matteo Scandolo9323b1b2015-11-25 12:08:41 -0800187};