1const { withDangerousMod, IOSConfig } = require('@expo/config-plugins');
2const fs = require('fs-extra');
3
4// Append this block to the AppDelegate to fix https://stackoverflow.com/a/56160671/4047926
5const customBlockObjc = `// [Custom]: Fixes \`Unable to find module for DevMenu\`
6#if RCT_DEV
7- (BOOL)bridge:(RCTBridge *)bridge didNotFindModule:(NSString *)moduleName {
8  return YES;
9}
10#endif
11
12`;
13
14module.exports = (config) => {
15  return withDangerousMod(config, [
16    'ios',
17    async (config) => {
18      const fileInfo = IOSConfig.Paths.getAppDelegate(config.modRequest.projectRoot);
19      let contents = await fs.readFile(fileInfo.path, 'utf-8');
20      if (fileInfo.language === 'objc') {
21        if (!contents.match(/didNotFindModule:\(NSString\s?\*\)moduleName/)) {
22          const sections = contents.split('@end');
23          sections[sections.length - 2] += customBlockObjc;
24          contents = sections.join('@end');
25        }
26      } else {
27        throw new Error(
28          `Cannot append didNotFindModule method to AppDelegate of language "${fileInfo.language}"`
29        );
30      }
31      await fs.writeFile(fileInfo.path, contents);
32
33      return config;
34    },
35  ]);
36};
37