text: 'use strict';
  
  var path = require('path');
  var gulp = require('gulp');
  var conf = require('./conf');
  var url = require('url');
  var browserSync = require('browser-sync');
  var browserSyncSpa = require('browser-sync-spa');
  
  var util = require('util');
  
  var proxy = require('proxy-middleware');
  
  function browserSyncInit(baseDir, browser) {
    browser = browser === undefined ? 'default' : browser;
  
    var routes = null;
    if(baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
      routes = {
        '/bower_components': 'bower_components'
      };
    }
  
      var endsWith = function(str, suffix) {
          return str.indexOf('/', str.length - suffix.length) !== -1;
      }
  
    /*
     * You can add a proxy to your backend by uncommenting the line below.
     * You just have to configure a context which will we redirected and the target url.
     * Example: $http.get('/users') requests will be automatically proxified.
     *
     * For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.9.0/README.md
     */
    // server.middleware = proxyMiddleware('/users', {target: 'http://jsonplaceholder.typicode.com', changeOrigin: true});
      var baseUri = conf.paths.uri + conf.paths.apiPort;
      // Routes to proxy to the backend. Routes ending with a / will setup
      // a redirect so that if accessed without a trailing slash, will
      // redirect. This is required for some endpoints for proxy-middleware
      // to correctly handle them.
  
  
      var requireTrailingSlash = conf.paths.proxyRoutes.filter(function (r) {
          return endsWith(r, '/');
      }).map(function (r) {
          // Strip trailing slash so we can use the route to match requests
          // with non trailing slash
          return r.substr(0, r.length - 1);
      });
  
      var proxies = [
          // Ensure trailing slash in routes that require it
          function (req, res, next) {
              requireTrailingSlash.forEach(function (route){
                  if (url.parse(req.url).path === route) {
                      res.statusCode = 301;
                      res.setHeader('Location', route + '/');
                      res.end();
                  }
              });
  
              next();
          }
      ]
          .concat(
              // Build a list of proxies for routes: [route1_proxy, route2_proxy, ...]
              conf.paths.proxyRoutes.map(function (r) {
                  var options = url.parse(baseUri + r);
                  options.route = r;
                  options.preserveHost = true;
                  return proxy(options);
              }));
      var server = {
          baseDir: baseDir,
          routes: routes,
          middleware: proxies
      };
    browserSync.instance = browserSync.init({
      port: conf.paths.port,
      startPath: '/',
      server: server,
      browser: browser,
      ghostMode: false
    });
  }
  
  browserSync.use(browserSyncSpa({
    selector: '[ng-app]'// Only needed for angular apps
  }));
  
  gulp.task('serve', ['watch'], function () {
    browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]);
  });
  
  gulp.task('serve:dist', ['build'], function () {
    browserSyncInit(conf.paths.dist);
  });
  
  gulp.task('serve:e2e', ['inject'], function () {
    browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []);
  });
  
  gulp.task('serve:e2e-dist', ['build'], function () {
    browserSyncInit(conf.paths.dist, []);
  });
  