requireProjectIdAsync method

Future<String> requireProjectIdAsync()

Implementation

Future<String> requireProjectIdAsync() async {
  final pid = resolvedProjectId;
  if (pid != null && pid.isNotEmpty) {
    return pid;
  }

  // Try auto-resolving from authenticated user if available
  try {
    final uToken = explicitUserKey ?? userConfig?.jws;
    if (uToken != null && uToken.isNotEmpty) {
      final client = container.read(projectServiceClientProvider);
      final projectsResponse = await client.getAllProjects(
        Empty(),
        options: getCallOptions(),
      );
      if (projectsResponse.projects.length == 1) {
        return projectsResponse.projects.first.id;
      } else if (projectsResponse.projects.length > 1) {
        final list = projectsResponse.projects
            .map((p) => '"${p.name}" (ID: ${p.id})')
            .join(', ');
        throw Exception(
          'Multiple projects found for user account: $list. Please specify --project-id <id> or create a localizable_project.json file.',
        );
      }
    }
  } catch (e) {
    if (e.toString().contains('Multiple projects found')) {
      rethrow;
    }
  }

  throw Exception(
    'Project ID is required. Pass --project-id <id> or provide a localizable_project.json file.',
  );
}