capacitor-firebase-kit
Version:
Provider-less Firebase Kit - Universal Firebase services integration for React, React Native, and Capacitor apps
156 lines (131 loc) ⢠5.15 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('š± Capacitor Firebase Kit - Post Install Setup');
// Helper function to check if file exists
function fileExists(filePath) {
try {
return fs.existsSync(filePath);
} catch (e) {
return false;
}
}
// Helper function to read file
function readFile(filePath) {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (e) {
return null;
}
}
// Helper function to write file
function writeFile(filePath, content) {
try {
fs.writeFileSync(filePath, content, 'utf8');
return true;
} catch (e) {
console.error(`Failed to write ${filePath}:`, e.message);
return false;
}
}
// Check if we're in a Capacitor project
const capacitorConfigPath = path.join(process.cwd(), 'capacitor.config.json');
if (!fileExists(capacitorConfigPath)) {
console.log('ā¹ļø Not in a Capacitor project root. Skipping auto-configuration.');
process.exit(0);
}
console.log('ā
Detected Capacitor project');
// Android Setup
function setupAndroid() {
console.log('\nš¤ Checking Android setup...');
const androidPath = path.join(process.cwd(), 'android');
if (!fileExists(androidPath)) {
console.log('ā¹ļø Android platform not added yet. Run: npx cap add android');
return;
}
// Check project-level build.gradle
const projectBuildGradlePath = path.join(androidPath, 'build.gradle');
if (fileExists(projectBuildGradlePath)) {
let projectBuildGradle = readFile(projectBuildGradlePath);
if (projectBuildGradle && !projectBuildGradle.includes('com.google.gms:google-services')) {
console.log('ā ļø Google Services plugin not found in android/build.gradle');
console.log('š Add this to your android/build.gradle buildscript dependencies:');
console.log(`
dependencies {
// ... existing dependencies ...
classpath 'com.google.gms:google-services:4.4.0'
}
`);
} else {
console.log('ā
Google Services plugin already configured');
}
}
// Check app-level build.gradle
const appBuildGradlePath = path.join(androidPath, 'app', 'build.gradle');
if (fileExists(appBuildGradlePath)) {
let appBuildGradle = readFile(appBuildGradlePath);
if (appBuildGradle && !appBuildGradle.includes("apply plugin: 'com.google.gms.google-services'")) {
console.log('ā ļø Google Services plugin not applied in android/app/build.gradle');
console.log('š Add this line at the top of your android/app/build.gradle:');
console.log(`
apply plugin: 'com.google.gms.google-services'
`);
} else {
console.log('ā
Google Services plugin already applied');
}
}
// Check for google-services.json
const googleServicesPath = path.join(androidPath, 'app', 'google-services.json');
if (!fileExists(googleServicesPath)) {
console.log('ā ļø google-services.json not found at android/app/google-services.json');
console.log('š Download it from Firebase Console and place it in android/app/');
} else {
console.log('ā
google-services.json found');
}
}
// iOS Setup
function setupIOS() {
console.log('\nš Checking iOS setup...');
const iosPath = path.join(process.cwd(), 'ios');
if (!fileExists(iosPath)) {
console.log('ā¹ļø iOS platform not added yet. Run: npx cap add ios');
return;
}
// Check for GoogleService-Info.plist
const googleServicePaths = [
path.join(iosPath, 'App', 'App', 'GoogleService-Info.plist'),
path.join(iosPath, 'App', 'GoogleService-Info.plist')
];
const plistExists = googleServicePaths.some(p => fileExists(p));
if (!plistExists) {
console.log('ā ļø GoogleService-Info.plist not found');
console.log('š Download it from Firebase Console and add it to your Xcode project');
} else {
console.log('ā
GoogleService-Info.plist found');
}
// Check AppDelegate for Firebase initialization
const appDelegatePath = path.join(iosPath, 'App', 'App', 'AppDelegate.swift');
if (fileExists(appDelegatePath)) {
const appDelegate = readFile(appDelegatePath);
if (appDelegate && !appDelegate.includes('import Firebase')) {
console.log('ā ļø Firebase not imported in AppDelegate.swift');
console.log('š Add "import Firebase" at the top of AppDelegate.swift');
}
if (appDelegate && !appDelegate.includes('FirebaseApp.configure()')) {
console.log('ā ļø Firebase not initialized in AppDelegate.swift');
console.log('š Add "FirebaseApp.configure()" in application:didFinishLaunchingWithOptions:');
}
if (appDelegate && appDelegate.includes('FirebaseApp.configure()')) {
console.log('ā
Firebase initialization found in AppDelegate');
}
}
}
// Run setup checks
setupAndroid();
setupIOS();
console.log('\nš For detailed setup instructions, see:');
console.log(' https://github.com/aoneahsan/capacitor-firebase-kit#setup');
console.log('\n⨠Happy coding with Capacitor Firebase Kit!');