-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.plugin.js
More file actions
76 lines (67 loc) · 2.53 KB
/
app.plugin.js
File metadata and controls
76 lines (67 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Expo Config Plugin for react-native-github-ota.
*
* Automatically patches MainApplication.kt during `expo prebuild`
* to load downloaded OTA bundles on app startup.
*
* Usage in app.json / app.config.js:
* "plugins": ["react-native-github-ota"]
*/
const { withMainApplication } = require("@expo/config-plugins");
const OTA_IMPORT = "import java.io.File";
const OTA_METHOD = [
"",
" override fun getJSBundleFile(): String? {",
' val otaBundle = File(applicationContext.filesDir, "ota/index.android.bundle")',
" if (!otaBundle.exists()) return null",
"",
" // If the APK was updated after the OTA bundle was downloaded, the OTA is stale",
" try {",
" val appInfo = applicationContext.packageManager.getPackageInfo(applicationContext.packageName, 0)",
" if (appInfo.lastUpdateTime > otaBundle.lastModified()) {",
" otaBundle.delete()",
' File(applicationContext.filesDir, "ota/meta.json").delete()',
" return null",
" }",
" } catch (_: Exception) {}",
"",
" return otaBundle.absolutePath",
" }",
].join("\n");
function withOtaBundleLoader(config) {
return withMainApplication(config, (mod) => {
let contents = mod.modResults.contents;
// Skip if already patched
if (contents.includes("getJSBundleFile")) {
return mod;
}
// Add import for java.io.File if missing
if (!contents.includes(OTA_IMPORT)) {
contents = contents.replace(
/(import\s+[^\n]+\n)(\s*\n*class\s)/,
`$1${OTA_IMPORT}\n\n$2`,
);
}
// Insert getJSBundleFile() after getUseDeveloperSupport()
const primaryPattern =
/(override\s+fun\s+getUseDeveloperSupport\(\)\s*:\s*Boolean\s*=\s*BuildConfig\.DEBUG\s*\n)/;
if (primaryPattern.test(contents)) {
contents = contents.replace(primaryPattern, `$1${OTA_METHOD}\n`);
} else {
// Fallback: insert after getJSMainModuleName()
const fallbackPattern =
/(override\s+fun\s+getJSMainModuleName\(\)\s*:\s*String\s*=\s*[^\n]+\n)/;
if (fallbackPattern.test(contents)) {
contents = contents.replace(fallbackPattern, `$1${OTA_METHOD}\n`);
} else {
console.warn(
"[react-native-github-ota] Could not auto-patch MainApplication.kt — see README for manual instructions.",
);
return mod;
}
}
mod.modResults.contents = contents;
return mod;
});
}
module.exports = withOtaBundleLoader;