Skip to content

Commit 81a91e8

Browse files
committed
Use expo mergeContents
1 parent d36e034 commit 81a91e8

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

src/expo.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {ConfigPlugin, withDangerousMod} from '@expo/config-plugins';
2+
import {mergeContents} from '@expo/config-plugins/build/utils/generateCode';
23
import * as fs from 'fs/promises';
34
import * as path from 'path';
45

@@ -27,11 +28,6 @@ type Props = {
2728
)[];
2829
};
2930

30-
const requireRegExp =
31-
/require Pod::Executable\.execute_command\('node', \['-p',\s*'require\.resolve\(\s*"react-native\/scripts\/react_native_pods\.rb",\s*{paths: \[process\.argv\[1\]]},\s*\)', __dir__\]\)\.strip/;
32-
33-
const prepareRegExp = /prepare_react_native_project!/;
34-
3531
const withPermissions: ConfigPlugin<Props> = (config, {iosPermissions = []}) =>
3632
withDangerousMod(config, [
3733
'ios',
@@ -42,41 +38,36 @@ const withPermissions: ConfigPlugin<Props> = (config, {iosPermissions = []}) =>
4238
if (iosPermissions.length === 0) {
4339
return config;
4440
}
45-
if (!requireRegExp.test(contents) || !prepareRegExp.test(contents)) {
41+
42+
const withRequire = mergeContents({
43+
tag: 'require',
44+
src: contents,
45+
anchor:
46+
/^require File\.join\(File\.dirname\(`node --print "require\.resolve\('react-native\/package\.json'\)"`\), "scripts\/react_native_pods"\)$/m,
47+
newSrc: `require File.join(File.dirname(\`node --print "require.resolve('react-native-permissions/package.json')"\`), "scripts/setup")`,
48+
offset: 1,
49+
comment: '#',
50+
});
51+
52+
const withSetup = mergeContents({
53+
tag: 'setup',
54+
src: withRequire.contents,
55+
anchor: /^prepare_react_native_project!$/m,
56+
newSrc: `setup_permissions([
57+
${iosPermissions.map((permission) => ` '${permission}',`).join('\n')}
58+
])`,
59+
offset: 1,
60+
comment: '#',
61+
});
62+
63+
if (!withRequire.didMerge || !withSetup.didMerge) {
4664
console.error(
4765
"ERROR: Cannot add react-native-permissions to the project's ios/Podfile because it's malformed. Please report this with a copy of your project Podfile.",
4866
);
4967
return config;
5068
}
5169

52-
const nodeRequire = `
53-
def node_require(script)
54-
# Resolve script with node to allow for hoisting
55-
require Pod::Executable.execute_command('node', ['-p',
56-
"require.resolve(
57-
'#{script}',
58-
{paths: [process.argv[1]]},
59-
)", __dir__]).strip
60-
end
61-
62-
node_require('react-native/scripts/react_native_pods.rb')
63-
node_require('react-native-permissions/scripts/setup.rb')
64-
`.trim();
65-
66-
const setupFunction = `
67-
prepare_react_native_project!
68-
69-
setup_permissions([
70-
${iosPermissions.map((permission) => ` '${permission}',`).join('\n')}
71-
])
72-
`.trim();
73-
74-
await fs.writeFile(
75-
file,
76-
contents.replace(requireRegExp, nodeRequire).replace(prepareRegExp, setupFunction),
77-
'utf-8',
78-
);
79-
70+
await fs.writeFile(file, withSetup.contents, 'utf-8');
8071
return config;
8172
},
8273
]);

0 commit comments

Comments
 (0)