|
| 1 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | + |
| 4 | +const ROOT_DIR = resolve(process.cwd()); |
| 5 | +const TARGET_FILE = resolve( |
| 6 | + ROOT_DIR, |
| 7 | + "node_modules/@capacitor/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsPlugin.swift", |
| 8 | +); |
| 9 | + |
| 10 | +const REPLACEMENTS: ReadonlyArray<[RegExp, string]> = [ |
| 11 | + [ |
| 12 | + /guard let notifications = call\.getArray\("notifications", JSObject\.self\) else \{/g, |
| 13 | + 'guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }) else {', |
| 14 | + ], |
| 15 | + [ |
| 16 | + /guard let notifications = call\.getArray\("notifications", JSObject\.self\), notifications\.count > 0 else \{/g, |
| 17 | + 'guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {', |
| 18 | + ], |
| 19 | + [ |
| 20 | + /guard let types = call\.getArray\("types", JSObject\.self\) else \{/g, |
| 21 | + 'guard let types = call.getArray("types")?.compactMap({ $0 as? JSObject }) else {', |
| 22 | + ], |
| 23 | + [ |
| 24 | + /return bridge\?\.localURL\(fromWebURL: webURL\)/g, |
| 25 | + [ |
| 26 | + " switch webURL.scheme {", |
| 27 | + ' case "res":', |
| 28 | + " return bridge?.config.appLocation.appendingPathComponent(webURL.path)", |
| 29 | + ' case "file":', |
| 30 | + " return webURL", |
| 31 | + " default:", |
| 32 | + " return nil", |
| 33 | + " }", |
| 34 | + ].join("\n"), |
| 35 | + ], |
| 36 | +]; |
| 37 | + |
| 38 | +if (!existsSync(TARGET_FILE)) { |
| 39 | + console.log(`Skipping Capacitor local-notifications patch; missing file: ${TARGET_FILE}`); |
| 40 | + process.exit(0); |
| 41 | +} |
| 42 | + |
| 43 | +const original = readFileSync(TARGET_FILE, "utf8"); |
| 44 | +let updated = original; |
| 45 | + |
| 46 | +for (const [pattern, replacement] of REPLACEMENTS) { |
| 47 | + updated = updated.replace(pattern, replacement); |
| 48 | +} |
| 49 | + |
| 50 | +if (updated !== original) { |
| 51 | + writeFileSync(TARGET_FILE, updated); |
| 52 | + console.log(`Patched ${TARGET_FILE}`); |
| 53 | +} else { |
| 54 | + console.log(`No patch needed for ${TARGET_FILE}`); |
| 55 | +} |
0 commit comments