createProject static method

Future<void> createProject(
  1. Client cloudApiClient, {
  2. required CommandLogger logger,
  3. required String projectId,
  4. required PlanProfile? plan,
  5. required bool enableDb,
  6. required String projectDir,
  7. required String configFilePath,
  8. bool skipConfirmation = false,
})

Subcommand to create a new tenant project.

Implementation

static Future<void> createProject(
  final Client cloudApiClient, {
  required final CommandLogger logger,
  required final String projectId,
  required final PlanProfile? plan,
  required final bool enableDb,
  required final String projectDir,
  required final String configFilePath,
  final bool skipConfirmation = false,
}) async {
  if (!skipConfirmation) {
    await UserConfirmations.confirmNewProjectCostAcceptance(logger);
  }

  String? subscriptionId;
  if (plan == null) {
    // If no plan is specified and user has a legacy plan, use that.
    final subscriptions = await cloudApiClient.plans.listSubscriptions();
    if (subscriptions.isNotEmpty) {
      final legacySubscription = subscriptions
          .where(
            (final s) =>
                _legacyPlanNames.contains(s.planProductId.split(':').first),
          )
          .firstOrNull;
      if (legacySubscription != null) {
        logger.init('Creating Serverpod Cloud project "$projectId".');
        logger.info('On plan: ${legacySubscription.planDisplayName}');
        subscriptionId = legacySubscription.subscriptionId;
      }
    }
  }

  if (subscriptionId == null) {
    final planProductName = plan?.name ?? defaultPlan;
    subscriptionId = await cloudApiClient.plans.procurePlan(
      planProductName: planProductName,
    );
    logger.init('Creating Serverpod Cloud project "$projectId".');
    logger.info('On plan: $planProductName');
  }

  try {
    await logger.progress(
      'Registering Serverpod Cloud project.',
      newParagraph: true,
      () async {
        await cloudApiClient.projects.createProject(
          cloudProjectId: projectId,
          projectProductName: plan?.projectProductName,
          underSubscriptionId: subscriptionId,
        );
        return true;
      },
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(
      e,
      s,
      'Request to create a new project failed',
    );
  }

  if (enableDb) {
    await logger.progress('Requesting database creation.', () async {
      try {
        await cloudApiClient.database.enableDatabase(
          cloudCapsuleId: projectId,
        );
        return true;
      } on Exception catch (e, s) {
        throw FailureException.nested(
          e,
          s,
          'Request to create a database for the new project failed',
        );
      }
    });
  }

  logger.success('Serverpod Cloud project created.', newParagraph: true);
}