'use strict';

var path = require('path');
var fs = require('fs-extra');
var Docker = require('dockerode');
var pluginTechdocsNode = require('@backstage/plugin-techdocs-node');
var backendCommon = require('@backstage/backend-common');
var config = require('@backstage/config');
var utility = require('./utility-3c0df5a9.cjs.js');
require('winston');
require('stream');
require('process');

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

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var Docker__default = /*#__PURE__*/_interopDefaultLegacy(Docker);

async function generate(opts) {
  const logger = utility.createLogger({ verbose: opts.verbose });
  const sourceDir = path.resolve(opts.sourceDir);
  const outputDir = path.resolve(opts.outputDir);
  const omitTechdocsCorePlugin = opts.omitTechdocsCoreMkdocsPlugin;
  const dockerImage = opts.dockerImage;
  const pullImage = opts.pull;
  const legacyCopyReadmeMdToIndexMd = opts.legacyCopyReadmeMdToIndexMd;
  const defaultPlugins = opts.defaultPlugin;
  logger.info(`Using source dir ${sourceDir}`);
  logger.info(`Will output generated files in ${outputDir}`);
  logger.verbose("Creating output directory if it does not exist.");
  await fs__default["default"].ensureDir(outputDir);
  const { path: mkdocsYmlPath, configIsTemporary } = await pluginTechdocsNode.getMkdocsYml(
    sourceDir
  );
  const config$1 = new config.ConfigReader({
    techdocs: {
      generator: {
        runIn: opts.docker ? "docker" : "local",
        dockerImage,
        pullImage,
        mkdocs: {
          legacyCopyReadmeMdToIndexMd,
          omitTechdocsCorePlugin,
          defaultPlugins
        }
      }
    }
  });
  let containerRunner;
  if (opts.docker) {
    const dockerClient = new Docker__default["default"]();
    containerRunner = new backendCommon.DockerContainerRunner({ dockerClient });
  }
  let parsedLocationAnnotation = {};
  if (opts.techdocsRef) {
    try {
      parsedLocationAnnotation = utility.convertTechDocsRefToLocationAnnotation(
        opts.techdocsRef
      );
    } catch (err) {
      logger.error(err.message);
    }
  }
  const techdocsGenerator = await pluginTechdocsNode.TechdocsGenerator.fromConfig(config$1, {
    logger,
    containerRunner
  });
  logger.info("Generating documentation...");
  await techdocsGenerator.run({
    inputDir: sourceDir,
    outputDir,
    ...opts.techdocsRef ? {
      parsedLocationAnnotation
    } : {},
    logger,
    etag: opts.etag,
    logStream: utility.getLogStream(logger),
    siteOptions: { name: opts.siteName }
  });
  if (configIsTemporary) {
    process.on("exit", async () => {
      fs__default["default"].rmSync(mkdocsYmlPath, {});
    });
  }
  logger.info("Done!");
}

exports["default"] = generate;
//# sourceMappingURL=generate-5fb2bc8c.cjs.js.map
