"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StartCmd = void 0;
function _bitError() {
  const data = require("@teambit/bit-error");
  _bitError = function () {
    return data;
  };
  return data;
}
function _legacy() {
  const data = require("@teambit/legacy.constants");
  _legacy = function () {
    return data;
  };
  return data;
}
function _open() {
  const data = _interopRequireDefault(require("open"));
  _open = function () {
    return data;
  };
  return data;
}
function _chalk() {
  const data = _interopRequireDefault(require("chalk"));
  _chalk = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class StartCmd {
  constructor(
  /**
   * access to the extension instance.
   */
  ui, logger) {
    this.ui = ui;
    this.logger = logger;
    _defineProperty(this, "name", 'start [component-pattern]');
    _defineProperty(this, "description", 'launch the Bit development server');
    _defineProperty(this, "extendedDescription", `starts the local development server providing a UI to browse, preview, and interact with components.
works in both workspaces and scopes. opens automatically in your browser at http://localhost:3000 (or specified port).
includes hot module reloading for development.`);
    _defineProperty(this, "arguments", [{
      name: 'component-pattern',
      description: _legacy().COMPONENT_PATTERN_HELP
    }]);
    _defineProperty(this, "alias", 'c');
    _defineProperty(this, "group", 'run-serve');
    _defineProperty(this, "options", [['d', 'dev', 'start UI server in dev mode.'], ['p', 'port [port-number]', 'port of the UI server.'], ['r', 'rebuild', 'rebuild the UI (useful e.g. when updating the workspace UI - can use the dev flag for HMR in this case)'], ['', 'skip-ui-build', 'skip building UI'], ['v', 'verbose', 'show verbose output for inspection and prints stack trace'], ['n', 'no-browser', 'do not automatically open browser when ready'], ['', 'show-internal-urls', 'show urls for all internal dev servers'], ['', 'skip-compilation', 'skip the auto-compilation before starting the web-server'], ['u', 'ui-root-name [type]', 'name of the ui root to use, e.g. "teambit.scope/scope" or "teambit.workspace/workspace"'], ['', 'use-root-modules', 'EXPERIMENTAL. resolve component previews from root node_modules instead of .bit_roots. mainly for internal usage, use with caution only if you understand the implications']]);
  }
  async wait([userPattern], {
    dev,
    port,
    rebuild,
    verbose,
    noBrowser,
    skipCompilation,
    skipUiBuild,
    showInternalUrls,
    uiRootName: uiRootAspectIdOrName,
    useRootModules
  }) {
    const spinnies = this.logger.multiSpinner;
    if (!this.ui.isHostAvailable()) {
      throw new (_bitError().BitError)(`bit start can only be run inside a bit workspace or a bit scope - please ensure you are running the command in the correct directory`);
    }
    const appName = this.ui.getUiName(uiRootAspectIdOrName);
    await this.ui.invokePreStart({
      skipCompilation
    });
    this.logger.off();
    spinnies.add('ui-server', {
      text: `Starting UI server for ${appName}`
    });
    const uiServer = this.ui.createRuntime({
      uiRootAspectIdOrName,
      skipUiBuild,
      pattern: userPattern,
      dev,
      port: +port,
      rebuild,
      verbose,
      showInternalUrls,
      useRootModules
    });
    uiServer.then(async server => {
      const url = this.ui.publicUrl || server.fullUrl;
      spinnies.succeed('ui-server', {
        text: `UI server ready ${_chalk().default.dim('\u2192')} ${_chalk().default.cyan(url)}`
      });
      if (!server.buildOptions?.launchBrowserOnStart) return undefined;
      await server.whenReady;
      const name = server.getName();
      const message = _chalk().default.green(`\nView '${_chalk().default.bold(name)}' components at ${_chalk().default.cyan(url)}`);
      spinnies.add('summary', {
        text: message,
        status: 'non-spinnable'
      });
      if (!noBrowser) {
        await (0, _open().default)(url);
      }
      return undefined;
    }).catch(error => {
      this.logger.error(`failed to start the UI server`, error);
      // spinnies.fail('ui-server', { text: `failed to start the UI server. ${error.message}` });
      throw new Error('failed to start the UI server, please try running the command with --log flag, or check bit debug.log file (see its location by running bit globals)');
    });

    // DO NOT CHANGE THIS - this meant to be an async hook.
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    this.ui.invokeOnStart();
    this.ui.clearConsole();
  }
}
exports.StartCmd = StartCmd;

//# sourceMappingURL=start.cmd.js.map