1import chalk from 'chalk';
2
3import { TransformPipeline } from '.';
4
5export function postTransforms(versionName: string): TransformPipeline {
6  return {
7    logHeader(filePath: string) {
8      console.log(`Post-transforming ${chalk.magenta(filePath)}:`);
9    },
10    transforms: [
11      // react-native
12      {
13        paths: ['RCTRedBox.m', 'RCTLog.mm'],
14        replace: /#if (ABI\d+_\d+_\d+)RCT_DEBUG/g,
15        with: '#if $1RCT_DEV',
16      },
17      {
18        paths: ['NSTextStorage+FontScaling.h', 'NSTextStorage+FontScaling.m'],
19        replace: /NSTextStorage \(FontScaling\)/,
20        with: `NSTextStorage (${versionName}FontScaling)`,
21      },
22      {
23        paths: [
24          'NSTextStorage+FontScaling.h',
25          'NSTextStorage+FontScaling.m',
26          'RCTTextShadowView.m',
27        ],
28        replace: /\b(scaleFontSizeToFitSize|scaleFontSizeWithRatio|compareToSize)\b/g,
29        with: `${versionName.toLowerCase()}_rct_$1`,
30      },
31      {
32        paths: 'RCTWebView.m',
33        replace: /@"ABI\d+_\d+_\d+React-js-navigation"/,
34        with: '@"react-js-navigation"',
35      },
36      {
37        replace: new RegExp(`FB${versionName}ReactNativeSpec`, 'g'),
38        with: 'FBReactNativeSpec',
39      },
40      {
41        replace: new RegExp('\\b(Native\\w+Spec)\\b', 'g'),
42        with: `${versionName}$1`,
43      },
44      {
45        paths: 'RCTInspectorPackagerConnection.m',
46        replace: /\b(RECONNECT_DELAY_MS)\b/g,
47        with: `${versionName}$1`,
48      },
49      {
50        paths: 'RCTView.m',
51        replace: /\b(SwitchAccessibilityTrait)\b/g,
52        with: `${versionName}$1`,
53      },
54      {
55        paths: 'RCTSpringAnimation.m',
56        replace: /\b(MAX_DELTA_TIME)\b/g,
57        with: `${versionName}$1`,
58      },
59      {
60        paths: 'ModuleRegistry.cpp',
61        replace: /(std::string normalizeName\(std::string name\) \{)/,
62        with: `$1\n  if (name.compare(0, ${versionName.length}, "${versionName}") == 0) {\n    name = name.substr(${versionName.length});\n  }\n`,
63      },
64      {
65        paths: 'ModuleRegistry.cpp',
66        replace: /(\(name\.compare\(\d+, \d+, ")([^"]+)(RCT"\))/,
67        with: '$1$3',
68      },
69      {
70        paths: ['RCTSRWebSocket.h', 'UIView+Private.h'],
71        replace: /@interface (\w+) \((CertificateAdditions|Private)\)/g,
72        with: `@interface $1 (${versionName}$2)`,
73      },
74      {
75        // Fix prefixing imports from React-bridging
76        paths: 'ReactCommon',
77        replace: new RegExp(`(React)\\/${versionName}(bridging)\\/`, 'g'),
78        with: `$1/$2/${versionName}`,
79      },
80
81      // Universal modules
82      {
83        paths: `UniversalModules/${versionName}EXScoped`,
84        replace: /(EXScopedABI\d+_\d+_\d+ReactNative)/g,
85        with: 'EXScopedReactNative',
86      },
87      {
88        paths: `${versionName}EXFileSystem`,
89        replace: new RegExp(`NSData\\+${versionName}EXFileSystem\\.h`, 'g'),
90        with: `${versionName}NSData+EXFileSystem.h`,
91      },
92      {
93        paths: [
94          `${versionName}EXNotifications`,
95          `${versionName}EXUpdates`,
96          `${versionName}EXJSONUtils`,
97        ],
98        replace: new RegExp(
99          `NSDictionary\\+${versionName}(EXNotificationsVerifyingClass|EXJSONUtils)\\.h`,
100          'g'
101        ),
102        with: `${versionName}NSDictionary+$1.h`,
103      },
104      {
105        paths: [
106          `${versionName}EXNotifications`,
107          `${versionName}EXAppState`,
108          `${versionName}EXVersionManager`,
109        ],
110        replace: new RegExp(`EXModuleRegistryHolder${versionName}React`, 'g'),
111        with: 'EXModuleRegistryHolderReact',
112      },
113      {
114        // Versioned ExpoKit has to use versioned modules provider
115        paths: 'EXVersionManager.mm',
116        replace: /@"(ExpoModulesProvider)"/,
117        with: `@"${versionName}$1"`,
118      },
119
120      // react-native-maps
121      {
122        paths: 'AIRMapWMSTile',
123        replace: /\b(TileOverlay)\b/g,
124        with: `${versionName}$1`,
125      },
126      {
127        paths: 'AIRGoogleMapWMSTile',
128        replace: /\b(WMSTileOverlay)\b/g,
129        with: `${versionName}$1`,
130      },
131
132      // react-native-svg
133      {
134        paths: 'RNSVGRenderable.m',
135        replace: /\b(saturate)\(/g,
136        with: `${versionName}$1(`,
137      },
138      {
139        paths: 'RNSVGPainter.m',
140        replace: /\b(PatternFunction)\b/g,
141        with: `${versionName}$1`,
142      },
143      {
144        paths: 'RNSVGFontData.m',
145        replace: /\b(AbsoluteFontWeight|bolder|lighter|nearestFontWeight)\(/gi,
146        with: `${versionName}$1(`,
147      },
148      {
149        paths: 'RNSVGTSpan.m',
150        replace: new RegExp(`\\b(${versionName}RNSVGTopAlignedLabel\\s*\\*\\s*label)\\b`, 'gi'),
151        with: 'static $1',
152      },
153      {
154        paths: 'RNSVGMarker.m',
155        replace: /\b(deg2rad)\b/g,
156        with: `${versionName}$1`,
157      },
158      {
159        paths: 'RNSVGMarkerPosition.m',
160        replace:
161          /\b(PathIsDone|rad2deg|SlopeAngleRadians|CurrentAngle|subtract|ExtractPathElementFeatures|UpdateFromPathElement)\b/g,
162        with: `${versionName}$1`,
163      },
164      {
165        paths: 'RNSVGMarkerPosition.m',
166        replace:
167          /\b(positions_|element_index_|origin_|subpath_start_|in_slope_|out_slope_|auto_start_reverse_)\b/g,
168        with: `${versionName}$1`,
169      },
170      {
171        paths: 'RNSVGPathMeasure.m',
172        replace: /\b(distance|subdivideBezierAtT)\b/g,
173        with: `${versionName}$1`,
174      },
175
176      // react-native-webview
177      {
178        paths: 'RNCWebView.m',
179        replace: new RegExp(`#import "objc/${versionName}runtime\\.h"`, ''),
180        with: '#import "objc/runtime.h"',
181      },
182      {
183        paths: 'RNCWebView.m',
184        replace: /\b(_SwizzleHelperWK)\b/g,
185        with: `${versionName}$1`,
186      },
187      {
188        // see issue: https://github.com/expo/expo/issues/4463
189        paths: 'RNCWebView.m',
190        replace: /MessageHandlerName = @"ABI\d+_\d+_\d+ReactNativeWebView";/,
191        with: `MessageHandlerName = @"ReactNativeWebView";`,
192      },
193      {
194        paths: 'EXVersionManager.mm',
195        replace: /\[(RNCWebView)/,
196        with: `[${versionName}$1`,
197      },
198
199      // react-native-reanimated
200      {
201        paths: 'EXVersionManager.mm',
202        replace: /(_bridge_reanimated)/g,
203        with: `${versionName}$1`,
204      },
205      {
206        paths: 'EXVersionManager.mm',
207        replace: /\b(REA)/g,
208        with: `${versionName}$1`,
209      },
210      {
211        paths: 'NativeProxy.mm',
212        replace: /@"ABI\d+_\d+_\d+RCTView"/g,
213        with: `@"RCTView"`,
214      },
215
216      // react-native-shared-element
217      {
218        paths: 'RNSharedElementNode.m',
219        replace: /\b(NSArray\s*\*\s*_imageResolvers)\b/,
220        with: 'static $1',
221      },
222
223      // react-native-safe-area-context
224      {
225        paths: [
226          'RNCSafeAreaUtils.h',
227          'RNCSafeAreaUtils.m',
228          'RNCSafeAreaProvider.m',
229          'RNCSafeAreaView.m',
230        ],
231        replace: /\b(UIEdgeInsetsEqualToEdgeInsetsWithThreshold)\b/g,
232        with: `${versionName}$1`,
233      },
234    ],
235  };
236}
237