Skip to content

Commit d636a84

Browse files
CopilotBunsDev
andauthored
refactor: simplify local-notifications patch validation constants
Agent-Logs-Url: https://github.com/OpenKnots/okcode/sessions/76a7e532-d68e-4b0b-9194-62dd020d7ae3 Co-authored-by: BunsDev <68980965+BunsDev@users.noreply.github.com>
1 parent d4fcd57 commit d636a84

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

scripts/patch-capacitor-local-notifications.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ const SWIFT_REPLACEMENTS: ReadonlyArray<[string, string]> = [
5252
].join("\n"),
5353
],
5454
];
55+
const LEGACY_PLUGIN_PATTERNS = [
56+
'call.getArray("notifications", JSObject.self)',
57+
'call.getArray("notifications")?.compactMap({ $0 as? JSObject })',
58+
"call.reject(",
59+
] as const;
60+
const REQUIRED_PATCHED_PATTERNS = [
61+
'call.getArray("notifications", []).compactMap({ $0 as? JSObject })',
62+
'call.getArray("types", []).compactMap({ $0 as? JSObject })',
63+
] as const;
64+
const FORBIDDEN_PATCHED_PATTERNS = [
65+
"call.reject(",
66+
'call.getArray("notifications", JSObject.self)',
67+
'call.getArray("notifications")?.compactMap({ $0 as? JSObject })',
68+
'call.getArray("types", JSObject.self)',
69+
'call.getArray("types")?.compactMap({ $0 as? JSObject })',
70+
] as const;
5571

5672
const HANDLER_PATCH = {
5773
search:
@@ -184,12 +200,13 @@ const HANDLER_PATCH = {
184200
};
185201

186202
function replaceAll(source: string, search: string, replacement: string): { updated: string; count: number } {
187-
const count = source.split(search).length - 1;
188-
if (count === 0) {
203+
const sourceParts = source.split(search);
204+
const count = sourceParts.length - 1;
205+
if (count <= 0) {
189206
return { updated: source, count: 0 };
190207
}
191208

192-
return { updated: source.split(search).join(replacement), count };
209+
return { updated: sourceParts.join(replacement), count };
193210
}
194211

195212
function assert(condition: unknown, message: string): asserts condition {
@@ -198,6 +215,13 @@ function assert(condition: unknown, message: string): asserts condition {
198215
}
199216
}
200217

218+
function isPluginSwiftPatched(source: string): boolean {
219+
return (
220+
REQUIRED_PATCHED_PATTERNS.every((pattern) => source.includes(pattern)) &&
221+
FORBIDDEN_PATCHED_PATTERNS.every((pattern) => !source.includes(pattern))
222+
);
223+
}
224+
201225
assert(existsSync(PACKAGE_FILE), `Missing local-notifications package: ${PACKAGE_FILE}`);
202226
assert(existsSync(TARGET_FILE), `Missing local-notifications Swift source: ${TARGET_FILE}`);
203227
assert(existsSync(HANDLER_FILE), `Missing local-notifications Swift source: ${HANDLER_FILE}`);
@@ -218,18 +242,10 @@ for (const [search, replacement] of SWIFT_REPLACEMENTS) {
218242
pluginChanges += result.count;
219243
}
220244

221-
const pluginHadKnownLegacyPattern =
222-
pluginOriginal.includes('call.getArray("notifications", JSObject.self)') ||
223-
pluginOriginal.includes('call.getArray("notifications")?.compactMap({ $0 as? JSObject })') ||
224-
pluginOriginal.includes("call.reject(");
225-
const pluginLooksPatched =
226-
pluginUpdated.includes('call.getArray("notifications", []).compactMap({ $0 as? JSObject })') &&
227-
pluginUpdated.includes('call.getArray("types", []).compactMap({ $0 as? JSObject })') &&
228-
!pluginUpdated.includes("call.reject(") &&
229-
!pluginUpdated.includes('call.getArray("notifications", JSObject.self)') &&
230-
!pluginUpdated.includes('call.getArray("notifications")?.compactMap({ $0 as? JSObject })') &&
231-
!pluginUpdated.includes('call.getArray("types", JSObject.self)') &&
232-
!pluginUpdated.includes('call.getArray("types")?.compactMap({ $0 as? JSObject })');
245+
const pluginHadKnownLegacyPattern = LEGACY_PLUGIN_PATTERNS.some((pattern) =>
246+
pluginOriginal.includes(pattern),
247+
);
248+
const pluginLooksPatched = isPluginSwiftPatched(pluginUpdated);
233249

234250
assert(
235251
pluginHadKnownLegacyPattern || pluginLooksPatched,

0 commit comments

Comments
 (0)