blob: 62fe41293b16bd32f8a741fb01b829e6a0c08dd2 [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: {
42 '/api': options.api
43 },
44 middleware: function(req, res, next){
45 if(req.url.indexOf('no_hyperlinks') !== -1){
46 proxy.web(req, res);
47 }
48 else{
49 next();
50 }
51 }
52 }
53 });
54
Matteo Scandolo4ea1cdb2015-11-04 11:34:44 +010055 gulp.watch(options.src + '**/*.js', ['js-watch']);
56 gulp.watch(options.src + '**/*.html', function(){
57 browserSync.reload();
58 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +010059 });
60
61 // transpile js with sourceMaps
62 gulp.task('babel', function(){
63 return gulp.src(options.scripts + '**/*.js')
64 .pipe(babel({sourceMaps: true}))
65 .pipe(gulp.dest(options.tmp));
66 });
67
68 // inject scripts
Matteo Scandolo8cbfa642015-11-04 11:31:05 +010069 gulp.task('inject', ['cleanTmp', 'babel'],function(){
Matteo Scandolof813b6a2015-11-03 14:32:00 +010070 return gulp.src(options.src + 'index.html')
71 .pipe(
72 inject(
73 gulp.src([
74 options.tmp + '**/*.js',
75 options.api + '*.js'
76 ])
77 .pipe(angularFilesort()),
78 {
79 ignorePath: [options.src, '/../../ngXosLib']
80 }
81 )
82 )
83 .pipe(gulp.dest(options.src));
84 });
85
86 // inject bower dependencies with wiredep
87 gulp.task('bower', function () {
88 gulp.src(options.src + 'index.html')
89 .pipe(wiredep({devDependencies: true}))
90 .pipe(gulp.dest(options.src));
91 });
92
Matteo Scandolo8cbfa642015-11-04 11:31:05 +010093 gulp.task('js-watch', ['inject'], function(){
94 browserSync.reload();
95 });
96
97 gulp.task('cleanTmp', function(){
98 return del([options.tmp + '**/*']);
99 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100100
101 gulp.task('serve', function() {
102 runSequence(
103 'bower',
104 'inject',
105 ['browser']
106 );
107 });
108}