diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 473f487f..a73c94b9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -145,8 +145,9 @@ build-apk: - npx vite build -l error - cp dist/index.html dist/404.html - # Sync web assets to Capacitor Android project + # Sync web assets to Capacitor Android project and register local plugins - npx cap sync android + - node scripts/patch-cap-config.mjs # Build signed release APK - cd android && chmod +x gradlew && ./gradlew assembleRelease bundleRelease && cd .. diff --git a/package.json b/package.json index 007f383f..118bb680 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dev": "npm i --silent && vite", "build": "npm i --silent && vite build -l error && cp dist/index.html dist/404.html && echo 'Project built successfully!'", "test": "npm i --silent && tsc --noEmit && eslint && vitest run --reporter=dot --silent && vite build -l error && cp dist/index.html dist/404.html && echo 'All tests passed!'", + "cap:sync": "npx cap sync && node scripts/patch-cap-config.mjs", "keygen": "keytool -genkey -v -keystore android/app/my-upload-key.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias upload", "icons": "bash scripts/generate-icons.sh" }, diff --git a/scripts/patch-cap-config.mjs b/scripts/patch-cap-config.mjs new file mode 100644 index 00000000..2ad36070 --- /dev/null +++ b/scripts/patch-cap-config.mjs @@ -0,0 +1,49 @@ +#!/usr/bin/env node + +/** + * Patch capacitor.config.json to include local (non-SPM) plugin classes. + * + * `npx cap sync` regenerates the `packageClassList` array from SPM packages + * only, so local plugins compiled directly into the app binary (like + * SandboxPlugin) are not included. This script appends them after sync so + * the Capacitor bridge eagerly registers them at startup. + * + * Usage: node scripts/patch-cap-config.mjs + * Typically run after `npx cap sync`. + */ + +import { readFileSync, writeFileSync } from 'fs'; +import { resolve } from 'path'; + +/** Local plugin class names to ensure are registered. */ +const LOCAL_PLUGINS = ['SandboxPlugin']; + +const platforms = ['ios/App/App', 'android/app/src/main/assets']; + +for (const platform of platforms) { + const configPath = resolve(platform, 'capacitor.config.json'); + + let config; + try { + config = JSON.parse(readFileSync(configPath, 'utf-8')); + } catch { + // Platform may not exist or config not yet generated — skip. + continue; + } + + const classList = new Set(config.packageClassList ?? []); + let changed = false; + + for (const plugin of LOCAL_PLUGINS) { + if (!classList.has(plugin)) { + classList.add(plugin); + changed = true; + } + } + + if (changed) { + config.packageClassList = [...classList]; + writeFileSync(configPath, JSON.stringify(config, null, '\t') + '\n'); + console.log(`Patched ${configPath}: added ${LOCAL_PLUGINS.join(', ')}`); + } +}