text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _logger = _interopRequireDefault(require("@wdio/logger"));
  
  var _util = require("util");
  
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
  
  var _seleniumStandalone = _interopRequireDefault(require("selenium-standalone"));
  
  var _getFilePath = _interopRequireDefault(require("./utils/getFilePath"));
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  const DEFAULT_LOG_FILENAME = 'selenium-standalone.txt';
  const log = (0, _logger.default)('@wdio/selenium-standalone-service');
  
  class SeleniumStandaloneLauncher {
    constructor() {
      _defineProperty(this, "_stopProcess", () => {
        if (this.process) {
          log.info('shutting down all browsers');
          this.process.kill();
        }
      });
  
      this.seleniumLogs = null;
      this.seleniumArgs = {};
      this.seleniumInstallArgs = {};
      return this;
    }
  
    async onPrepare(config) {
      this.seleniumArgs = config.seleniumArgs || {};
      this.seleniumInstallArgs = config.seleniumInstallArgs || {};
      this.seleniumLogs = config.seleniumLogs;
      this.skipSeleniumInstall = !!config.skipSeleniumInstall;
      this.watchMode = !!config.watch;
  
      if (!this.skipSeleniumInstall) {
        await (0, _util.promisify)(_seleniumStandalone.default.install)(this.seleniumInstallArgs);
      }
  
      this.process = await (0, _util.promisify)(_seleniumStandalone.default.start)(this.seleniumArgs);
  
      if (typeof this.seleniumLogs === 'string') {
        this._redirectLogStream();
      }
  
      if (this.watchMode) {
        process.on('SIGINT', this._stopProcess);
        process.on('exit', this._stopProcess);
        process.on('uncaughtException', this._stopProcess);
      }
    }
  
    onComplete() {
      if (!this.watchMode) {
        this._stopProcess();
      }
    }
  
    _redirectLogStream() {
      const logFile = (0, _getFilePath.default)(this.seleniumLogs, DEFAULT_LOG_FILENAME);
  
      _fsExtra.default.ensureFileSync(logFile);
  
      const logStream = _fsExtra.default.createWriteStream(logFile, {
        flags: 'w'
      });
  
      this.process.stdout.pipe(logStream);
      this.process.stderr.pipe(logStream);
    }
  
  }
  
  exports.default = SeleniumStandaloneLauncher;