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
83 lines (64 loc) · 2.37 KB
/
react-native.config.js
File metadata and controls
83 lines (64 loc) · 2.37 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
const path = require('path');
const fs = require('fs');
const CONFIG_KEY = 'reactNativePermissionsIOS';
const pkgDir = (dir) => {
const pkgPath = path.join(dir, 'package.json');
if (fs.existsSync(pkgPath)) {
return dir;
}
const parentDir = path.resolve(dir, '..');
if (parentDir !== dir) {
return pkgDir(parentDir);
}
};
const logError = (message) => {
console.log(`\x1b[31m${message}\x1b[0m`);
};
const logWarning = (message) => {
console.log(`\x1b[33m${message}\x1b[0m`);
};
module.exports = {
commands: [
{
name: 'setup-ios-permissions',
description:
'Update react-native-permissions podspec to link additional permission handlers.',
func: () => {
const rootDir = pkgDir(process.cwd()) || process.cwd();
const pkgPath = path.join(rootDir, 'package.json');
const jsonPath = path.join(rootDir, `${CONFIG_KEY}.json`);
let config = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))[CONFIG_KEY];
if (!config && fs.existsSync(jsonPath)) {
const text = fs.readFileSync(jsonPath, 'utf-8');
config = JSON.parse(text);
}
if (!config) {
logError(`No ${CONFIG_KEY} config found`);
process.exit(1);
}
if (!Array.isArray(config)) {
logError(`Invalid ${CONFIG_KEY} config`);
process.exit(1);
}
const iosDir = path.join(__dirname, 'ios');
const iosDirents = fs.readdirSync(iosDir, {withFileTypes: true});
const directories = iosDirents
.filter((dirent) => dirent.isDirectory() || dirent.name.endsWith('.xcodeproj'))
.map((dirent) => dirent.name)
.filter((name) => config.includes(name));
const sourceFiles = [
'"ios/*.{h,m,mm}"',
...directories.map((name) => `"ios/${name}/*.{h,m,mm}"`),
];
const unknownPermissions = config.filter((name) => !directories.includes(name));
if (unknownPermissions.length > 0) {
logWarning(`Unknown permissions: ${unknownPermissions.join(', ')}`);
}
const podspecPath = path.join(__dirname, 'RNPermissions.podspec');
const podspec = fs.readFileSync(podspecPath, 'utf-8');
const podspecContent = podspec.replace(/"ios\/\*\.{h,m,mm}".*/, sourceFiles.join(', '));
fs.writeFileSync(podspecPath, podspecContent, 'utf-8');
},
},
],
};