createProject static method

Future<void> createProject(
  1. Client cloudApiClient, {
  2. required CommandLogger logger,
  3. required String projectId,
  4. required bool enableDb,
  5. required String projectDir,
  6. required String configFilePath,
})

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 bool enableDb,
  required final String projectDir,
  required final String configFilePath,
}) async {
  logger.init('Creating Serverpod Cloud project "$projectId".');

  // Check that the user is on a plan and automatically procure one if not.
  // This behavior will be changed in the future.
  final planNames = await cloudApiClient.plans.listProcuredPlanNames();
  if (planNames.isEmpty) {
    const defaultPlanName = 'closed-beta';
    await cloudApiClient.plans.procurePlan(planName: defaultPlanName);
    logger.info('On plan: $defaultPlanName');
  } else {
    logger.debug('On plan: ${planNames.first}');
  }

  try {
    await logger.progress(
      'Registering Serverpod Cloud project.',
      newParagraph: true,
      () async {
        await cloudApiClient.projects.createProject(
          cloudProjectId: projectId,
        );
        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.infraResources
              .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');
        }
      },
    );
  }

  if (isServerpodServerDirectory(Directory(projectDir))) {
    // write scloud project files unless the config file already exists

    final scloudYamlFile = File(configFilePath);
    if (scloudYamlFile.existsSync()) {
      logger.success(
        'Serverpod Cloud project created.',
        newParagraph: true,
      );

      return;
    }

    final projectConfig = await _fetchProjectConfig(
      logger,
      cloudApiClient,
      projectId,
    );

    await logger.progress(
      'Writing cloud project configuration files.',
      () async {
        _writeProjectFiles(
          logger,
          projectConfig,
          projectDir,
          configFilePath,
        );
        return true;
      },
    );
  } else {
    logger.terminalCommand(
      message: 'Since no Serverpod server directory was identified, '
          'an scloud.yaml configuration file has not been created. '
          'Use the link command to create it in the server '
          'directory of this project:',
      newParagraph: true,
      'scloud project link --project $projectId',
    );
  }

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