#!/usr/bin/env node /** * Post-sync script to fix Capacitor auto-generated files * * Fixes: * 1. capacitor.plugins.json - Ensures DailyNotification plugin is registered * 2. capacitor.settings.gradle - Verifies plugin path points to android/ (standard structure) * * This script should run automatically after 'npx cap sync android' * to verify Capacitor's auto-generated files are correct. * * @author Matthew Raymer */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PLUGINS_JSON_PATH = path.join(__dirname, '../android/app/src/main/assets/capacitor.plugins.json'); const SETTINGS_GRADLE_PATH = path.join(__dirname, '../android/capacitor.settings.gradle'); const PLUGIN_ENTRY = { name: "DailyNotification", classpath: "com.timesafari.dailynotification.DailyNotificationPlugin" }; /** * Fix capacitor.plugins.json to ensure DailyNotification is registered */ function fixCapacitorPlugins() { console.log('šŸ”§ Fixing capacitor.plugins.json...'); try { // Read current content let plugins = []; if (fs.existsSync(PLUGINS_JSON_PATH)) { const content = fs.readFileSync(PLUGINS_JSON_PATH, 'utf8'); plugins = JSON.parse(content); } // Check if our plugin is already there const hasPlugin = plugins.some(p => p.name === PLUGIN_ENTRY.name); if (!hasPlugin) { plugins.push(PLUGIN_ENTRY); fs.writeFileSync(PLUGINS_JSON_PATH, JSON.stringify(plugins, null, 2)); console.log('āœ… Added DailyNotification plugin to capacitor.plugins.json'); } else { console.log('āœ… DailyNotification plugin already exists in capacitor.plugins.json'); } } catch (error) { console.error('āŒ Error fixing capacitor.plugins.json:', error.message); process.exit(1); } } /** * Fix capacitor.settings.gradle to verify plugin path points to android/ (standard structure) */ function fixCapacitorSettingsGradle() { console.log('šŸ”§ Verifying capacitor.settings.gradle...'); if (!fs.existsSync(SETTINGS_GRADLE_PATH)) { console.log('ā„¹ļø capacitor.settings.gradle not found (may not be a test-app)'); return; } try { let content = fs.readFileSync(SETTINGS_GRADLE_PATH, 'utf8'); const originalContent = content; // Check if the path correctly points to android/ (standard structure) const correctPath = "project(':timesafari-daily-notification-plugin').projectDir = new File('../node_modules/@timesafari/daily-notification-plugin/android')"; const oldPluginPath = "project(':timesafari-daily-notification-plugin').projectDir = new File('../node_modules/@timesafari/daily-notification-plugin/android/plugin')"; // Check if it's using the old android/plugin/ path (needs fixing) if (content.includes('android/plugin')) { console.log('āš ļø capacitor.settings.gradle uses old path (android/plugin/) - fixing to standard structure'); content = content.replace( oldPluginPath, `// Plugin uses standard Capacitor structure: android/ (not android/plugin/) ${correctPath}` ); if (content !== originalContent) { fs.writeFileSync(SETTINGS_GRADLE_PATH, content); console.log('āœ… Fixed plugin path in capacitor.settings.gradle (android/plugin -> android)'); } } else if (content.includes(correctPath) || content.includes("android')")) { console.log('āœ… capacitor.settings.gradle has correct path (android/)'); } else { console.log('ā„¹ļø capacitor.settings.gradle doesn\'t reference the plugin or uses a different structure'); } } catch (error) { console.error('āŒ Error verifying capacitor.settings.gradle:', error.message); process.exit(1); } } /** * Run all fixes */ function fixAll() { console.log('šŸ”§ DailyNotification Plugin - Post-Sync Fix Script'); console.log('================================================\n'); fixCapacitorPlugins(); fixCapacitorSettingsGradle(); console.log('\nāœ… All fixes applied successfully!'); console.log('šŸ’” These fixes will persist until the next "npx cap sync android"'); } // Run if called directly if (import.meta.url === `file://${process.argv[1]}`) { fixAll(); } export { fixCapacitorPlugins, fixCapacitorSettingsGradle, fixAll };