forked from zoontek/react-native-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-native.config.js
More file actions
74 lines (60 loc) · 2.46 KB
/
react-native.config.js
File metadata and controls
74 lines (60 loc) · 2.46 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
const {existsSync} = require('fs');
const fs = require('fs/promises');
const path = require('path');
const pc = require('picocolors');
const pkgDir = require('pkg-dir');
const CONFIG_KEY = 'reactNativePermissionsIOS';
const log = {
error: (text) => console.log(pc.red(text)),
warning: (text) => console.log(pc.yellow(text)),
};
module.exports = {
commands: [
{
name: 'setup-ios-permissions',
description:
'Update react-native-permissions podspec to link additional permission handlers.',
func: async () => {
const rootDir = pkgDir.sync() || process.cwd();
const pkgPath = path.join(rootDir, 'package.json');
const pkg = await fs.readFile(pkgPath, 'utf-8');
const jsonPath = path.join(rootDir, `${CONFIG_KEY}.json`);
let config = JSON.parse(pkg)[CONFIG_KEY];
if (!config && existsSync(jsonPath)) {
const text = await fs.readFile(jsonPath, 'utf-8');
config = JSON.parse(text);
}
if (!config) {
log.error(
`No config detected. In order to setup iOS permissions, you first need to add an "${CONFIG_KEY}" array in your package.json.`,
);
process.exit(1);
}
if (!Array.isArray(config) || config.length === 0) {
log.error(`Invalid "${CONFIG_KEY}" config detected. It must be a non-empty array.`);
process.exit(1);
}
const iosDirPath = path.join(__dirname, 'ios');
const podspecPath = path.join(__dirname, 'RNPermissions.podspec');
const iosDir = await fs.readdir(iosDirPath, {withFileTypes: true});
const podspec = await fs.readFile(podspecPath, 'utf-8');
const directories = iosDir
.filter((dirent) => dirent.isDirectory() || dirent.name.endsWith('.xcodeproj'))
.map((dirent) => dirent.name)
.filter((name) => config.includes(name));
const unknownPermissions = config
.filter((name) => !directories.includes(name))
.map((name) => `"${name}"`);
if (unknownPermissions.length > 0) {
log.warning(`Unknown iOS permissions: ${unknownPermissions.join(', ')}`);
}
const sourceFiles = [
'"ios/*.{h,m,mm}"',
...directories.map((name) => `"ios/${name}/*.{h,m,mm}"`),
];
const podspecContent = podspec.replace(/"ios\/\*\.{h,m,mm}".*/, sourceFiles.join(', '));
return fs.writeFile(podspecPath, podspecContent, 'utf-8');
},
},
],
};