blob: d386289758f424c6b1f00ade1b866a44e533e2b8 [file] [log] [blame]
Rizwan Haider8e5f4772016-08-17 18:04:35 -04001'use strict';
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var inject = require('gulp-inject');
6var runSequence = require('run-sequence');
7var angularFilesort = require('gulp-angular-filesort');
8var babel = require('gulp-babel');
9var wiredep = require('wiredep').stream;
10var httpProxy = require('http-proxy');
11var del = require('del');
12var sass = require('gulp-sass');
13var fs = require('fs');
14var path = require('path');
15
16const environment = process.env.NODE_ENV;
17
18if(!fs.existsSync(path.join(__dirname, `../../../env/${environment || 'default'}.js`))){
19 if(!environment){
20 throw new Error('You should define a default.js config in /views/env folder.');
21 }
22 else{
23 throw new Error(`Since you are loading a custom environment, you should define a ${environment}.js config in /views/env folder.`);
24 }
25}
26
27var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
29var proxy = httpProxy.createProxyServer({
30 target: conf.host
31});
32
33
34proxy.on('error', function(error, req, res) {
35 res.writeHead(500, {
36 'Content-Type': 'text/plain'
37 });
38
39 console.error('[Proxy]', error);
40});
41
42module.exports = function(options){
43
44 gulp.task('browser', function() {
45 browserSync.init({
46 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
55 '/xos/core/xoslib/static/js/vendor': options.helpers,
56 '/xos/core/static': options.static + '../../static/'
57 },
58 middleware: function(req, res, next){
59 if(
60 req.url.indexOf('/api/') !== -1 ||
61 req.url.indexOf('/onos/') !== -1
62 ){
63 if(conf.xoscsrftoken && conf.xossessionid){
64 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
65 req.headers['x-csrftoken'] = conf.xoscsrftoken;
66 req.headers['Authorization'] = "Basic cGFkbWluQHZpY2NpLm9yZzpsZXRtZWlu";
67 }
68 proxy.web(req, res);
69 }
70 else{
71 next();
72 }
73 }
74 }
75 });
76
77 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
78 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
79 browserSync.reload();
80 });
81 gulp.watch(options.src + '**/*.html', function(){
82 browserSync.reload();
83 });
84 gulp.watch(options.css + '**/*.css', function(){
85 browserSync.reload();
86 });
87 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
88 browserSync.reload();
89 });
90
91 gulp.watch([
92 options.helpers + 'ngXosHelpers.js',
93 options.static + '../../static/xosNgLib.css'
94 ], function(){
95 browserSync.reload();
96 });
97 });
98
99 // compile sass
100 gulp.task('sass', function () {
101 return gulp.src(`${options.sass}/**/*.scss`)
102 .pipe(sass().on('error', sass.logError))
103 .pipe(gulp.dest(options.css));
104 });
105
106 // transpile js with sourceMaps
107 gulp.task('babel', function(){
108 return gulp.src(options.scripts + '**/*.js')
109 .pipe(babel({sourceMaps: true}))
110 .pipe(gulp.dest(options.tmp));
111 });
112
113 // inject scripts
114 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
115 return gulp.src(options.src + 'index.html')
116 .pipe(
117 inject(
118 gulp.src([
119 options.tmp + '**/*.js',
120 options.helpers + 'ngXosHelpers.js'
121 ])
122 .pipe(angularFilesort()),
123 {
124 ignorePath: [options.src, '/../../ngXosLib']
125 }
126 )
127 )
128 .pipe(gulp.dest(options.src));
129 });
130
131 // inject CSS
132 gulp.task('injectCss', function(){
133 return gulp.src(options.src + 'index.html')
134 .pipe(
135 inject(
136 gulp.src([
137 options.src + 'css/*.css',
138 options.static + '../../static/xosNgLib.css'
139 ]),
140 {
141 ignorePath: [options.src]
142 }
143 )
144 )
145 .pipe(gulp.dest(options.src));
146 });
147
148 // inject bower dependencies with wiredep
149 gulp.task('bower', function () {
150 return gulp.src(options.src + 'index.html')
151 .pipe(wiredep({devDependencies: true}))
152 .pipe(gulp.dest(options.src));
153 });
154
155 gulp.task('js-watch', ['injectScript'], function(){
156 browserSync.reload();
157 });
158
159 gulp.task('cleanTmp', function(){
160 return del([options.tmp + '**/*']);
161 });
162
163 gulp.task('serve', function() {
164 runSequence(
165 'sass',
166 'bower',
167 'injectScript',
168 'injectCss',
169 ['browser']
170 );
171 });
172};