forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindPackageClassName.ts
More file actions
84 lines (70 loc) · 2.42 KB
/
findPackageClassName.ts
File metadata and controls
84 lines (70 loc) · 2.42 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import fs from 'fs';
import glob from 'fast-glob';
import path from 'path';
import {unixifyPaths} from '@react-native-community/cli-tools';
export function getMainActivityFiles(
folder: string,
includePackage: boolean = true,
) {
let patternArray = [];
if (includePackage) {
patternArray.push('Package.java', 'Package.kt');
} else {
patternArray.push('.java', '.kt');
}
return glob.sync(`**/*{${patternArray.join(',')}}`, {
cwd: unixifyPaths(folder),
onlyFiles: true,
ignore: ['**/.cxx/**'],
});
}
export default function getPackageClassName(folder: string) {
let files = getMainActivityFiles(folder);
let packages = getClassNameMatches(files, folder);
if (packages && packages.length > 0 && Array.isArray(packages[0])) {
return packages[0][1];
}
/*
When module contains `expo-module.config.json` we return null
because expo modules follow other practices and don't implement
ReactPackage/TurboReactPackage directly, so it doesn't make sense
to scan and read hundreds of files to get package class name.
Exception is `expo` package itself which contains `expo-module.config.json`
and implements `ReactPackage/TurboReactPackage`.
Following logic is done due to performance optimization.
*/
if (fs.existsSync(path.join(folder, '..', 'expo-module.config.json'))) {
return null;
}
files = getMainActivityFiles(folder, false);
packages = getClassNameMatches(files, folder);
// @ts-ignore
return packages.length ? packages[0][1] : null;
}
function getClassNameMatches(files: string[], folder: string) {
return files
.map((filePath) => fs.readFileSync(path.join(folder, filePath), 'utf8'))
.map(matchClassName)
.filter((match) => match);
}
export function matchClassName(file: string) {
const nativeModuleMatch = file.match(
/class\s+(\w+[^(\s]*)[\s\w():]*(\s+implements\s+|:)[\s\w():,]*[^{]*ReactPackage/,
);
// We first check for implementation of ReactPackage to find native
// modules and then for subclasses of TurboReactPackage to find turbo modules.
if (nativeModuleMatch) {
return nativeModuleMatch;
} else {
return file.match(
/class\s+(\w+[^(\s]*)[\s\w():]*(\s+extends\s+|:)[\s\w():,]*[^{]*(Turbo|Base)ReactPackage/,
);
}
}