Skip to content

Commit 154ebfb

Browse files
TitozzzjblarriviereMateusAndrade
authored
fix(expo): use expo-build-properties to create the config plugin (#1618)
* fix: use expo-build-properties to build the plugin Instead of manually recreating everything, I've just used expo-build-properties. This fixes that plugin so that it does not override other manifest queries from other plugins but gracefully merges everything. * fix: yarn lock dedupe * chore: remove potential duplicates from android permissions --------- Co-authored-by: Jean-Baptiste LARRIVIERE <[email protected]> Co-authored-by: Mateus Andrade <[email protected]>
1 parent ea6cbce commit 154ebfb

File tree

4 files changed

+123
-170
lines changed

4 files changed

+123
-170
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Configure you `app.config.ts` or `app.json` to use the permissions needed by the
3333
"com.instagram.android",
3434
"com.twitter.android",
3535
"com.zhiliaoapp.musically",
36-
]
36+
],
37+
"enableBase64ShareAndroid": true
3738
}
3839
]
3940
]
@@ -63,6 +64,12 @@ Configure you `app.config.ts` or `app.json` to use the permissions needed by the
6364
</queries>
6465
```
6566

67+
`enableBase64ShareAndroid` will take care of adding the permission to the AndroidManifest.xml.
68+
69+
```xml
70+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
71+
```
72+
6673
And prebuild the project with `expo prebuild`.
6774

6875
## Bare React Native

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"babel-jest": "^29.7.0",
4949
"eslint": "^8.53.0",
5050
"eslint-config-satya164": "^3.2.0",
51+
"expo-build-properties": "^0.13.1",
5152
"husky": "^4.3.0",
5253
"jest": "^29.7.0",
5354
"lint-staged": "^15.0.2",

plugin/src/index.ts

Lines changed: 43 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,53 @@
1-
import {
2-
withAndroidManifest,
3-
createRunOncePlugin,
4-
ExportedConfigWithProps,
5-
ExportedConfig,
6-
} from '@expo/config-plugins';
7-
8-
// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-var-requires
9-
const pkg = require('../../package.json');
1+
import { ExportedConfig } from '@expo/config-plugins';
2+
import { withBuildProperties } from 'expo-build-properties';
103

114
/**
12-
* @type {import('./types').ManifestQueries}
13-
* what we are trying to add:
14-
* <queries>
15-
<package android:name="com.facebook.katana"/>
16-
<package android:name="com.instagram.android"/>
17-
<package android:name="com.twitter.android"/>
18-
<package android:name="com.zhiliaoapp.musically"/>
19-
<intent></intent>
20-
<action android:name="android.intent.action.VIEW"/>
21-
<category android:name="android.intent.category.BROWSABLE"/>
22-
<data android:scheme="https"/>
23-
</intent>
24-
</queries>
5+
* Handles for edge case when LSApplicationQueriesSchemes is an object or undefined.
256
*/
26-
27-
/**
28-
* @param {import('@expo/config-plugins').ExportedConfig} config
29-
*/
30-
const withAndroidManifestService = (config: ExportedConfig, props: WithSocialShareProps) => {
31-
return withAndroidManifest(config, (config: ExportedConfigWithProps) => {
32-
config.modResults.manifest = {
33-
...config.modResults.manifest,
34-
queries: {
35-
package: props?.android?.map((social) => ({
36-
$: {
37-
'android:name': social,
38-
},
39-
})),
40-
intent: [
41-
{
42-
action: {
43-
$: {
44-
'android:name': 'android.intent.action.VIEW',
45-
},
46-
},
47-
category: {
48-
$: {
49-
'android:name': 'android.intent.category.BROWSABLE',
50-
},
51-
},
52-
data: {
53-
$: {
54-
'android:scheme': 'https',
55-
},
56-
},
57-
},
58-
],
59-
},
60-
};
61-
62-
return config;
63-
});
64-
};
65-
66-
/**
67-
* Handles for edge case when LSApplicationQueriesSchemes is an object or undefined.
68-
*/
69-
const getIOSQuerySchemes = (config: ExportedConfig): Array<string> => {
7+
const getIOSQuerySchemes = (config: ExportedConfig): string[] => {
708
return Array.isArray(config.ios?.infoPlist?.LSApplicationQueriesSchemes)
719
? config.ios?.infoPlist?.LSApplicationQueriesSchemes ?? []
7210
: [];
73-
}
11+
};
7412

75-
const withInfoPlist = (config: ExportedConfig, props: WithSocialShareProps) => {
76-
return {
77-
...config,
78-
ios: {
79-
...config.ios,
80-
infoPlist: {
81-
...config.ios?.infoPlist,
82-
LSApplicationQueriesSchemes: [...getIOSQuerySchemes(config), ...props?.ios ?? []]
13+
export default (
14+
config: ExportedConfig,
15+
props: {
16+
enableBase64ShareAndroid?: boolean;
17+
android?: string[];
18+
ios?: string[];
19+
},
20+
) => {
21+
return withBuildProperties(
22+
{
23+
...config,
24+
android: {
25+
...config.android,
26+
...(props.enableBase64ShareAndroid
27+
? {
28+
permissions: [
29+
...new Set([
30+
...(config.android?.permissions ?? []),
31+
'android.permission.WRITE_EXTERNAL_STORAGE',
32+
]),
33+
],
34+
}
35+
: {}),
36+
},
37+
ios: {
38+
...config.ios,
39+
infoPlist: {
40+
...config.ios?.infoPlist,
41+
LSApplicationQueriesSchemes: [...getIOSQuerySchemes(config), ...(props?.ios ?? [])],
42+
},
8343
},
8444
},
85-
};
86-
};
87-
88-
type WithSocialShareProps = {
89-
ios: string[];
90-
android: string[];
45+
{
46+
android: {
47+
manifestQueries: {
48+
package: props.android ?? [],
49+
},
50+
},
51+
},
52+
);
9153
};
92-
93-
function withSocialShare(config: ExportedConfig, props: WithSocialShareProps) {
94-
config = withAndroidManifestService(config, props); // Android
95-
config = withInfoPlist(config, props); // iOS
96-
return config;
97-
}
98-
99-
// eslint-disable-next-line import/no-commonjs
100-
module.exports = createRunOncePlugin(withSocialShare, pkg.name, pkg.version);

0 commit comments

Comments
 (0)