blob: fbc605be8f3bec268cd727bd303a04ca4c3c8c73 [file] [log] [blame]
Matteo Scandolo8a64fa42016-01-21 11:21:03 -08001'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');
Matteo Scandolo1df21732016-03-29 14:06:13 -070012var sass = require('gulp-sass');
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070013var fs = require('fs');
14var path = require('path');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080015
16const environment = process.env.NODE_ENV;
17
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070018if(!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 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080025}
26
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070027var conf = require(path.join(__dirname, `../../../env/${environment || 'default'}.js`));
28
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080029var proxy = httpProxy.createProxyServer({
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070030 target: conf.host
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080031});
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
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080044 gulp.task('browser', function() {
45 browserSync.init({
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080046 startPath: '#/',
47 snippetOptions: {
48 rule: {
49 match: /<!-- browserSync -->/i
50 }
51 },
52 server: {
53 baseDir: options.src,
54 routes: {
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070055 '/xos/core/xoslib/static/js/vendor': options.helpers,
56 '/xos/core/static': options.static + '../../static/'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080057 },
58 middleware: function(req, res, next){
59 if(
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070060 req.url.indexOf('/?no_hyperlinks') !== -1 ||
61 req.url.indexOf('/api/') !== -1
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080062 ){
63 if(conf.xoscsrftoken && conf.xossessionid){
64 req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
65 req.headers['x-csrftoken'] = conf.xoscsrftoken;
66 }
67 proxy.web(req, res);
68 }
69 else{
70 next();
71 }
72 }
73 }
74 });
75
76 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
77 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
78 browserSync.reload();
79 });
80 gulp.watch(options.src + '**/*.html', function(){
81 browserSync.reload();
82 });
Matteo Scandolo1df21732016-03-29 14:06:13 -070083 gulp.watch(options.css + '**/*.css', function(){
84 browserSync.reload();
85 });
86 gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
87 browserSync.reload();
88 });
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070089
90 gulp.watch([
91 options.helpers + 'ngXosHelpers.js',
92 options.static + '../../static/xosNgLib.css'
93 ], function(){
94 browserSync.reload();
95 });
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080096 });
97
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070098 // compile sass
Matteo Scandolo1df21732016-03-29 14:06:13 -070099 gulp.task('sass', function () {
100 return gulp.src(`${options.sass}/**/*.scss`)
101 .pipe(sass().on('error', sass.logError))
102 .pipe(gulp.dest(options.css));
103 });
Matteo Scandolo1df21732016-03-29 14:06:13 -0700104
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800105 // transpile js with sourceMaps
106 gulp.task('babel', function(){
107 return gulp.src(options.scripts + '**/*.js')
108 .pipe(babel({sourceMaps: true}))
109 .pipe(gulp.dest(options.tmp));
110 });
111
112 // inject scripts
113 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
114 return gulp.src(options.src + 'index.html')
115 .pipe(
116 inject(
117 gulp.src([
118 options.tmp + '**/*.js',
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -0700119 options.helpers + 'ngXosHelpers.js'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800120 ])
121 .pipe(angularFilesort()),
122 {
123 ignorePath: [options.src, '/../../ngXosLib']
124 }
125 )
126 )
127 .pipe(gulp.dest(options.src));
128 });
129
130 // inject CSS
131 gulp.task('injectCss', function(){
132 return gulp.src(options.src + 'index.html')
133 .pipe(
134 inject(
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -0700135 gulp.src([
136 options.src + 'css/*.css',
137 options.static + '../../static/xosNgLib.css'
138 ]),
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800139 {
140 ignorePath: [options.src]
141 }
142 )
143 )
144 .pipe(gulp.dest(options.src));
145 });
146
147 // inject bower dependencies with wiredep
148 gulp.task('bower', function () {
149 return gulp.src(options.src + 'index.html')
150 .pipe(wiredep({devDependencies: true}))
151 .pipe(gulp.dest(options.src));
152 });
153
154 gulp.task('js-watch', ['injectScript'], function(){
155 browserSync.reload();
156 });
157
158 gulp.task('cleanTmp', function(){
159 return del([options.tmp + '**/*']);
160 });
161
162 gulp.task('serve', function() {
163 runSequence(
Matteo Scandolo1df21732016-03-29 14:06:13 -0700164 'sass',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800165 'bower',
166 'injectScript',
167 'injectCss',
168 ['browser']
169 );
170 });
171};