'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var openBrowser = require('react-dev-utils/openBrowser');
var utility = require('../../lib/utility.cjs.js');
var mkdocsServer = require('../../lib/mkdocsServer.cjs.js');
var pluginTechdocsNode = require('@backstage/plugin-techdocs-node');
var fs = require('fs-extra');
var utils = require('./utils.cjs.js');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

var openBrowser__default = /*#__PURE__*/_interopDefaultCompat(openBrowser);
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);

async function serveMkdocs(opts) {
  const logger = utility.createLogger({ verbose: opts.verbose });
  const dockerAddr = `http://0.0.0.0:${opts.port}`;
  const localAddr = `http://127.0.0.1:${opts.port}`;
  const expectedDevAddr = opts.docker ? dockerAddr : localAddr;
  if (opts.docker) {
    const isDockerOperational = await utils.checkIfDockerIsOperational(logger);
    if (!isDockerOperational) {
      return;
    }
  }
  const { path: mkdocsYmlPath, configIsTemporary } = await pluginTechdocsNode.getMkdocsYml(
    "./",
    opts.siteName
  );
  let boolOpenBrowserTriggered = false;
  const logFunc = (data) => {
    const logLines = data.toString().split("\n");
    const logPrefix = opts.docker ? "[docker/mkdocs]" : "[mkdocs]";
    logLines.forEach((line) => {
      if (line === "") {
        return;
      }
      logger.verbose(`${logPrefix} ${line}`);
      if (!boolOpenBrowserTriggered && line.includes(`Serving on ${expectedDevAddr}`)) {
        logger.info(`
Starting mkdocs server on ${localAddr}
`);
        openBrowser__default.default(localAddr);
        boolOpenBrowserTriggered = true;
      }
    });
  };
  const childProcess = mkdocsServer.runMkdocsServer({
    port: opts.port,
    dockerImage: opts.dockerImage,
    dockerEntrypoint: opts.dockerEntrypoint,
    dockerOptions: opts.dockerOption,
    useDocker: opts.docker,
    onStdout: logFunc,
    onStderr: logFunc
  });
  await childProcess.waitForExit();
  if (configIsTemporary) {
    process.on("exit", async () => {
      fs__default.default.rmSync(mkdocsYmlPath, {});
    });
  }
}

exports.default = serveMkdocs;
//# sourceMappingURL=mkdocs.cjs.js.map
