-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.plugin.js
279 lines (242 loc) · 7.83 KB
/
app.plugin.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const {
withProjectBuildGradle,
withGradleProperties,
withPlugins,
withDangerousMod,
withInfoPlist,
} = require("@expo/config-plugins");
const path = require("path");
const fs = require("fs");
/**
* Plugin to add mapbox api key and access token to mapbox_access_token.xml
* @param {*} apiKey api key to set
* @param {*} token access token to set
* @param {*} config expo project config
* @returns modified config
*/
const withAPIKeys = (token) => (config) => {
return withDangerousMod(config, [
"android",
async (config) => {
const filePath = path.resolve(
config.modRequest.projectRoot,
config.modRequest.platformProjectRoot,
`app/src/main/res/values`
);
// new file
const fileName = "mapbox_access_token.xml";
const content = `<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="mapbox_access_token" translatable="false">${token}</string>
<string name="mapbox_api_key" translatable="false">${token}</string>
</resources>`;
const newFilePath = path.resolve(filePath, fileName);
// create a new file
if (!fs.existsSync(newFilePath)) {
fs.writeFileSync(newFilePath, content);
}
return config;
},
]);
};
/**
* Plugin to add mapbox private token to gradle properties
* @param {*} config expo project config
* @param {*} token private token to set
* @returns modified config
*/
const withGradleProps = (token) => (config) => {
return withGradleProperties(config, (modConf) => {
modConf.modResults.push({
type: "property",
key: "MAPBOX_DOWNLOADS_TOKEN",
value: token,
});
return modConf;
});
};
/**
* Plugin to add mapsindoors maven repository
* @param {*} config expo project config
* @returns modified config
*/
const withMavenRepo = (config) => {
return withProjectBuildGradle(config, (modConf) => {
const content = modConf.modResults.contents.replace(
"maven { url 'https://www.jitpack.io' }",
`maven { url 'https://www.jitpack.io' }
maven { url 'https://maven.mapsindoors.com/' }
maven {
url 'https://api.mapbox.com/downloads/v2/releases/maven'
authentication {
basic(BasicAuthentication)
}
credentials {
// Do not change the username below.
// This should always be 'mapbox' (not your username).
username = "mapbox"
// Use the secret token you stored in gradle.properties as the password
password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
}
}`
);
modConf.modResults.contents = content;
return modConf;
});
};
/**
* Plugin to add mapsindoors Plist infos
* @param {*} config expo project config
* @param {*} token access token to set
* @returns modified config
*/
const withTokenInfoPlist = (token) => (config) => {
return withInfoPlist(config, (modConf) => {
modConf.modResults["MBXAccessToken"] = token;
return modConf;
});
};
/**
* Plugin to add mapsindoors user system setup
* @param {*} token private token to set
* @param {*} config expo project config
*/
const withiOSSecret = (token) => (config) => {
return withDangerousMod(config, [
"ios",
async (modConf) => {
const home = path.resolve(process.env.HOME);
const netrc = path.resolve(home, ".netrc");
// conf to add
const conf = `machine api.mapbox.com
login mapbox
password ${token}
`;
const exists = fs.existsSync(netrc);
if (!exists) {
// no previous conf, write new one
fs.writeFileSync(netrc, conf, { flag: "w" });
fs.chmodSync(netrc, 0o600);
} else {
// get content
let content = fs.readFileSync(netrc, "utf-8");
const lines = content.split("\n");
// check if mapbox conf exist
const found = lines.findIndex((line) =>
line.includes("machine api.mapbox.com")
);
if (found >= 0) {
// remove previous mapbox conf
content = lines
.filter((_, idx) => ![found, found + 1, found + 2].includes(idx))
.join("\n");
fs.writeFileSync(netrc, content, { flag: "w" });
}
// write new conf
fs.writeFileSync(netrc, conf, { flag: "a+" });
}
return modConf;
},
]);
};
/**
* Plugin to add podfile script
* @param {*} config expo project config
* @returns modified config
*/
const withPodfile = (config) => {
return withDangerousMod(config, [
"ios",
(modConf) => {
const podfile = path.join(
modConf.modRequest.platformProjectRoot,
"Podfile"
);
let contents = fs.readFileSync(podfile, "utf-8");
contents = contents.replace(
`post_install do |installer|`,
`post_install do |installer|\n
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
end
end\n`
);
fs.writeFileSync(podfile, contents, "utf8");
return modConf;
},
]);
};
/**
* Plugin to add podfile linkage script script
* @param {*} staticPods true if project is build as static, false/undefined if build as dynamic
* @param {*} config expo project config
* @returns modified config
*/
const withStaticLinkage = (staticPods) => (config) => {
if (!staticPods) {
return config;
}
return withDangerousMod(config, [
"ios",
(modConf) => {
const podfile = path.join(
modConf.modRequest.platformProjectRoot,
"Podfile"
);
let contents = fs.readFileSync(podfile, "utf-8");
if (!contents.includes("pre_install do |installer|")) {
// add preinstall script if not present
contents = contents.replace(
`post_install do |installer|`,
`pre_install do |installer|
end
post_install do |installer|`
);
}
// complete script with dynamic linkage for this lib
contents = contents.replace(
`pre_install do |installer|`,
`dynamic_frameworks = ["MapboxMaps", "MapboxCoreMaps", "MapboxCommon", "MapboxMobileEvents", "MapboxDirections", "Polyline", "Turf"]
# override default static linkage
pre_install do |installer|
installer.pod_targets.each do |pod|
if dynamic_frameworks.include?(pod.name)
puts "Overriding the static_framework? method for #{pod.name}"
def pod.static_framework?;
false
end
def pod.build_type;
Pod::BuildType.dynamic_framework
end
end
end`
);
fs.writeFileSync(podfile, contents, "utf8");
return modConf;
},
]);
};
/**
* Combine Expo configuration and properties
* @param {*} config expo project config
* @param {*} {downloadToken} Mapbox download token
* @param {*} {publicToken} Mapbox public token
* @returns modified config
*/
const withMapsPeopleMapbox = (
config,
{ downloadToken, publicToken, staticPods }
) => {
const plugins = [
withGradleProps(downloadToken),
withAPIKeys(publicToken),
withMavenRepo,
withTokenInfoPlist(publicToken),
withiOSSecret(downloadToken),
withPodfile,
withStaticLinkage(staticPods),
];
return withPlugins(config, plugins);
};
module.exports = withMapsPeopleMapbox;