1import { TransformPipeline } from '.';
2
3/**
4 * These modification will be run against `ios/Exponent/kernel` directory.
5 * If you need to modify other files from the `ios` directory then find
6 * a better place for it or refactor the function that depends on this list.
7 * The nature of these changes is that they're not permanent and at one point
8 * of time (SDK drop) these should be rollbacked.
9 * @param versionName e.g. 21.0.0, 37.0.0, etc.
10 * @param rollback This flag indicates whether the change should be rollbacked.
11 */
12export function kernelFilesTransforms(
13  versionName: string,
14  rollback: boolean = false
15): TransformPipeline {
16  return {
17    transforms: [
18      {
19        paths: ['EXAppViewController.m'],
20        ...withRollback(rollback, {
21          replace: /(?<=#import <React\/RCTAppearance\.h>)/,
22          with: `\n#if __has_include(<${versionName}React/${versionName}RCTAppearance.h>)\n#import <${versionName}React/${versionName}RCTAppearance.h>\n#endif`,
23        }),
24      },
25      {
26        paths: ['EXAppViewController.m'],
27        ...withRollback(rollback, {
28          replace: /(?<=\sRCTOverrideAppearancePreference\(appearancePreference\);)/,
29          with: `\n#if __has_include(<${versionName}React/${versionName}RCTAppearance.h>)\n  ${versionName}RCTOverrideAppearancePreference(appearancePreference);\n#endif`,
30        }),
31      },
32    ],
33  };
34}
35
36type Replacement = {
37  replace: RegExp | string;
38  with: string;
39};
40
41/**
42 * If `rollback = true` then this function either return `rollbackReplacement`
43 * or if it's not provided it used `replace` from `replacement` argument.
44 * For the latter case, ensure you're not constructing `replacement.with` field with
45 * any capture group from `replacement.replace` part, because it will be inlined directly
46 * and additionally ensure if you don't want to escape some characters ��.
47 */
48function withRollback(
49  rollback: boolean,
50  replacement: Replacement,
51  rollbackReplacement?: Replacement
52): Replacement {
53  return rollback ? rollbackReplacement ?? { replace: replacement.with, with: '' } : replacement;
54}
55