 => {
      const listener = app.listen(0, () => {
        const info = listener.address();
        if (typeof info !== "object" || info === null) {
          reject(new Error(`Unexpected listener info '${info}'`));
          return;
        }
        const { port } = info;
        resolve(`http://localhost:${port}`);
      });
    });
  }
}

var index = async (org) => {
  const answers = await inquirer__default["default"].prompt({
    name: "appType",
    type: "checkbox",
    message: "Select permissions [required] (these can be changed later but then require approvals in all installations)",
    choices: [
      {
        name: "Read access to content (required by Software Catalog to ingest data from repositories)",
        value: "read",
        checked: true
      },
      {
        name: "Read access to members (required by Software Catalog to ingest GitHub teams)",
        value: "members",
        checked: true
      },
      {
        name: "Read and Write to content and actions (required by Software Templates to create new repositories)",
        value: "write"
      }
    ]
  });
  if (answers.appType.length === 0) {
    console.log(chalk__default["default"].red("You must select at least one permission"));
    process.exit(1);
  }
  await verifyGithubOrg(org);
  const { slug, name, ...config } = await GithubCreateAppServer.run({
    org,
    permissions: answers.appType
  });
  const fileName = `github-app-${slug}-credentials.yaml`;
  const content = `# Name: ${name}
${yaml.stringify(config)}`;
  await fs__default["default"].writeFile(index$1.paths.resolveTargetRoot(fileName), content);
  console.log(`GitHub App configuration written to ${chalk__default["default"].cyan(fileName)}`);
  console.log(chalk__default["default"].yellow("This file contains sensitive credentials, it should not be committed to version control and handled with care!"));
  console.log("Here's an example on how to update the integrations section in app-config.yaml");
  console.log(chalk__default["default"].green(`
integrations:
  github:
    - host: github.com
      apps:
        - $include: ${fileName}`));
};
async function verifyGithubOrg(org) {
  let response;
  try {
    response = await fetch__default["default"](`https://api.github.com/orgs/${encodeURIComponent(org)}`);
  } catch (e) {
    console.log(chalk__default["default"].yellow("Warning: Unable to verify existence of GitHub organization. ", e));
  }
  if ((response == null ? void 0 : response.status) === 404) {
    const questions = [
      {
        type: "confirm",
        name: "shouldCreateOrg",
        message: `GitHub organization ${chalk__default["default"].cyan(org)} does not exist. Would you like to create a new Organization instead?`
      }
    ];
    const answers = await inquirer__default["default"].prompt(questions);
    if (!answers.shouldCreateOrg) {
      console.log(chalk__default["default"].yellow("GitHub organization must exist to create GitHub app"));
      process.exit(1);
    }
    openBrowser__default["default"]("https://github.com/account/organizations/new");
    console.log(chalk__default["default"].yellow("Please re-run this command when you have created your new organization"));
    process.exit(0);
  }
}

exports["default"] = index;
//# sourceMappingURL=index-644b213c.cjs.js.map
