You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
4.3 KiB

#!/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 - Corrects plugin path from android/ to android/plugin/
*
* This script should run automatically after 'npx cap sync android'
* to fix issues with Capacitor's auto-generated files.
*
* @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 point to android/plugin/ instead of android/
*/
function fixCapacitorSettingsGradle() {
console.log('🔧 Fixing 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 already points to android/plugin
if (content.includes('android/plugin')) {
console.log('✅ capacitor.settings.gradle already has correct path (android/plugin)');
return;
}
// Check if we need to fix the path (points to android but should be android/plugin)
if (content.includes("project(':timesafari-daily-notification-plugin').projectDir = new File('../node_modules/@timesafari/daily-notification-plugin/android')")) {
// Replace the path
content = content.replace(
"project(':timesafari-daily-notification-plugin').projectDir = new File('../node_modules/@timesafari/daily-notification-plugin/android')",
`// NOTE: Plugin module is in android/plugin/ subdirectory, not android root
// This file is auto-generated by Capacitor, but must be manually corrected for this plugin structure
project(':timesafari-daily-notification-plugin').projectDir = new File('../node_modules/@timesafari/daily-notification-plugin/android/plugin')`
);
fs.writeFileSync(SETTINGS_GRADLE_PATH, content);
console.log('✅ Fixed plugin path in capacitor.settings.gradle (android -> android/plugin)');
} else {
console.log('ℹ️ capacitor.settings.gradle doesn\'t reference the plugin or uses a different structure');
}
} catch (error) {
console.error('❌ Error fixing 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 };