-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall.js
More file actions
192 lines (177 loc) · 7.6 KB
/
Copy pathpostinstall.js
File metadata and controls
192 lines (177 loc) · 7.6 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* postinstall.js
*
* Patches native Mapbox libraries to fix compilation errors with Mapbox SDK v11.
*
* 1. @youssefhenna/expo-mapbox-navigation – LifecycleOwner null-safe cast
* 2. @youssefhenna/expo-mapbox-navigation – Remove deprecated .voiceUnits() calls
* 3. @rnmapbox/maps – Comment out deprecated style properties removed in SDK v11.11
*/
const fs = require('fs');
const path = require('path');
// ─── Helper ────────────────────────────────────────────────────────────────────
function patchFile(filePath, patches, label) {
if (!fs.existsSync(filePath)) {
console.warn(`[postinstall] Skipping ${label}: file not found at`, filePath);
return;
}
let content = fs.readFileSync(filePath, 'utf8');
let changed = false;
for (const { find, replace, check } of patches) {
if (check && content.includes(check)) {
console.log(`[postinstall] ${label}: patch already applied (found "${check}"), skipping.`);
continue;
}
if (content.includes(find)) {
content = content.replace(find, replace);
changed = true;
console.log(`[postinstall] ${label}: applied patch.`);
} else {
console.warn(`[postinstall] ${label}: target code not found, library may have changed.`);
}
}
if (changed) {
fs.writeFileSync(filePath, content, 'utf8');
console.log(`[postinstall] ${label}: file saved.`);
}
}
// ─── 1. Patch ExpoMapboxNavigationView.kt ──────────────────────────────────────
const navViewFile = path.join(
__dirname,
'node_modules',
'@youssefhenna',
'expo-mapbox-navigation',
'android', 'src', 'main', 'java', 'expo', 'modules', 'mapboxnavigation',
'ExpoMapboxNavigationView.kt'
);
patchFile(navViewFile, [
// 1a. LifecycleOwner null-safe cast
{
find: ` init {
this.setViewTreeLifecycleOwner(
appContext.activityProvider?.currentActivity as LifecycleOwner
)
}`,
replace: ` init {
// Safe null-check: under New Architecture (Fabric), currentActivity can be null
// during view construction. Force-casting null as LifecycleOwner throws
// NullPointerException -> InvocationTargetException. Use a safe cast instead.
val lifecycleOwner = appContext.activityProvider?.currentActivity as? LifecycleOwner
if (lifecycleOwner != null) {
this.setViewTreeLifecycleOwner(lifecycleOwner)
}
}`,
check: 'as? LifecycleOwner',
},
// 1b. Remove .voiceUnits() – removed from Navigation SDK v3.11+
{
find: '.voiceUnits(com.mapbox.api.directions.v5.DirectionsCriteria.IMPERIAL)',
replace: '// .voiceUnits(com.mapbox.api.directions.v5.DirectionsCriteria.IMPERIAL)',
check: '// .voiceUnits(',
},
], 'expo-mapbox-navigation');
// ─── 2. Patch ExpoMapboxNavigationModule.kt ─────────────────────────────────────
const navModuleFile = path.join(
__dirname,
'node_modules',
'@youssefhenna',
'expo-mapbox-navigation',
'android', 'src', 'main', 'java', 'expo', 'modules', 'mapboxnavigation',
'ExpoMapboxNavigationModule.kt'
);
patchFile(navModuleFile, [
{
find: ` (activity as LifecycleOwner).lifecycleScope.launch(Dispatchers.Main) {
if (!MapboxNavigationApp.isSetup()) {
MapboxNavigationApp.setup {
NavigationOptions.Builder(activity.applicationContext).build()
}
}
MapboxNavigationApp.attach(activity as LifecycleOwner)`,
replace: ` val currentAct = appContext.activityProvider?.currentActivity ?: return@OnActivityEntersForeground
(currentAct as LifecycleOwner).lifecycleScope.launch(Dispatchers.Main) {
if (!MapboxNavigationApp.isSetup()) {
MapboxNavigationApp.setup {
NavigationOptions.Builder(currentAct.applicationContext).build()
}
}
MapboxNavigationApp.attach(currentAct as LifecycleOwner)`,
check: 'val currentAct = appContext.activityProvider',
},
], 'expo-mapbox-navigation-module');
// ─── 3. Patch RNMBXStyleFactory.kt (@rnmapbox/maps) ────────────────────────────
const styleFactoryFile = path.join(
__dirname,
'node_modules',
'@rnmapbox',
'maps',
'android', 'src', 'main', 'java', 'com', 'rnmapbox', 'rnmbx',
'components', 'styles',
'RNMBXStyleFactory.kt'
);
// These properties were removed in Mapbox Maps SDK v11.11 but are still
// referenced by @rnmapbox/maps v10.2.10. We comment out the body of each
// setter so the function signature remains (callers still reference it in the
// when-block) but the unresolved SDK call is removed.
const deprecatedSetters = [
{ name: 'fillPatternCrossFade', layer: 'FillLayer', layerProp: 'fillPatternCrossFade', logTag: 'RNMBXFill', valueType: 'Double' },
{ name: 'linePatternCrossFade', layer: 'LineLayer', layerProp: 'linePatternCrossFade', logTag: 'RNMBXLine', valueType: 'Double' },
{ name: 'fillExtrusionPatternCrossFade', layer: 'FillExtrusionLayer', layerProp: 'fillExtrusionPatternCrossFade', logTag: 'RNMBXFillExtrusion', valueType: 'Double' },
];
// circleElevationReference uses enum instead of Double
const circleElevRefFind = ` fun setCircleElevationReference(layer: CircleLayer, styleValue: RNMBXStyleValue ) {
if (styleValue.isExpression()) {
val expression = styleValue.getExpression()
if (expression != null) {
layer.circleElevationReference(expression)
} else {
Logger.e("RNMBXCircle", "Expression for circleElevationReference is null")
}
} else {
layer.circleElevationReference(CircleElevationReference.valueOf(styleValue.getEnumName()))
}
}`;
const circleElevRefReplace = ` fun setCircleElevationReference(layer: CircleLayer, styleValue: RNMBXStyleValue ) {
/*
if (styleValue.isExpression()) {
val expression = styleValue.getExpression()
if (expression != null) {
layer.circleElevationReference(expression)
} else {
Logger.e("RNMBXCircle", "Expression for circleElevationReference is null")
}
} else {
layer.circleElevationReference(CircleElevationReference.valueOf(styleValue.getEnumName()))
}
*/
}`;
const stylePatches = deprecatedSetters.map(({ name, layer, layerProp, logTag, valueType }) => {
const getter = valueType === 'Double' ? 'getDouble' : 'getInt';
const find = ` fun set${name.charAt(0).toUpperCase() + name.slice(1)}(layer: ${layer}, styleValue: RNMBXStyleValue ) {
if (styleValue.isExpression()) {
val expression = styleValue.getExpression()
if (expression != null) {
layer.${layerProp}(expression)
} else {
Logger.e("${logTag}", "Expression for ${layerProp} is null")
}
} else {
val value = styleValue.${getter}(VALUE_KEY)
if (value != null) {
layer.${layerProp}(value)
} else {
Logger.e("${logTag}", "value for ${layerProp} is null")
}
}
}`;
const replace = find
.replace(` if (styleValue`, ` /*\n if (styleValue`)
.replace(` }\n }`, ` */\n }`);
return { find, replace, check: null };
});
stylePatches.push({
find: circleElevRefFind,
replace: circleElevRefReplace,
check: null,
});
patchFile(styleFactoryFile, stylePatches, 'rnmapbox-maps-style-factory');