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+ / o v e r r i d e f u n g e t J S M a i n M o d u l e N a m e \( \) : S t r i n g = " \. e x p o \/ \. v i r t u a l - m e t r o - e n t r y " / ,
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