Skip to content

Commit 684630b

Browse files
committed
adapter pushy for Expo
1 parent b316b43 commit 684630b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { withDangerousMod } = require('@expo/config-plugins');
2+
const { mergeContents } = require('@expo/config-plugins/build/utils/generateCode');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const withUpdate = (config) => {
7+
config = withDangerousMod(config, [
8+
'ios',
9+
async (config) => {
10+
const projectName = config.modRequest.projectName;
11+
const appDelegatePath = path.join(config.modRequest.platformProjectRoot, projectName, 'AppDelegate.mm');
12+
const contents = fs.readFileSync(appDelegatePath, 'utf-8');
13+
14+
const newContents = mergeContents({
15+
src: contents,
16+
newSrc: '#import "RCTPushy.h"',
17+
anchor: '#import <React/RCTBundleURLProvider.h>',
18+
offset: 1,
19+
tag: 'react-native-update-header',
20+
comment: '//',
21+
});
22+
23+
const finalContents = newContents.contents.replace(
24+
'return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];',
25+
'return [RCTPushy bundleURL];'
26+
);
27+
28+
fs.writeFileSync(appDelegatePath, finalContents);
29+
30+
return config;
31+
},
32+
]);
33+
34+
config = withDangerousMod(config, [
35+
'android',
36+
async (config) => {
37+
const mainApplicationPath = path.join(config.modRequest.platformProjectRoot, 'app/src/main/java', ...config.android.package.split('.'), 'MainApplication.kt');
38+
const contents = fs.readFileSync(mainApplicationPath, 'utf-8');
39+
40+
// 添加 import
41+
const contentsWithImport = mergeContents({
42+
src: contents,
43+
newSrc: 'import cn.reactnative.modules.update.UpdateContext',
44+
anchor: 'package',
45+
offset: 1,
46+
tag: 'react-native-update-import',
47+
comment: '//',
48+
});
49+
50+
const bundleMethodCode = 'override fun getJSBundleFile(): String? = UpdateContext.getBundleUrl(this@MainApplication)';
51+
const finalContents = contentsWithImport.contents.replace(
52+
/override fun getJSMainModuleName\(\): String = "\.expo\/\.virtual-metro-entry"/,
53+
`$&\n\n ${bundleMethodCode}`
54+
);
55+
56+
fs.writeFileSync(mainApplicationPath, finalContents);
57+
58+
return config;
59+
},
60+
]);
61+
62+
return config;
63+
};
64+
65+
module.exports = withUpdate;

0 commit comments

Comments
 (0)