This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
/
permissions.ts
405 lines (357 loc) · 13 KB
/
permissions.ts
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { ExpoConfig, readConfigJsonAsync, writeConfigJsonAsync } from '@expo/config';
import { IosPlist, IosWorkspace } from '@expo/xdl';
import StandaloneContext from '@expo/xdl/build/detach/StandaloneContext';
import * as Manifest from '@expo/android-manifest';
import chalk from 'chalk';
import { Command } from 'commander';
// @ts-ignore: MultiSelect is not typed
import { Form, MultiSelect } from 'enquirer';
import fs from 'fs-extra';
import path from 'path';
const DefaultiOSPermissions: { [permission: string]: string } = {
NSCalendarsUsageDescription: `Allow $(PRODUCT_NAME) to access your calendar`,
NSCameraUsageDescription: `Allow $(PRODUCT_NAME) to use the camera`,
NSContactsUsageDescription: `Allow $(PRODUCT_NAME) experiences to access your contacts`,
NSLocationAlwaysUsageDescription: `Allow $(PRODUCT_NAME) to use your location`,
NSLocationAlwaysAndWhenInUseUsageDescription: `Allow $(PRODUCT_NAME) to use your location`,
NSLocationWhenInUseUsageDescription: `Allow $(PRODUCT_NAME) experiences to use your location`,
NSMicrophoneUsageDescription: `Allow $(PRODUCT_NAME) to access your microphone`,
NSMotionUsageDescription: `Allow $(PRODUCT_NAME) to access your device's accelerometer`,
NSPhotoLibraryAddUsageDescription: `Give $(PRODUCT_NAME) experiences permission to save photos`,
NSPhotoLibraryUsageDescription: `Give $(PRODUCT_NAME) experiences permission to access your photos`,
NSRemindersUsageDescription: `Allow $(PRODUCT_NAME) to access your reminders`,
};
const DefaultiOSPermissionNames: { [key: string]: string } = {
NSCalendarsUsageDescription: `Calendars`,
NSCameraUsageDescription: `Camera`,
NSContactsUsageDescription: `Contacts`,
NSLocationAlwaysUsageDescription: `Always location`,
NSLocationAlwaysAndWhenInUseUsageDescription: `Always location and when in use`,
NSLocationWhenInUseUsageDescription: `Location when in use`,
NSMicrophoneUsageDescription: `Microphone`,
NSMotionUsageDescription: `Motion`,
NSPhotoLibraryAddUsageDescription: `Saving Photos`,
NSPhotoLibraryUsageDescription: `Camera Roll`,
NSRemindersUsageDescription: `Reminders`,
};
const CHEVRON = `\u203A`;
type AnyPermissions = { [permission: string]: string };
async function writePermissionsToIOSAsync(
projectDir: string,
{ ios = {} }: ExpoConfig,
permissions: { [permission: string]: string | undefined }
): Promise<void> {
console.log(
chalk.magenta(
`${CHEVRON} Saving selection to the ${chalk.underline`expo.ios.infoPlist`} object in your universal ${chalk.bold`app.json`}...`
)
);
await writeConfigJsonAsync(projectDir, {
ios: {
...ios,
infoPlist: {
...(ios.infoPlist || {}),
...permissions,
},
},
});
}
function getPermissionDescription(
appName: string,
{ ios = {} }: ExpoConfig,
permission: string
): string {
const { infoPlist = {} } = ios;
if (typeof infoPlist[permission] === 'string') {
return infoPlist[permission];
}
return getDefaultExpoPermissionDescription(appName, permission);
}
function getDefaultExpoPermissionDescription(appName: string, permission: string): string {
// TODO(Bacon): Maybe use name instead of $(PRODUCT_NAME)
const permissionDescription = DefaultiOSPermissions[permission];
if (!permissionDescription) {
throw new Error(`No permission for ${permission}`);
}
return permissionDescription;
}
async function getActiveAndroidPermissionsAsync(
manifestPath: string | null,
exp: ExpoConfig
): Promise<string[]> {
console.log('');
let permissions: string[];
// The Android Manifest takes priority over the app.json
if (manifestPath) {
console.log(
chalk.magenta(
`${CHEVRON} Getting permissions from the native ${chalk.bold`AndroidManifest.xml`}`
)
);
const manifest = await Manifest.readAsync(manifestPath);
permissions = Manifest.getPermissions(manifest);
// Remove the required permissions
permissions = permissions.filter(v => v !== 'android.permission.INTERNET');
} else {
console.log(
chalk.magenta(
`${CHEVRON} Getting permissions from the ${chalk.underline`expo.android.permissions`} array in the universal ${chalk.bold`app.json`}...`
)
);
permissions = (exp.android || {}).permissions;
// If no array is defined then that means all permissions will be used
if (!Array.isArray(permissions)) {
console.log(
chalk.magenta(
`${CHEVRON} ${chalk.underline`expo.android.permissions`} does not exist in the ${chalk.bold`app.json`}, the default value is ${chalk.bold`all permissions`}`
)
);
permissions = Object.keys(Manifest.UnimodulePermissions);
}
}
// Ensure the names are formatted correctly
permissions = permissions.map(permission => Manifest.ensurePermissionNameFormat(permission));
return permissions;
}
export async function actionAndroid(projectDir: string = './'): Promise<void> {
const { exp } = await readConfigJsonAsync(projectDir, { skipSDKVersionRequirement: true });
const manifestPath = await Manifest.getProjectAndroidManifestPathAsync(projectDir);
const permissions = await getActiveAndroidPermissionsAsync(manifestPath, exp);
const isExpo = !manifestPath;
const choices: { name: string; message: string }[] = [];
const allPermissions = [
...new Set([...permissions, ...Object.keys(Manifest.UnimodulePermissions)]),
];
for (const permission of allPermissions) {
choices.push({
name: permission,
message: permissions.includes(permission) ? chalk.green(permission) : chalk.gray(permission),
});
}
const prompt = new MultiSelect({
header() {
const descriptions: AnyPermissions = {
// TODO(Bacon): Add descriptions of what each permission is used for
};
return descriptions[Object.keys(Manifest.UnimodulePermissions)[this.index]];
},
initial: permissions,
hint: '(Use <space> to select, <return> to submit, <a> to toggle, <i> to invert the selection)',
message: `Select Android permissions`,
choices,
});
console.log('');
let answer: string[];
try {
answer = await prompt.run();
} catch (error) {
console.log(chalk.yellow(`${CHEVRON} Exiting...`));
return;
}
const selectedAll = choices.length === answer.length;
console.log(
chalk.magenta(
`${CHEVRON} Saving selection to the ${chalk.underline`expo.android.permissions`} array in the ${chalk.bold`app.json`}...`
)
);
if (isExpo) {
if (selectedAll) {
console.log(
chalk.magenta(
`${CHEVRON} Expo will default to using all permissions in your project by deleting the ${chalk.underline`expo.android.permissions`} array.`
)
);
}
}
await writeConfigJsonAsync(projectDir, {
android: {
...(exp.android || {}),
// An empty array means no permissions
// No value means all permissions
permissions: selectedAll
? undefined
: answer.map((permission: string) => {
if (permission.startsWith('android.permission.')) {
return permission.split('.').pop();
}
return permission;
}),
},
});
if (!isExpo) {
console.log(
chalk.magenta(`${CHEVRON} Saving selection to the native ${chalk.bold`AndroidManifest.xml`}`)
);
await Manifest.persistAndroidPermissionsAsync(projectDir, [
'android.permission.INTERNET',
...answer,
]);
}
}
type IOSPermissionChoice = { name: string; message: string; initial: string };
async function promptForPermissionDescriptionsAsync(
choices: IOSPermissionChoice[],
hasNativeConfig: boolean,
currentDescriptions: AnyPermissions
): Promise<AnyPermissions> {
console.log('');
const keys = Object.keys(DefaultiOSPermissionNames);
const prompt = new Form({
name: hasNativeConfig ? 'Native project' : 'Universal project',
message: 'Modify iOS Permissions',
header() {
const permission = keys[this.index];
if (!currentDescriptions[permission]) {
return chalk.magenta(
`${CHEVRON} Add a description to enable the "${chalk.bold(
permission
)}" permission in your iOS app`
);
}
return chalk.magenta(
`${CHEVRON} Update the description for the permission "${chalk.bold(permission)}"`
);
},
choices,
});
try {
const answer: AnyPermissions = await prompt.run();
// Trimming allows users to pass in " " to delete a permission
for (const key of Object.keys(answer)) {
answer[key] = answer[key].trim();
}
return answer;
} catch (error) {
console.log(chalk.yellow(`${CHEVRON} Exiting...`));
process.exit(0);
}
return {};
}
export async function actionIOS(projectDir: string = './'): Promise<void> {
const { exp } = await readConfigJsonAsync(projectDir, { skipSDKVersionRequirement: true });
const appName = exp.name!;
const context = await StandaloneContext.createUserContext(projectDir, exp, '');
const supportingDirectory = getInfoPlistDirectory(context);
let infoPlist: AnyPermissions | undefined;
console.log('');
let currentDescriptions: AnyPermissions = {};
let defaultExpoDescriptions: AnyPermissions = {};
if (supportingDirectory) {
console.log(chalk.magenta(`${CHEVRON} Using native ios ${chalk.bold`Info.plist`}`));
infoPlist = (await IosPlist.modifyAsync(
supportingDirectory,
'Info',
infoPlist => infoPlist
)) as AnyPermissions;
for (const key of Object.keys(DefaultiOSPermissionNames)) {
if (key in infoPlist && infoPlist[key]) {
currentDescriptions[key] = infoPlist[key];
} else {
currentDescriptions[key] = '';
}
}
} else {
if ((exp.ios || {}).infoPlist) {
console.log(
chalk.magenta(
`${CHEVRON} Getting permissions from the ${chalk.underline`expo.ios.infoPlist`} object in the universal ${chalk.bold`app.json`}...`
)
);
} else {
console.log(
chalk.magenta(
`${CHEVRON} Showing the default permissions used by ${chalk.bold`Turtle`} to build your project...`
)
);
}
defaultExpoDescriptions = Object.keys(DefaultiOSPermissions).reduce(
(previous, current) => ({
...previous,
[current]: getDefaultExpoPermissionDescription(appName, current),
}),
{}
);
currentDescriptions = Object.keys(DefaultiOSPermissions).reduce(
(previous, current) => ({
...previous,
[current]: getPermissionDescription(appName, exp, current),
}),
{}
);
}
const choices: IOSPermissionChoice[] = [];
for (const key of Object.keys(currentDescriptions)) {
choices.push({
name: key,
message: DefaultiOSPermissionNames[key],
initial: currentDescriptions[key],
});
}
const answer = await promptForPermissionDescriptionsAsync(
choices,
!!supportingDirectory,
currentDescriptions
);
const modifiedAnswers: { [key: string]: string | undefined } = Object.keys(answer).reduce(
(previous, current) => {
const permissionDescription = answer[current];
if (permissionDescription !== defaultExpoDescriptions[current] && permissionDescription) {
return {
...previous,
[current]: permissionDescription,
};
}
return {
...previous,
[current]: undefined,
};
},
{}
);
await writePermissionsToIOSAsync(projectDir, exp, modifiedAnswers);
if (supportingDirectory && infoPlist) {
console.log(
chalk.magenta(`${CHEVRON} Saving selection to the native ${chalk.bold`Info.plist`}...`)
);
await IosPlist.modifyAsync(supportingDirectory, 'Info', infoPlist => {
for (const key of Object.keys(answer)) {
if (answer[key]) {
infoPlist[key] = answer[key];
} else {
delete infoPlist[key];
}
}
return infoPlist;
});
} else {
console.log(
`${CHEVRON} ${chalk.bold`Remember:`} ${chalk.reset
.dim`Permission messages are a build-time configuration. Your selection will only be available in Standalone, or native builds. You will continue to see the predefined permission dialogs when using your app in the Expo client.`}`
);
}
}
// Find the location of the native iOS info.plist if it exists
// TODO(Bacon): Do a deep search for the Info.plist in case the app name was changed
function getInfoPlistDirectory(context: any): string | null {
const { supportingDirectory } = IosWorkspace.getPaths(context);
if (fs.existsSync(path.resolve(supportingDirectory, 'Info.plist'))) {
return supportingDirectory;
} else if (fs.existsSync(path.resolve(supportingDirectory, '..', 'Info.plist'))) {
return path.resolve(supportingDirectory, '..');
}
return null;
}
function command(program: Command) {
program
.command('permissions:ios [project-dir]')
.description('Manage permissions in your native iOS project.')
.allowOffline()
.asyncAction(actionIOS);
program
.command('permissions:android [project-dir]')
.description('Manage permissions in your native Android project.')
.allowOffline()
.asyncAction(actionAndroid);
}
// @ts-ignore
export default command;