createProject static method

Future<String> createProject(
  1. CommandLogger logger, {
  2. required String consoleServer,
  3. required bool openBrowser,
  4. required String projectName,
  5. required bool usesDb,
  6. Duration timeLimit = const Duration(minutes: 5),
})

Hands off project creation to the Serverpod Cloud console.

Opens the console's create-project page, forwarding the analyzed project information (projectName and whether the project usesDb), and waits for the console to redirect back to a local callback server with the id of the created project. Returns the created project id.

Implementation

static Future<String> createProject(
  final CommandLogger logger, {
  required final String consoleServer,
  required final bool openBrowser,
  required final String projectName,
  required final bool usesDb,
  final Duration timeLimit = const Duration(minutes: 5),
}) async {
  final callbackUrlFuture = Completer<Uri>();
  final projectIdFuture = ListenerServer.listenForCallback(
    queryParameter: 'projectId',
    logger: logger,
    onConnected: callbackUrlFuture.complete,
    timeLimit: timeLimit,
    successMessage:
        'Project created, you may now close this window and return to the CLI.',
    failureMessage:
        'Project creation failed, please try again or contact support.',
  );

  final callbackUrl = await callbackUrlFuture.future;
  final createProjectUrl = Uri.parse(consoleServer).replace(
    path: ConsoleRoutes.createProject,
    queryParameters: {
      'project-name': projectName,
      'database-enabled': usesDb.toString(),
      'return-url': callbackUrl.toString(),
    },
  );

  logger.info(
    'Please create your project in the opened browser or through this link:\n'
    '$createProjectUrl',
  );

  if (openBrowser) {
    try {
      await BrowserLauncher.openUrl(createProjectUrl);
    } on Exception catch (e) {
      logger.error('Failed to open browser', exception: e);
    }
  }

  String? createdProjectId;
  await logger.progress(
    'Waiting for project creation',
    successMessage: 'Project created.',
    padRight: StatusCommands.progressMessagePadLength,
    () async {
      createdProjectId = await projectIdFuture;
      return createdProjectId != null;
    },
  );

  final projectId = createdProjectId;
  if (projectId == null) {
    throw FailureException(
      error: 'Failed to create project.',
      hint: 'Please try again.',
    );
  }

  return projectId;
}