Skip to content

Commit cb1b722

Browse files
committed
adapter for Expo
1 parent 90f6b7b commit cb1b722

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

app.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('react-native-update/plugin/withUpdate');

plugin/withUpdate.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const { withDangerousMod } = require('@expo/config-plugins');
2+
const {
3+
mergeContents,
4+
} = require('@expo/config-plugins/build/utils/generateCode');
5+
const fs = require('fs');
6+
const path = require('path');
7+
8+
const withUpdate = config => {
9+
config = withDangerousMod(config, [
10+
'ios',
11+
async config => {
12+
const projectName = config.modRequest.projectName;
13+
const appDelegatePath = path.join(
14+
config.modRequest.platformProjectRoot,
15+
projectName,
16+
'AppDelegate.mm',
17+
);
18+
const contents = fs.readFileSync(appDelegatePath, 'utf-8');
19+
20+
const newContents = mergeContents({
21+
src: contents,
22+
newSrc: '#import "RCTPushy.h"',
23+
anchor: '#import <React/RCTBundleURLProvider.h>',
24+
offset: 1,
25+
tag: 'react-native-update-header',
26+
comment: '//',
27+
});
28+
29+
const finalContents = newContents.contents.replace(
30+
'return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];',
31+
'return [RCTPushy bundleURL];',
32+
);
33+
34+
fs.writeFileSync(appDelegatePath, finalContents);
35+
36+
return config;
37+
},
38+
]);
39+
40+
config = withDangerousMod(config, [
41+
'android',
42+
async config => {
43+
const buildGradlePath = path.join(
44+
config.modRequest.platformProjectRoot,
45+
'app/build.gradle',
46+
);
47+
const gradleContents = fs.readFileSync(buildGradlePath, 'utf-8');
48+
49+
const buildTimeConfig = `
50+
int MILLIS_IN_MINUTE = 1000 * 60
51+
int minutesSinceEpoch = System.currentTimeMillis() / MILLIS_IN_MINUTE
52+
53+
android {
54+
buildTypes {
55+
debug {
56+
resValue("string", "pushy_build_time", "0")
57+
}
58+
release {
59+
resValue("string", "pushy_build_time", "\${minutesSinceEpoch}")
60+
}
61+
}
62+
}
63+
`;
64+
65+
const newContents = mergeContents({
66+
src: gradleContents,
67+
newSrc: buildTimeConfig,
68+
anchor: 'android {',
69+
offset: 0,
70+
tag: 'react-native-update-buildtime',
71+
comment: '//',
72+
});
73+
74+
fs.writeFileSync(buildGradlePath, newContents.contents);
75+
76+
const mainApplicationPath = path.join(
77+
config.modRequest.platformProjectRoot,
78+
'app/src/main/java',
79+
...config.android.package.split('.'),
80+
'MainApplication.kt',
81+
);
82+
const mainApplicationContents = fs.readFileSync(
83+
mainApplicationPath,
84+
'utf-8',
85+
);
86+
87+
const contentsWithImport = mergeContents({
88+
src: mainApplicationContents,
89+
newSrc: 'import cn.reactnative.modules.update.UpdateContext',
90+
anchor: 'package',
91+
offset: 1,
92+
tag: 'react-native-update-import',
93+
comment: '//',
94+
});
95+
96+
const bundleMethodCode =
97+
'override fun getJSBundleFile(): String? = UpdateContext.getBundleUrl(this@MainApplication)';
98+
const finalContents = contentsWithImport.contents.replace(
99+
/override fun getJSMainModuleName\(\): String = "\.expo\/\.virtual-metro-entry"/,
100+
`$&\n\n ${bundleMethodCode}`,
101+
);
102+
103+
fs.writeFileSync(mainApplicationPath, finalContents);
104+
105+
return config;
106+
},
107+
]);
108+
109+
return config;
110+
};
111+
112+
module.exports = withUpdate;

0 commit comments

Comments
 (0)