<commit_before>const fs = require('../../core/fs.js');
var {canonicalHost} = require('../../utils/host.js');
var utils = require('../../utils/utils.js');
var Server = require('./server.js');
const opn = require('opn');

require('ejs');
const Templates = {
  embark_building_placeholder: require('./templates/embark-building-placeholder.html.ejs')
};

class WebServer {
  constructor(embark) {
    this.embark = embark;
    this.logger = embark.logger;
    this.events = embark.events;
    this.buildDir = embark.config.buildDir;
    this.webServerConfig = embark.config.webServerConfig;
    if (!this.webServerConfig.enabled) {
      return;
    }

    this.host = this.webServerConfig.host;
    this.port = parseInt(this.webServerConfig.port);
    this._openBrowser = this.webServerConfig.openBrowser;

    this.events.emit("status", __("Starting Server"));

    this.server = new Server({
      buildDir: this.buildDir,
      events: this.events,
      host: this.host,
      port: this.port
    });

    this.events.on('webserver:config:change', () => {
      this.embark.config.webServerConfig = null;
      this.embark.config.loadWebServerConfigFile();
      this.webServerConfig = this.embark.config.webServerConfig;
      this.host = this.webServerConfig.host;
      this.port = this.webServerConfig.port;
      this.server.host = this.host;
      this.server.port = this.port;

      this.testPort(() => {
        this.server.stop((_err) => {
          this.server.start((_err, message, port) => {
            this.logger.info(message);
            this.port = port;
            this.events.request('open-browser', () => {});
          });
        });
      });
    });

    this.testPort(() => {
      this.listenToCommands();
      this.registerConsoleCommands();
      this.server.start((_err, message, port) => {
        this.logger.info(message);
        this.port = port;
        this.setServiceCheck();
      });
    });
  }

  testPort(done) {
    utils.pingEndpoint(this.host, this.port, 'http', 'http', '', (err) => {
      if (err) { // Port is ok
        return done();<commit_msg>if port === 0 (per cli or config), auto-assign a port<commit_after>const fs = require('../../core/fs.js');
var {canonicalHost} = require('../../utils/host.js');
var utils = require('../../utils/utils.js');
var Server = require('./server.js');
const opn = require('opn');

require('ejs');
const Templates = {
  embark_building_placeholder: require('./templates/embark-building-placeholder.html.ejs')
};

class WebServer {
  constructor(embark) {
    this.embark = embark;
    this.logger = embark.logger;
    this.events = embark.events;
    this.buildDir = embark.config.buildDir;
    this.webServerConfig = embark.config.webServerConfig;
    if (!this.webServerConfig.enabled) {
      return;
    }

    this.host = this.webServerConfig.host;
    this.port = parseInt(this.webServerConfig.port);
    this._openBrowser = this.webServerConfig.openBrowser;

    this.events.emit("status", __("Starting Server"));

    this.server = new Server({
      buildDir: this.buildDir,
      events: this.events,
      host: this.host,
      port: this.port
    });

    this.events.on('webserver:config:change', () => {
      this.embark.config.webServerConfig = null;
      this.embark.config.loadWebServerConfigFile();
      this.webServerConfig = this.embark.config.webServerConfig;
      this.host = this.webServerConfig.host;
      this.port = this.webServerConfig.port;
      this.server.host = this.host;
      this.server.port = this.port;

      this.testPort(() => {
        this.server.stop((_err) => {
          this.server.start((_err, message, port) => {
            this.logger.info(message);
            this.port = port;
            this.events.request('open-browser', () => {});
          });
        });
      });
    });

    this.testPort(() => {
      this.listenToCommands();
      this.registerConsoleCommands();
      this.server.start((_err, message, port) => {
        this.logger.info(message);
        this.port = port;
        this.setServiceCheck();
      });
    });
  }

  testPort(done) {
    if (this.port === 0) {
      this.logger.warn(__('Assigning an available port'));
      this.server.port = 0;
      return done();
    }
    utils.pingEndpoint(this.host, this.port, 'http', 'http', '', (err) => {
      if (err) { // Port is ok
        return done();
{'fraction_non_alphanumeric': 0.16586812685375313, 'fraction_numerical': 0.0006844626967830253, 'mean_word_length': 2.6052631578947367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1758990', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1347, 'n_words': 223}