-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathindex.ts
More file actions
172 lines (148 loc) · 4.97 KB
/
index.ts
File metadata and controls
172 lines (148 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
type ConfigPlugin,
AndroidConfig,
createRunOncePlugin,
withAndroidManifest,
withAppDelegate,
withInfoPlist,
withMainApplication,
} from '@expo/config-plugins';
import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';
import {
addImports,
appendContentsInsideDeclarationBlock,
} from '@expo/config-plugins/build/android/codeMod';
import {
addObjcImports,
insertContentsInsideObjcFunctionBlock,
insertContentsInsideSwiftFunctionBlock,
} from '@expo/config-plugins/build/ios/codeMod';
import type { IntercomPluginProps, IntercomRegion } from './@types';
import { withIntercomPushNotification } from './withPushNotifications';
const mainApplication: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
withMainApplication(_config, (config) => {
let stringContents = config.modResults.contents;
stringContents = addImports(
stringContents,
['com.intercom.reactnative.IntercomModule'],
config.modResults.language === 'java'
);
// Remove previous code
stringContents = stringContents.replace(
/IntercomModule\.initialize\(.*?\)\s*;?\n?/g,
''
);
stringContents = appendContentsInsideDeclarationBlock(
stringContents,
'onCreate',
`IntercomModule.initialize(this, "${props.androidApiKey}", "${
props.appId
}")${config.modResults.language === 'java' ? ';' : ''}\n`
);
config.modResults.contents = stringContents;
return config;
});
const androidManifest: ConfigPlugin<IntercomPluginProps> = (
_config,
{ intercomRegion = 'US' }
) => {
let newConfig = AndroidConfig.Permissions.withPermissions(
_config,
[
'android.permission.READ_EXTERNAL_STORAGE',
'android.permission.VIBRATE',
].filter(Boolean)
);
newConfig = withAndroidManifest(newConfig, (config) => {
const currentMainApplication =
AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults);
const androidRegionMapper: Record<IntercomRegion, string> = {
US: '@integer/intercom_server_region_us',
EU: '@integer/intercom_server_region_eu',
AU: '@integer/intercom_server_region_aus',
};
AndroidConfig.Manifest.addMetaDataItemToMainApplication(
currentMainApplication,
'io.intercom.android.sdk.server.region',
androidRegionMapper[intercomRegion]
);
return config;
});
return newConfig;
};
const withIntercomAndroid: ConfigPlugin<IntercomPluginProps> = (
config,
props
) => {
let newConfig = config;
newConfig = mainApplication(newConfig, props);
newConfig = androidManifest(newConfig, props);
return newConfig;
};
const appDelegate: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
withAppDelegate(_config, (config) => {
let stringContents = config.modResults.contents;
const isSwift = config.modResults.language === 'swift';
stringContents = isSwift
? mergeContents({
src: stringContents,
newSrc: 'import IntercomModule',
comment: '//',
tag: 'Intercom header',
anchor: /import Expo/,
offset: 1,
}).contents
: addObjcImports(stringContents, ['<IntercomModule.h>']);
// Remove previous code
stringContents = stringContents
.replace(
/\s*\[IntercomModule initialize:@"(.*)" withAppId:@"(.*)"];/g,
''
)
.replace(/\s*IntercomModule\.initialize\((.*), withAppId: (.*)\)/g, '');
stringContents = isSwift
? insertContentsInsideSwiftFunctionBlock(
stringContents,
'application(_:didFinishLaunchingWithOptions:)',
`IntercomModule.initialize("${props.iosApiKey}", withAppId: "${props.appId}")`,
{ position: 'tailBeforeLastReturn' }
)
: insertContentsInsideObjcFunctionBlock(
stringContents,
'application didFinishLaunchingWithOptions:',
`[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`,
{ position: 'tailBeforeLastReturn' }
);
config.modResults.contents = stringContents;
return config;
});
const infoPlist: ConfigPlugin<IntercomPluginProps> = (
_config,
{ intercomRegion }
) => {
const newConfig = withInfoPlist(_config, (config) => {
if (intercomRegion) {
config.modResults.IntercomRegion = intercomRegion;
}
return config;
});
return newConfig;
};
const withIntercomIOS: ConfigPlugin<IntercomPluginProps> = (config, props) => {
let newConfig = appDelegate(config, props);
newConfig = infoPlist(newConfig, props);
return newConfig;
};
const withIntercomReactNative: ConfigPlugin<IntercomPluginProps> = (
config,
props
) => {
let newConfig = config;
newConfig = withIntercomAndroid(newConfig, props);
newConfig = withIntercomIOS(newConfig, props);
newConfig = withIntercomPushNotification(newConfig, props);
return newConfig;
};
const configPlugin = (pkg: { name: string; version: string }) =>
createRunOncePlugin(withIntercomReactNative, pkg.name, pkg.version);
export default configPlugin;