blob: f11a87504e1d56b97e8c4cfb317d99ea083556f1 [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
Rizwan Haider8e5f4772016-08-17 18:04:35 -040019'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');
30var sass = require('gulp-sass');
31var fs = require('fs');
32var path = require('path');
33
34const environment = process.env.NODE_ENV;
35
36if(!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
45var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
46
47var proxy = httpProxy.createProxyServer({
48 target: conf.host
49});
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
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: {
73 '/xos/core/xoslib/static/js/vendor': options.helpers,
74 '/xos/core/static': options.static + '../../static/'
75 },
76 middleware: function(req, res, next){
77 if(
78 req.url.indexOf('/api/') !== -1 ||
79 req.url.indexOf('/onos/') !== -1
80 ){
81 if(conf.xoscsrftoken && conf.xossessionid){
82 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
83 req.headers['x-csrftoken'] = conf.xoscsrftoken;
84 req.headers['Authorization'] = "Basic cGFkbWluQHZpY2NpLm9yZzpsZXRtZWlu";
85 }
86 proxy.web(req, res);
87 }
88 else{
89 next();
90 }
91 }
92 }
93 });
94
95 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
96 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
97 browserSync.reload();
98 });
99 gulp.watch(options.src + '**/*.html', function(){
100 browserSync.reload();
101 });
102 gulp.watch(options.css + '**/*.css', function(){
103 browserSync.reload();
104 });
105 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
106 browserSync.reload();
107 });
108
109 gulp.watch([
110 options.helpers + 'ngXosHelpers.js',
111 options.static + '../../static/xosNgLib.css'
112 ], function(){
113 browserSync.reload();
114 });
115 });
116
117 // compile sass
118 gulp.task('sass', function () {
119 return gulp.src(`${options.sass}/**/*.scss`)
120 .pipe(sass().on('error', sass.logError))
121 .pipe(gulp.dest(options.css));
122 });
123
124 // transpile js with sourceMaps
125 gulp.task('babel', function(){
126 return gulp.src(options.scripts + '**/*.js')
127 .pipe(babel({sourceMaps: true}))
128 .pipe(gulp.dest(options.tmp));
129 });
130
131 // inject scripts
132 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
133 return gulp.src(options.src + 'index.html')
134 .pipe(
135 inject(
136 gulp.src([
137 options.tmp + '**/*.js',
138 options.helpers + 'ngXosHelpers.js'
139 ])
140 .pipe(angularFilesort()),
141 {
142 ignorePath: [options.src, '/../../ngXosLib']
143 }
144 )
145 )
146 .pipe(gulp.dest(options.src));
147 });
148
149 // inject CSS
150 gulp.task('injectCss', function(){
151 return gulp.src(options.src + 'index.html')
152 .pipe(
153 inject(
154 gulp.src([
155 options.src + 'css/*.css',
156 options.static + '../../static/xosNgLib.css'
157 ]),
158 {
159 ignorePath: [options.src]
160 }
161 )
162 )
163 .pipe(gulp.dest(options.src));
164 });
165
166 // inject bower dependencies with wiredep
167 gulp.task('bower', function () {
168 return gulp.src(options.src + 'index.html')
169 .pipe(wiredep({devDependencies: true}))
170 .pipe(gulp.dest(options.src));
171 });
172
173 gulp.task('js-watch', ['injectScript'], function(){
174 browserSync.reload();
175 });
176
177 gulp.task('cleanTmp', function(){
178 return del([options.tmp + '**/*']);
179 });
180
181 gulp.task('serve', function() {
182 runSequence(
183 'sass',
184 'bower',
185 'injectScript',
186 'injectCss',
187 ['browser']
188 );
189 });
190};