var settings = require('../conf/settings');
var path = require('path');
var _ = require('./util');
var dev_ip = require('dev-ip')()[0];
module.exports = function (newPath, port) {
  var browserSync = require('browser-sync').create();
  var bs = browserSync.init({
    port: settings.browserSyncPort,
    server: newPath,
    socket: {
      domain: '127.0.0.1:'+settings.browserSyncPort
    },
    files: [
      path.join(newPath, '**/*.css'),
      path.join(newPath, '**/*.png'),
      path.join(newPath, '**/*.jpg'),
      path.join(newPath, '**/*.js'),
      path.join(newPath, '*.html'),
    ],
    scriptPath: function (path) {
      return 'http://'+dev_ip + ":" + settings.browserSyncPort + path;
    },
    script: {
      domain: '127.0.0.1:' + settings.browserSyncPort
    },
    logFileChanges: true,
    logSnippet: false
  }, function (err) {
    if (err) {
      hoo.log.error(err);
      return;
    }
    var bsPort = bs.options.get('port');
    if (bsPort !== settings.browserSyncPort) {
      hoo.log.error("您的本机端口" + settings.browserSyncPort + "已被占用，hoo暂无法使用，请关掉相关进程后重试!");
      process.exit();
    }
  });

};
