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      // Universal modules
76      {
77        paths: `UniversalModules/${versionName}EXScoped`,
78        replace: /(EXScopedABI\d+_\d+_\d+ReactNative)/g,
79        with: 'EXScopedReactNative',
80      },
81      {
82        paths: `${versionName}EXFileSystem`,
83        replace: new RegExp(`NSData\\+${versionName}EXFileSystem\\.h`, 'g'),
84        with: `${versionName}NSData+EXFileSystem.h`,
85      },
86      {
87        paths: [
88          `${versionName}EXNotifications`,
89          `${versionName}EXUpdates`,
90          `${versionName}EXJSONUtils`,
91        ],
92        replace: new RegExp(
93          `NSDictionary\\+${versionName}(EXNotificationsVerifyingClass|EXJSONUtils)\\.h`,
94          'g'
95        ),
96        with: `${versionName}NSDictionary+$1.h`,
97      },
98      {
99        paths: [`${versionName}EXNotifications`, `${versionName}EXAppState`],
100        replace: new RegExp(`EXModuleRegistryHolder${versionName}React`, 'g'),
101        with: 'EXModuleRegistryHolderReact',
102      },
103      {
104        // Versioned ExpoKit has to use versioned modules provider
105        paths: 'EXVersionManager.mm',
106        replace: /@"(ExpoModulesProvider)"/,
107        with: `@"${versionName}$1"`,
108      },
109
110      // react-native-maps
111      {
112        paths: 'AIRMapWMSTile',
113        replace: /\b(TileOverlay)\b/g,
114        with: `${versionName}$1`,
115      },
116      {
117        paths: 'AIRGoogleMapWMSTile',
118        replace: /\b(WMSTileOverlay)\b/g,
119        with: `${versionName}$1`,
120      },
121
122      // react-native-svg
123      {
124        paths: 'RNSVGRenderable.m',
125        replace: /\b(saturate)\(/g,
126        with: `${versionName}$1(`,
127      },
128      {
129        paths: 'RNSVGPainter.m',
130        replace: /\b(PatternFunction)\b/g,
131        with: `${versionName}$1`,
132      },
133      {
134        paths: 'RNSVGFontData.m',
135        replace: /\b(AbsoluteFontWeight|bolder|lighter|nearestFontWeight)\(/gi,
136        with: `${versionName}$1(`,
137      },
138      {
139        paths: 'RNSVGTSpan.m',
140        replace: new RegExp(`\\b(${versionName}RNSVGTopAlignedLabel\\s*\\*\\s*label)\\b`, 'gi'),
141        with: 'static $1',
142      },
143      {
144        paths: 'RNSVGMarker.m',
145        replace: /\b(deg2rad)\b/g,
146        with: `${versionName}$1`,
147      },
148      {
149        paths: 'RNSVGMarkerPosition.m',
150        replace:
151          /\b(PathIsDone|rad2deg|SlopeAngleRadians|CurrentAngle|subtract|ExtractPathElementFeatures|UpdateFromPathElement)\b/g,
152        with: `${versionName}$1`,
153      },
154      {
155        paths: 'RNSVGMarkerPosition.m',
156        replace:
157          /\b(positions_|element_index_|origin_|subpath_start_|in_slope_|out_slope_|auto_start_reverse_)\b/g,
158        with: `${versionName}$1`,
159      },
160      {
161        paths: 'RNSVGPathMeasure.m',
162        replace: /\b(distance|subdivideBezierAtT)\b/g,
163        with: `${versionName}$1`,
164      },
165
166      // react-native-webview
167      {
168        paths: 'RNCWebView.m',
169        replace: new RegExp(`#import "objc/${versionName}runtime\\.h"`, ''),
170        with: '#import "objc/runtime.h"',
171      },
172      {
173        paths: 'RNCWebView.m',
174        replace: /\b(_SwizzleHelperWK)\b/g,
175        with: `${versionName}$1`,
176      },
177      {
178        // see issue: https://github.com/expo/expo/issues/4463
179        paths: 'RNCWebView.m',
180        replace: /MessageHandlerName = @"ABI\d+_\d+_\d+ReactNativeWebView";/,
181        with: `MessageHandlerName = @"ReactNativeWebView";`,
182      },
183      {
184        paths: 'EXVersionManager.mm',
185        replace: /\[(RNCWebView)/,
186        with: `[${versionName}$1`,
187      },
188
189      // react-native-reanimated
190      {
191        paths: 'EXVersionManager.mm',
192        replace: /(_bridge_reanimated)/g,
193        with: `${versionName}$1`,
194      },
195      {
196        paths: 'EXVersionManager.mm',
197        replace: /\b(REA)/g,
198        with: `${versionName}$1`,
199      },
200      {
201        paths: 'NativeProxy.mm',
202        replace: /@"ABI\d+_\d+_\d+RCTView"/g,
203        with: `@"RCTView"`,
204      },
205
206      // react-native-shared-element
207      {
208        paths: 'RNSharedElementNode.m',
209        replace: /\b(NSArray\s*\*\s*_imageResolvers)\b/,
210        with: 'static $1',
211      },
212
213      // react-native-safe-area-context
214      {
215        paths: [
216          'RNCSafeAreaUtils.h',
217          'RNCSafeAreaUtils.m',
218          'RNCSafeAreaProvider.m',
219          'RNCSafeAreaView.m',
220        ],
221        replace: /\b(UIEdgeInsetsEqualToEdgeInsetsWithThreshold)\b/g,
222        with: `${versionName}$1`,
223      },
224    ],
225  };
226}
227