blob: ad7c6817f864801720b60715c46b5267d52c8813 [file] [log] [blame]
Matteo Scandolof813b6a2015-11-03 14:32:00 +01001'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');
Matteo Scandolo8cbfa642015-11-04 11:31:05 +010011var del = require('del');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010012
13var proxy = httpProxy.createProxyServer({
14 target: 'http://0.0.0.0:9999'
15});
16
17
18proxy.on('error', function(error, req, res) {
19 res.writeHead(500, {
20 'Content-Type': 'text/plain'
21 });
22
23 console.error('[Proxy]', error);
24});
25
26module.exports = function(options){
27
28 // open in browser with sync and proxy to 0.0.0.0
29 gulp.task('browser', function() {
30 browserSync.init({
Matteo Scandolo8cbfa642015-11-04 11:31:05 +010031 // reloadDelay: 500,
32 // logLevel: 'debug',
33 // logConnections: true,
34 snippetOptions: {
35 rule: {
36 match: /<!-- browserSync -->/i
37 }
38 },
Matteo Scandolof813b6a2015-11-03 14:32:00 +010039 server: {
40 baseDir: options.src,
41 routes: {
Matteo Scandolob0238d32015-11-04 16:03:59 +010042 '/api': options.api,
Matteo Scandolo6328d9b2015-11-05 16:02:11 +010043 '/xosHelpers/src': options.helpers
Matteo Scandolof813b6a2015-11-03 14:32:00 +010044 },
45 middleware: function(req, res, next){
Matteo Scandoloc8b95322015-11-06 09:34:03 +010046 if(
47 req.url.indexOf('/xos/') !== -1 ||
48 req.url.indexOf('/xoslib/') !== -1 ||
49 req.url.indexOf('/hpcapi/') !== -1
50 ){
Matteo Scandolof813b6a2015-11-03 14:32:00 +010051 proxy.web(req, res);
52 }
53 else{
54 next();
55 }
56 }
57 }
58 });
59
Matteo Scandolo93487772015-11-04 15:15:17 +010060 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
61 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
62 browserSync.reload();
63 });
Matteo Scandolo4ea1cdb2015-11-04 11:34:44 +010064 gulp.watch(options.src + '**/*.html', function(){
65 browserSync.reload();
66 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +010067 });
68
69 // transpile js with sourceMaps
70 gulp.task('babel', function(){
71 return gulp.src(options.scripts + '**/*.js')
72 .pipe(babel({sourceMaps: true}))
73 .pipe(gulp.dest(options.tmp));
74 });
75
76 // inject scripts
Matteo Scandoloc7794962015-11-06 10:35:20 +010077 gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
Matteo Scandolof813b6a2015-11-03 14:32:00 +010078 return gulp.src(options.src + 'index.html')
79 .pipe(
80 inject(
81 gulp.src([
82 options.tmp + '**/*.js',
Matteo Scandolob0238d32015-11-04 16:03:59 +010083 options.api + '*.js',
84 options.helpers + '**/*.js'
Matteo Scandolof813b6a2015-11-03 14:32:00 +010085 ])
86 .pipe(angularFilesort()),
87 {
88 ignorePath: [options.src, '/../../ngXosLib']
89 }
90 )
91 )
92 .pipe(gulp.dest(options.src));
93 });
94
Matteo Scandoloc7794962015-11-06 10:35:20 +010095 // inject CSS
96 gulp.task('injectCss', function(){
97 return gulp.src(options.src + 'index.html')
98 .pipe(
99 inject(
100 gulp.src(options.src + 'css/*.css'),
101 {
102 ignorePath: [options.src]
103 }
104 )
105 )
106 .pipe(gulp.dest(options.src));
107 });
108
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100109 // inject bower dependencies with wiredep
110 gulp.task('bower', function () {
Matteo Scandolo352b6012015-11-04 15:07:36 +0100111 return gulp.src(options.src + 'index.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100112 .pipe(wiredep({devDependencies: true}))
113 .pipe(gulp.dest(options.src));
114 });
115
Matteo Scandoloab91f432015-11-06 18:25:52 +0100116 gulp.task('js-watch', ['injectScript'], function(){
Matteo Scandolo8cbfa642015-11-04 11:31:05 +0100117 browserSync.reload();
118 });
119
120 gulp.task('cleanTmp', function(){
121 return del([options.tmp + '**/*']);
122 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100123
124 gulp.task('serve', function() {
125 runSequence(
126 'bower',
Matteo Scandoloc7794962015-11-06 10:35:20 +0100127 'injectScript',
128 'injectCss',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100129 ['browser']
130 );
131 });
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100132};