{"size": 882, "ext": "js", "max_stars_repo_path": "gulp/tasks/browserSync.js", "max_stars_repo_name": "alpha-ville/bcn-iot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "gulp/tasks/browserSync.js", "max_issues_repo_name": "alpha-ville/bcn-iot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "gulp/tasks/browserSync.js", "max_forks_repo_name": "alpha-ville/bcn-iot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5625, "max_line_length": 112, "alphanum_fraction": 0.5396825397}
var browserSync = require('browser-sync');
var gulp        = require('gulp');
var fs          = require('fs');
var pkg         = require('../../package.json');

// gulp.task('browserSync', ['build'], function() {
//   browserSync.init(null, {
//     proxy: '0.0.0.0:3000'
//   });
// });

gulp.task('browserSync', ['build'], function() {
  browserSync.init([pkg.folders.dest+'/**'], {

    server: {
      baseDir: [pkg.folders.src, pkg.folders.dest],
      middleware: function (req, res, next) {

        var filePath = req.url.split("?");

        // static route for pushstate
        var exists = fs.existsSync(process.cwd() + "/" + pkg.folders.dest + filePath[0]);
        if((req.url == "/" || !exists) && req.url.indexOf("browser-sync-client") == -1) req.url = "/index.html";

        if(filePath.length > 1) req.url += filePath[1];

        next();
      }
    }
  });
});
