blob: 0f690643f58f169b0e9015b395388a012def08aa [file] [log] [blame]
Matteo Scandolo46b56102015-12-16 14:23:08 -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 del = require('del');
Matteo Scandolo8942e042015-12-16 14:41:21 -080011var httpProxy = require('http-proxy');
12
13const environment = process.env.NODE_ENV;
14
15if (environment){
16 var conf = require(`../env/${environment}.js`);
17}
18else{
19 var conf = require('../env/default.js')
20}
21
22console.log(conf);
23
24var proxy = httpProxy.createProxyServer({
25 target: conf.host || 'http://0.0.0.0:9999'
26});
27
28proxy.on('error', function(error, req, res) {
29 res.writeHead(500, {
30 'Content-Type': 'text/plain'
31 });
32
33 console.error('[Proxy]', error);
34});
Matteo Scandolo46b56102015-12-16 14:23:08 -080035
36module.exports = function(options){
37
38 // open in browser with sync and proxy to 0.0.0.0
39 gulp.task('browser', function() {
40 browserSync.init({
41 // reloadDelay: 500,
42 // logLevel: 'debug',
43 // logConnections: true,
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080044 // directory: true,
Matteo Scandolo46b56102015-12-16 14:23:08 -080045 startPath: '#/',
46 snippetOptions: {
47 rule: {
48 match: /<!-- browserSync -->/i
49 }
50 },
51 server: {
52 baseDir: options.src,
Matteo Scandolo8942e042015-12-16 14:41:21 -080053 middleware: function(req, res, next){
54 if(
55 req.url.indexOf('autoscaledata') !== -1
56 ){
57 proxy.web(req, res);
58 }
59 else{
60 next();
61 }
62 }
Matteo Scandolo46b56102015-12-16 14:23:08 -080063 }
64 });
65
66 gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
67
68 gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
69 browserSync.reload();
70 });
71 gulp.watch(options.src + '**/*.html', function(){
72 browserSync.reload();
73 });
74 gulp.watch(options.src + '**/*.css', function(){
75 browserSync.reload();
76 });
77 });
78
79 // transpile js with sourceMaps
80 gulp.task('babel', function(){
81 return gulp.src([options.scripts + '**/*.js'])
82 .pipe(babel({sourceMaps: true}))
83 .pipe(gulp.dest(options.tmp));
84 });
85
86 // inject scripts
87 gulp.task('injectScript', function(){
Matteo Scandolo46b56102015-12-16 14:23:08 -080088 runSequence(
89 'cleanTmp',
90 'babel',
91 function() {
92 return gulp.src(options.src + 'index.html')
93 .pipe(
94 inject(
95 gulp.src([
96 options.tmp + '**/*.js',
97 options.api + '*.js',
98 options.helpers + '**/*.js'
99 ])
100 // .pipe(debug({title: 'unicorn:'}))
101 .pipe(angularFilesort()),
102 {
103 ignorePath: [options.src, '/../../ngXosLib']
104 }
105 )
106 )
107 .pipe(gulp.dest(options.src));
108 }
109 );
110 });
111
112 // inject CSS
113 gulp.task('injectCss', function(){
114 return gulp.src(options.src + 'index.html')
115 .pipe(
116 inject(
117 gulp.src(options.src + 'css/*.css'),
118 {
119 ignorePath: [options.src]
120 }
121 )
122 )
123 .pipe(gulp.dest(options.src));
124 });
125
126 // inject bower dependencies with wiredep
127 gulp.task('bower', function () {
128 return gulp.src(options.src + 'index.html')
129 .pipe(wiredep({devDependencies: true}))
130 .pipe(gulp.dest(options.src));
131 });
132
133 gulp.task('js-watch', ['injectScript'], function(){
134 browserSync.reload();
135 });
136
137 gulp.task('cleanTmp', function(){
138 return del([options.tmp + '**/*']);
139 });
140
141 gulp.task('serve', function() {
142 runSequence(
143 'bower',
144 'injectScript',
145 'injectCss',
146 ['browser']
147 );
148 });
149};