'use strict';

var openBrowser = require('react-dev-utils/openBrowser');
var utility = require('./utility-3c0df5a9.cjs.js');
var mkdocsServer = require('./mkdocsServer-99ec8af2.cjs.js');
var pluginTechdocsNode = require('@backstage/plugin-techdocs-node');
var fs = require('fs-extra');
require('winston');
require('stream');
require('process');
require('child_process');

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

var openBrowser__default = /*#__PURE__*/_interopDefaultLegacy(openBrowser);
var fs__default = /*#__PURE__*/_interopDefaultLegacy(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;
  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 = await mkdocsServer.runMkdocsServer({
    port: opts.port,
    dockerImage: opts.dockerImage,
    dockerEntrypoint: opts.dockerEntrypoint,
    dockerOptions: opts.dockerOption,
    useDocker: opts.docker,
    stdoutLogFunc: logFunc,
    stderrLogFunc: logFunc
  });
  await mkdocsServer.waitForSignal([childProcess]);
  if (configIsTemporary) {
    process.on("exit", async () => {
      fs__default["default"].rmSync(mkdocsYmlPath, {});
    });
  }
}

exports["default"] = serveMkdocs;
//# sourceMappingURL=mkdocs-582c42fd.cjs.js.map
