'use strict';

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

var path = require('path');
var fs = require('fs-extra');
var pluginTechdocsNode = require('@backstage/plugin-techdocs-node');
var config = require('@backstage/config');
var utility = require('../../lib/utility.cjs.js');

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

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

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 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
  });
  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 },
    runAsDefaultUser: opts.runAsDefaultUser
  });
  if (configIsTemporary) {
    process.on("exit", async () => {
      fs__default.default.rmSync(mkdocsYmlPath, {});
    });
  }
  logger.info("Done!");
}

exports.default = generate;
//# sourceMappingURL=generate.cjs.js.map
