'use strict';

var cliNode = require('@backstage/cli-node');
var errors = require('@backstage/errors');
var fs = require('fs-extra');
var os = require('os');
var path = require('path');
var chalk = require('chalk');
var inquirer = require('inquirer');
var partition = require('lodash/partition');
var camelCase = require('lodash/camelCase');
var upperFirst = require('lodash/upperFirst');
var index = require('./index-79e50cf7.cjs.js');
var tasks = require('./tasks-581252fb.cjs.js');
var Lockfile = require('./Lockfile-1c04a5bb.cjs.js');
require('commander');
require('semver');
require('@backstage/cli-common');
require('handlebars');
require('ora');
require('recursive-readdir');
require('child_process');
require('util');
require('@yarnpkg/lockfile');
require('@yarnpkg/parsers');

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

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var chalk__default = /*#__PURE__*/_interopDefaultLegacy(chalk);
var inquirer__default = /*#__PURE__*/_interopDefaultLegacy(inquirer);
var partition__default = /*#__PURE__*/_interopDefaultLegacy(partition);
var camelCase__default = /*#__PURE__*/_interopDefaultLegacy(camelCase);
var upperFirst__default = /*#__PURE__*/_interopDefaultLegacy(upperFirst);

const TEAM_ID_RE = /^@[-\w]+\/[-\w]+$/;
const USER_ID_RE = /^@[-\w]+$/;
const EMAIL_RE = /^[^@]+@[-.\w]+\.[-\w]+$/i;
const DEFAULT_OWNER = "@backstage/maintainers";
async function getCodeownersFilePath(rootDir) {
  const possiblePaths = [
    path__default["default"].join(rootDir, ".github", "CODEOWNERS"),
    path__default["default"].join(rootDir, ".gitlab", "CODEOWNERS"),
    path__default["default"].join(rootDir, "docs", "CODEOWNERS"),
    path__default["default"].join(rootDir, "CODEOWNERS")
  ];
  for (const p of possiblePaths) {
    if (await fs__default["default"].pathExists(p)) {
      return p;
    }
  }
  return void 0;
}
function isValidSingleOwnerId(id) {
  if (!id || typeof id !== "string") {
    return false;
  }
  return TEAM_ID_RE.test(id) || USER_ID_RE.test(id) || EMAIL_RE.test(id);
}
function parseOwnerIds(spaceSeparatedOwnerIds) {
  if (!spaceSeparatedOwnerIds || typeof spaceSeparatedOwnerIds !== "string") {
    return void 0;
  }
  const ids = spaceSeparatedOwnerIds.split(" ").filter(Boolean);
  if (!ids.every(isValidSingleOwnerId)) {
    return void 0;
  }
  return ids;
}
async function addCodeownersEntry(ownedPath, ownerStr, codeownersFilePath) {
  const ownerIds = parseOwnerIds(ownerStr);
  if (!ownerIds || ownerIds.length === 0) {
    return false;
  }
  let filePath = codeownersFilePath;
  if (!filePath) {
    filePath = await getCodeownersFilePath(index.paths.targetRoot);
    if (!filePath) {
      return false;
    }
  }
  const allLines = (await fs__default["default"].readFile(filePath, "utf8")).split("\n");
  const commentLines = [];
  for (const line of allLines) {
    if (line[0] !== "#") {
      break;
    }
    commentLines.push(line);
  }
  const oldDeclarationEntries = allLines.filter((line) => line[0] !== "#").map((line) => line.split(/\s+/).filter(Boolean)).filter((tokens) => tokens.length >= 2).map((tokens) => ({
    ownedPath: tokens[0],
    ownerIds: tokens.slice(1)
  }));
  const newDeclarationEntries = oldDeclarationEntries.filter((entry) => entry.ownedPath !== "*").concat([{ ownedPath, ownerIds }]).sort((l1, l2) => l1.ownedPath.localeCompare(l2.ownedPath));
  newDeclarationEntries.unshift({
    ownedPath: "*",
    ownerIds: [DEFAULT_OWNER]
  });
  const longestOwnedPath = newDeclarationEntries.reduce(
    (length, entry) => Math.max(length, entry.ownedPath.length),
    0
  );
  const newDeclarationLines = newDeclarationEntries.map((entry) => {
    const entryPath = entry.ownedPath + " ".repeat(longestOwnedPath - entry.ownedPath.length);
    return [entryPath, ...entry.ownerIds].join(" ");
  });
  const newLines = [...commentLines, "", ...newDeclarationLines, ""];
  await fs__default["default"].writeFile(filePath, newLines.join("\n"), "utf8");
  return true;
}

function createFactory(config) {
  return config;
}

function pluginIdPrompt() {
  return {
    type: "input",
    name: "id",
    message: "Enter the ID of the plugin [required]",
    validate: (value) => {
      if (!value) {
        return "Please enter the ID of the plugin";
      } else if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value)) {
        return "Plugin IDs must be lowercase and contain only letters, digits, and dashes.";
      }
      return true;
    }
  };
}
function moduleIdIdPrompt() {
  return {
    type: "input",
    name: "moduleId",
    message: "Enter the ID of the module [required]",
    validate: (value) => {
      if (!value) {
        return "Please enter the ID of the module";
      } else if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value)) {
        return "Module IDs must be lowercase and contain only letters, digits, and dashes.";
      }
      return true;
    }
  };
}
function ownerPrompt() {
  return {
    type: "input",
    name: "owner",
    message: "Enter an owner to add to CODEOWNERS [optional]",
    when: (opts) => Boolean(opts.codeOwnersPath),
    validate: (value) => {
      if (!value) {
        return true;
      }
      const ownerIds = parseOwnerIds(value);
      if (!ownerIds) {
        return "The owner must be a space separated list of team names (e.g. @org/team-name), usernames (e.g. @username), or the email addresses (e.g. user@example.com).";
      }
      return true;
    }
  };
}

async function executePluginPackageTemplate(ctx, options) {
  const { targetDir } = options;
  let lockfile;
  try {
    lockfile = await Lockfile.Lockfile.load(index.paths.resolveTargetRoot("yarn.lock"));
  } catch {
  }
  tasks.Task.section("Checking Prerequisites");
  const shortPluginDir = path.relative(index.paths.targetRoot, targetDir);
  await tasks.Task.forItem("availability", shortPluginDir, async () => {
    if (await fs__default["default"].pathExists(targetDir)) {
      throw new Error(
        `A package with the same plugin ID already exists at ${chalk__default["default"].cyan(
          shortPluginDir
        )}. Please try again with a different ID.`
      );
    }
  });
  const tempDir = await tasks.Task.forItem("creating", "temp dir", async () => {
    return await ctx.createTemporaryDirectory("backstage-create");
  });
  tasks.Task.section("Executing Template");
  await tasks.templatingTask(
    index.paths.resolveOwn("templates", options.templateName),
    tempDir,
    options.values,
    index.createPackageVersionProvider(lockfile),
    ctx.isMonoRepo
  );
  const pkgJsonPath = path.resolve(tempDir, "package.json");
  if (await fs__default["default"].pathExists(pkgJsonPath)) {
    const pkgJson = await fs__default["default"].readJson(pkgJsonPath);
    await fs__default["default"].writeJson(pkgJsonPath, pkgJson, { spaces: 2 });
  }
  tasks.Task.section("Installing");
  await tasks.Task.forItem("moving", shortPluginDir, async () => {
    await fs__default["default"].move(tempDir, targetDir).catch((error) => {
      throw new Error(
        `Failed to move package from ${tempDir} to ${targetDir}, ${error.message}`
      );
    });
  });
  ctx.markAsModified();
}

const frontendPlugin = createFactory({
  name: "plugin",
  description: "A new frontend plugin",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const name = `@janus-idp/backstage-plugin-${id}`;
    const extensionName = `${upperFirst__default["default"](camelCase__default["default"](id))}Page`;
    tasks.Task.log();
    tasks.Task.log(`Creating frontend plugin ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", id) : index.paths.resolveTargetRoot(`backstage-plugin-${id}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-plugin",
      values: {
        id,
        name,
        extensionName,
        pluginVar: `${camelCase__default["default"](id)}Plugin`,
        pluginVersion: ctx.defaultVersion,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry
      }
    });
    if (!ctx.doNotEditPackages && await fs__default["default"].pathExists(index.paths.resolveTargetRoot("packages/app"))) {
      await tasks.Task.forItem("app", "adding dependency", async () => {
        await tasks.addPackageDependency(
          index.paths.resolveTargetRoot("packages/app/package.json"),
          {
            dependencies: {
              [name]: `^${ctx.defaultVersion}`
            }
          }
        );
      });
      await tasks.Task.forItem("app", "adding import", async () => {
        var _a;
        const pluginsFilePath = index.paths.resolveTargetRoot(
          "packages/app/src/App.tsx"
        );
        if (!await fs__default["default"].pathExists(pluginsFilePath)) {
          return;
        }
        const content = await fs__default["default"].readFile(pluginsFilePath, "utf8");
        const revLines = content.split("\n").reverse();
        const lastImportIndex = revLines.findIndex(
          (line) => line.match(/ from ("|').*("|')/)
        );
        const lastRouteIndex = revLines.findIndex(
          (line) => line.match(/<\/FlatRoutes/)
        );
        if (lastImportIndex !== -1 && lastRouteIndex !== -1) {
          const importLine = `import { ${extensionName} } from '${name}';`;
          if (!content.includes(importLine)) {
            revLines.splice(lastImportIndex, 0, importLine);
          }
          const componentLine = `<Route path="/${id}" element={<${extensionName} />} />`;
          if (!content.includes(componentLine)) {
            const [indentation] = (_a = revLines[lastRouteIndex + 1].match(/^\s*/)) != null ? _a : [];
            revLines.splice(lastRouteIndex + 1, 0, indentation + componentLine);
          }
          const newContent = revLines.reverse().join("\n");
          await fs__default["default"].writeFile(pluginsFilePath, newContent, "utf8");
        }
      });
    }
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${id}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const backendPlugin = createFactory({
  name: "backend-plugin",
  description: "A new backend plugin",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const pluginId = `${id}-backend`;
    const name = `@janus-idp/backstage-plugin-${pluginId}`;
    tasks.Task.log();
    tasks.Task.log(`Creating backend plugin ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", pluginId) : index.paths.resolveTargetRoot(`backstage-plugin-${pluginId}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-backend-plugin",
      values: {
        id,
        name,
        pluginVar: `${camelCase__default["default"](id)}Plugin`,
        pluginVersion: ctx.defaultVersion,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry
      }
    });
    if (!ctx.doNotEditPackages && await fs__default["default"].pathExists(index.paths.resolveTargetRoot("packages/backend"))) {
      await tasks.Task.forItem("backend", "adding dependency", async () => {
        await tasks.addPackageDependency(
          index.paths.resolveTargetRoot("packages/backend/package.json"),
          {
            dependencies: {
              [name]: `^${ctx.defaultVersion}`
            }
          }
        );
      });
    }
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${id}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const backendModule = createFactory({
  name: "backend-module",
  description: "A new backend module",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), moduleIdIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id: pluginId, moduleId } = options;
    const dirName = `${pluginId}-backend-module-${moduleId}`;
    const name = `@janus-idp/backstage-plugin-${dirName}`;
    tasks.Task.log();
    tasks.Task.log(`Creating backend module ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", dirName) : index.paths.resolveTargetRoot(`backstage-plugin-${dirName}`);
    const moduleCamelCase = camelCase__default["default"](moduleId);
    const modulePascalCase = moduleCamelCase[0].toUpperCase() + moduleCamelCase.slice(1);
    const moduleVar = `${camelCase__default["default"](pluginId)}Module${modulePascalCase}`;
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-backend-module",
      values: {
        pluginId,
        moduleId,
        name,
        moduleVar,
        packageVersion: ctx.defaultVersion,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry
      }
    });
    if (!ctx.doNotEditPackages && await fs__default["default"].pathExists(index.paths.resolveTargetRoot("packages/backend"))) {
      await tasks.Task.forItem("backend", "adding dependency", async () => {
        await tasks.addPackageDependency(
          index.paths.resolveTargetRoot("packages/backend/package.json"),
          {
            dependencies: {
              [name]: `^${ctx.defaultVersion}`
            }
          }
        );
      });
    }
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${dirName}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const webLibraryPackage = createFactory({
  name: "web-library",
  description: "A new web-library package",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const name = `@janus-idp/backstage-${id}`;
    tasks.Task.log();
    tasks.Task.log(`Creating web-library package ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("packages", id) : index.paths.resolveTargetRoot(`${id}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "web-library-package",
      values: {
        id,
        name,
        pluginVersion: ctx.defaultVersion,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry
      }
    });
    if (options.owner) {
      await addCodeownersEntry(`/packages/${id}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const pluginCommon = createFactory({
  name: "plugin-common",
  description: "A new isomorphic common plugin package",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const suffix = `${id}-common`;
    const name = `@janus-idp/backstage-plugin-${suffix}`;
    tasks.Task.log();
    tasks.Task.log(`Creating backend plugin ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", suffix) : index.paths.resolveTargetRoot(`backstage-plugin-${suffix}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-common-plugin-package",
      values: {
        id,
        name,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry,
        pluginVersion: ctx.defaultVersion
      }
    });
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${suffix}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const pluginNode = createFactory({
  name: "plugin-node",
  description: "A new Node.js library plugin package",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const suffix = `${id}-node`;
    const name = `@janus-idp/backstage-plugin-${suffix}`;
    tasks.Task.log();
    tasks.Task.log(`Creating Node.js plugin library ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", suffix) : index.paths.resolveTargetRoot(`backstage-plugin-${suffix}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-node-plugin-package",
      values: {
        id,
        name,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry,
        pluginVersion: ctx.defaultVersion
      }
    });
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${suffix}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const pluginWeb = createFactory({
  name: "plugin-react",
  description: "A new web library plugin package",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [pluginIdPrompt(), ownerPrompt()],
  async create(options, ctx) {
    const { id } = options;
    const suffix = `${id}-react`;
    const name = `@janus-idp/backstage-plugin-${suffix}`;
    tasks.Task.log();
    tasks.Task.log(`Creating web plugin library ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", suffix) : index.paths.resolveTargetRoot(`backstage-plugin-${suffix}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "default-react-plugin-package",
      values: {
        id,
        name,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry,
        pluginVersion: ctx.defaultVersion
      }
    });
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${suffix}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

const scaffolderModule = createFactory({
  name: "scaffolder-module",
  description: "An module exporting custom actions for @backstage/plugin-scaffolder-backend",
  optionsDiscovery: async () => ({
    codeOwnersPath: await getCodeownersFilePath(index.paths.targetRoot)
  }),
  optionsPrompts: [
    {
      type: "input",
      name: "id",
      message: "Enter the name of the module [required]",
      validate: (value) => {
        if (!value) {
          return "Please enter the name of the module";
        } else if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value)) {
          return "Module names must be lowercase and contain only letters, digits, and dashes.";
        }
        return true;
      }
    },
    ownerPrompt()
  ],
  async create(options, ctx) {
    const { id } = options;
    const slug = `scaffolder-backend-module-${id}`;
    const name = `@janus-idp/backstage-plugin-${slug}`;
    tasks.Task.log();
    tasks.Task.log(`Creating module ${chalk__default["default"].cyan(name)}`);
    const targetDir = ctx.isMonoRepo ? index.paths.resolveTargetRoot("plugins", slug) : index.paths.resolveTargetRoot(`backstage-plugin-${slug}`);
    await executePluginPackageTemplate(ctx, {
      targetDir,
      templateName: "scaffolder-module",
      values: {
        id,
        name,
        privatePackage: ctx.private,
        npmRegistry: ctx.npmRegistry,
        pluginVersion: ctx.defaultVersion
      }
    });
    if (options.owner) {
      await addCodeownersEntry(`/plugins/${slug}`, options.owner);
    }
    await tasks.Task.forCommand("yarn install", { cwd: targetDir, optional: true });
    await tasks.Task.forCommand("yarn lint --fix", {
      cwd: targetDir,
      optional: true
    });
  }
});

var factories = /*#__PURE__*/Object.freeze({
  __proto__: null,
  frontendPlugin: frontendPlugin,
  backendPlugin: backendPlugin,
  backendModule: backendModule,
  webLibraryPackage: webLibraryPackage,
  pluginCommon: pluginCommon,
  pluginNode: pluginNode,
  pluginWeb: pluginWeb,
  scaffolderModule: scaffolderModule
});

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
function applyPromptMessageTransforms(prompt, transforms) {
  return {
    ...prompt,
    message: prompt.message && (async (answers) => {
      if (typeof prompt.message === "function") {
        return transforms.message(await prompt.message(answers));
      }
      return transforms.message(await prompt.message);
    }),
    validate: prompt.validate && (async (...args) => {
      const result = await prompt.validate(...args);
      if (typeof result === "string") {
        return transforms.error(result);
      }
      return result;
    })
  };
}
class FactoryRegistry {
  static async interactiveSelect(preselected) {
    let selected = preselected;
    if (!selected) {
      const answers = await inquirer__default["default"].prompt([
        {
          type: "list",
          name: "name",
          message: "What do you want to create?",
          choices: Array.from(this.factoryMap.values()).map((factory2) => ({
            name: `${factory2.name} - ${factory2.description}`,
            value: factory2.name
          }))
        }
      ]);
      selected = answers.name;
    }
    const factory = this.factoryMap.get(selected);
    if (!factory) {
      throw new Error(`Unknown selection '${selected}'`);
    }
    return factory;
  }
  static async populateOptions(factory, provided) {
    let currentOptions = provided;
    if (factory.optionsDiscovery) {
      const discoveredOptions = await factory.optionsDiscovery();
      currentOptions = {
        ...currentOptions,
        ...discoveredOptions
      };
    }
    if (factory.optionsPrompts) {
      const [hasAnswers, needsAnswers] = partition__default["default"](
        factory.optionsPrompts,
        (option) => option.name in currentOptions
      );
      for (const option of hasAnswers) {
        const value = provided[option.name];
        if (option.validate) {
          const result = option.validate(value);
          if (result !== true) {
            throw new Error(`Invalid option '${option.name}'. ${result}`);
          }
        }
      }
      currentOptions = await inquirer__default["default"].prompt(
        needsAnswers.map(
          (option) => applyPromptMessageTransforms(option, {
            message: chalk__default["default"].blue,
            error: chalk__default["default"].red
          })
        ),
        currentOptions
      );
    }
    return currentOptions;
  }
}
__publicField(FactoryRegistry, "factoryMap", new Map(
  Object.values(factories).map((factory) => [factory.name, factory])
));

function parseOptions(optionStrings) {
  const options = {};
  for (const str of optionStrings) {
    const [key] = str.split("=", 1);
    const value = str.slice(key.length + 1);
    if (!key || str[key.length] !== "=") {
      throw new Error(
        `Invalid option '${str}', must be of the format <key>=<value>`
      );
    }
    options[key] = value;
  }
  return options;
}
var _new = async (opts) => {
  const factory = await FactoryRegistry.interactiveSelect(opts.select);
  const providedOptions = parseOptions(opts.option);
  const options = await FactoryRegistry.populateOptions(
    factory,
    providedOptions
  );
  let defaultVersion = "0.1.0";
  if (opts.baseVersion) {
    defaultVersion = opts.baseVersion;
  } else {
    const lernaVersion = await fs__default["default"].readJson(index.paths.resolveTargetRoot("lerna.json")).then((pkg) => pkg.version).catch(() => void 0);
    if (lernaVersion) {
      defaultVersion = lernaVersion;
    }
  }
  const tempDirs = new Array();
  async function createTemporaryDirectory(name) {
    const dir = await fs__default["default"].mkdtemp(path.join(os__default["default"].tmpdir(), name));
    tempDirs.push(dir);
    return dir;
  }
  let modified = false;
  try {
    await factory.create(options, {
      isMonoRepo: await cliNode.isMonoRepo(),
      defaultVersion,
      npmRegistry: opts.npmRegistry,
      private: Boolean(opts.private),
      createTemporaryDirectory,
      markAsModified() {
        modified = true;
      },
      doNotEditPackages: Boolean(opts.doNotEditPackages)
    });
    tasks.Task.log();
    tasks.Task.log(`\u{1F389}  Successfully created ${factory.name}`);
    tasks.Task.log();
  } catch (error) {
    errors.assertError(error);
    tasks.Task.error(error.message);
    if (modified) {
      tasks.Task.log("It seems that something went wrong in the creation process \u{1F914}");
      tasks.Task.log();
      tasks.Task.log(
        "We have left the changes that were made intact in case you want to"
      );
      tasks.Task.log(
        "continue manually, but you can also revert the changes and try again."
      );
      tasks.Task.error(`\u{1F525}  Failed to create ${factory.name}!`);
    }
  } finally {
    for (const dir of tempDirs) {
      try {
        await fs__default["default"].remove(dir);
      } catch (error) {
        console.error(
          `Failed to remove temporary directory '${dir}', ${error}`
        );
      }
    }
  }
};

exports["default"] = _new;
//# sourceMappingURL=new-a9a0e75f.cjs.js.map
