createFirebaseProject function
Creates a new Firebase project or uses an existing one.
This function guides the user through the process of setting up a Firebase
project for a given client. It checks for a logged-in Firebase user,
lists existing projects, and prompts the user to either use an existing
project or create a new one if a project with the specified firebaseProjectId
already exists.
clientId The ID of the client for which the Firebase project is being created.
packageName The package name of the application.
firebaseProjectId The desired Firebase project ID.
Throws an Exception if no user is logged in to Firebase CLI, if retrieving project lists fails, or if the user provides an empty new project ID.
Implementation
Future<void> createFirebaseProject({
required String clientId,
required String packageName,
required String firebaseProjectId,
}) async {
try {
// Step 1: Get the logged-in user
logger.i('π Checking logged-in Firebase user...');
final userResult = await Process.run('firebase', ['login:list', '--json']);
if (userResult.exitCode != 0) {
throw Exception('β Failed to retrieve Firebase user info.');
}
final userData = jsonDecode(userResult.stdout) as Map<String, dynamic>;
if (userData['status'] != 'success' ||
(userData['result'] as List).isEmpty) {
throw Exception('β No user is logged in to Firebase CLI.');
}
final loggedInUser = userData['result'][0]['user'] as Map<String, dynamic>;
final userEmail = loggedInUser['email'];
logger.i('π€ Logged in as: $userEmail');
// Step 2: Get the list of Firebase projects
logger.i('π Fetching the list of Firebase projects...');
final projectsResult = await Process.run('firebase', [
'projects:list',
'--json',
]);
if (projectsResult.exitCode != 0) {
throw Exception('β Failed to retrieve the list of Firebase projects.');
}
final projectsJson =
jsonDecode(projectsResult.stdout) as Map<String, dynamic>;
final projectsData = projectsJson['results'] as List<dynamic>?;
if (projectsData == null || projectsData.isEmpty) {
logger.i('π No existing Firebase projects found.');
} else {
logger.i('π Current Firebase projects:');
for (final project in projectsData) {
logger.i(
' - ${project['projectId']} (${project['displayName']}) by ${project['projectOwner'] ?? 'Unknown'}',
);
}
}
// Step 3: Check if a project with the same ID exists
final existingProject = projectsData?.firstWhere(
(project) => project['projectId'] == firebaseProjectId,
orElse: () => null,
);
if (existingProject != null) {
// Prompt user to decide whether to use the existing project
logger.i(
'β οΈ A project with the ID "$firebaseProjectId" already exists (Display Name: ${existingProject['displayName']}).',
);
final choice = prompt('Do you want to use this existing project? (y/n):');
if (choice.toLowerCase() == 'y') {
logger.i('β
Using existing project: $firebaseProjectId');
return;
} else {
logger.i(
'π You chose not to use the existing project. Please provide a new project ID.',
);
final newProjectId = prompt(
'Enter a new Firebase Project ID (e.g., my-new-project):',
);
if (newProjectId.isEmpty) {
throw Exception('β Project ID cannot be empty.');
}
firebaseProjectId = newProjectId;
}
}
// Step 4: Create a new Firebase project
final displayName = '${toTitleCase(clientId)}-HR-Flutter';
logger.i('π Creating Firebase project: $firebaseProjectId...');
await runCommand(
'firebase',
['projects:create', '--display-name', displayName, firebaseProjectId],
successMessage:
'β
Firebase project created successfully: $firebaseProjectId',
);
} catch (e) {
logger.e('β Error during Firebase project creation: $e');
}
}