{"size": 2394, "ext": "js", "max_stars_repo_path": "tasks/server.js", "max_stars_repo_name": "corballis/corballis-build", "max_stars_repo_stars_event_min_datetime": "2015-10-22T15:41:56.000Z", "max_stars_repo_stars_event_max_datetime": "2016-11-28T12:04:18.000Z", "max_issues_repo_path": "tasks/server.js", "max_issues_repo_name": "corballis/corballis-build", "max_issues_repo_issues_event_min_datetime": "2015-11-15T19:08:40.000Z", "max_issues_repo_issues_event_max_datetime": "2015-11-25T12:50:54.000Z", "max_forks_repo_path": "tasks/server.js", "max_forks_repo_name": "corballis/corballis-build", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 33.7183098592, "max_line_length": 120, "alphanum_fraction": 0.6675020886}
'use strict';

var path = require('path');
var gulp = require('gulp');
var config = require('../config');
var browserSync = require('browser-sync');
var browserSyncSpa = require('browser-sync-spa');
var util = require('util');
var proxyMiddleware = require('http-proxy-middleware');

function browserSyncInit(baseDir, browser, serverPort, clientPort) {
  browser = browser === undefined ? 'default' : browser;
  serverPort = serverPort === undefined ? 8080 : serverPort;
  clientPort = clientPort === undefined ? 3000 : clientPort;

  var routes = null;
  if (baseDir === config.paths.app || (util.isArray(baseDir) && baseDir.indexOf(config.paths.app) !== -1)) {
    routes = {
      '/bower_components': 'bower_components',
      '/fonts': 'bower_components/font-awesome/fonts',
      '/fonts/bootstrap': 'bower_components/bootstrap-sass/assets/fonts/bootstrap'
    };
  }

  var springServer = config.proxy.springHost + ':' + serverPort;

  var server = {
    baseDir: baseDir,
    routes: routes,
    middleware: [
      proxyMiddleware('/api', {target: springServer, changeOrigin: true}),
      proxyMiddleware('/info', {target: springServer, changeOrigin: true}),
      // glob patterns do not support 'ends with', so we need to add the [] at the end, otherwise login.html would match
      proxyMiddleware('/logi[n]', {target: springServer, changeOrigin: true}),
      proxyMiddleware('/logout', {target: springServer, changeOrigin: true})
    ]
  };

  browserSync.instance = browserSync.init({
    port: clientPort,
    startPath: '/',
    server: server,
    browser: browser,
    open: false
  });
}

browserSync.use(browserSyncSpa({
  selector: '[data-ng-app]'
}));

gulp.task('serve', ['clean', 'watch'], function () {
  browserSyncInit([path.join(config.paths.tmp, config.paths.serve), config.paths.app]);
});

gulp.task('serve:dist', ['clean', 'build'], function () {
  browserSyncInit(config.paths.dist);
});

gulp.task('serve:e2e', ['clean', 'watch'], function () {
  browserSyncInit([path.join(config.paths.tmp, config.paths.serve), config.paths.app], [], 8081, 3001);
});

gulp.task('serve:e2e-single', ['clean', 'ts-compile-emit', 'inject'], function() {
  browserSyncInit([path.join(config.paths.tmp, config.paths.serve), config.paths.app], [], 8081, 3001);
});

gulp.task('serve:e2e-dist', ['build'], function () {
  browserSyncInit(config.paths.dist, [], 8081, 3001);
});
