-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy pathindex.ts
179 lines (154 loc) · 4.91 KB
/
index.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
/**
* 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 path from 'path';
import fs from 'fs';
import findAndroidDir from './findAndroidDir';
import findManifest from './findManifest';
import findPackageClassName from './findPackageClassName';
import {
AndroidProjectParams,
AndroidProjectConfig,
AndroidDependencyParams,
AndroidDependencyConfig,
} from '@react-native-community/cli-types';
import {
getPackageName,
parseApplicationIdFromBuildGradleFile,
} from './getAndroidProject';
import {findLibraryName} from './findLibraryName';
import {findComponentDescriptors} from './findComponentDescriptors';
import {findBuildGradle} from './findBuildGradle';
import {CLIError} from '@react-native-community/cli-tools';
import getMainActivity from './getMainActivity';
/**
* Gets android project config by analyzing given folder and taking some
* defaults specified by user into consideration
*/
export function projectConfig(
root: string,
userConfig: AndroidProjectParams = {},
): AndroidProjectConfig | null {
const src = userConfig.sourceDir || findAndroidDir(root);
if (!src) {
return null;
}
const sourceDir = path.join(root, src);
const appName = getAppName(sourceDir, userConfig.appName);
const manifestPath = userConfig.manifestPath
? path.join(sourceDir, userConfig.manifestPath)
: findManifest(path.join(sourceDir, appName));
const buildGradlePath = findBuildGradle(sourceDir, false);
if (!manifestPath && !buildGradlePath) {
return null;
}
const packageName =
userConfig.packageName || getPackageName(manifestPath, buildGradlePath);
if (!packageName) {
throw new CLIError(
`Package name not found in neither ${manifestPath} nor ${buildGradlePath}`,
);
}
const applicationId = buildGradlePath
? getApplicationId(buildGradlePath, packageName)
: packageName;
const mainActivity = getMainActivity(manifestPath || '');
if (!mainActivity) {
throw new CLIError(`Main activity not found in ${manifestPath}`);
}
return {
sourceDir,
appName,
packageName,
applicationId,
mainActivity,
dependencyConfiguration: userConfig.dependencyConfiguration,
watchModeCommandParams: userConfig.watchModeCommandParams,
unstable_reactLegacyComponentNames:
userConfig.unstable_reactLegacyComponentNames,
};
}
function getApplicationId(buildGradlePath: string, packageName: string) {
let appId = packageName;
const applicationId = parseApplicationIdFromBuildGradleFile(buildGradlePath);
if (applicationId) {
appId = applicationId;
}
return appId;
}
function getAppName(sourceDir: string, userConfigAppName: string | undefined) {
let appName = '';
if (
typeof userConfigAppName === 'string' &&
fs.existsSync(path.join(sourceDir, userConfigAppName))
) {
appName = userConfigAppName;
} else if (fs.existsSync(path.join(sourceDir, 'app'))) {
appName = 'app';
}
return appName;
}
/**
* Same as projectConfigAndroid except it returns
* different config that applies to packages only
*/
export function dependencyConfig(
root: string,
userConfig: AndroidDependencyParams | null = {},
): AndroidDependencyConfig | null {
if (userConfig === null) {
return null;
}
const src = userConfig.sourceDir || findAndroidDir(root);
if (!src) {
return null;
}
const sourceDir = path.join(root, src);
const manifestPath = userConfig.manifestPath
? path.join(sourceDir, userConfig.manifestPath)
: findManifest(sourceDir);
const buildGradlePath = findBuildGradle(sourceDir, true);
if (!manifestPath && !buildGradlePath) {
return null;
}
const packageName =
userConfig.packageName || getPackageName(manifestPath, buildGradlePath);
const packageClassName = findPackageClassName(sourceDir);
/**
* This module has no package to export
*/
if (!packageClassName) {
return null;
}
const packageImportPath =
userConfig.packageImportPath ||
`import ${packageName}.${packageClassName};`;
const packageInstance =
userConfig.packageInstance || `new ${packageClassName}()`;
const buildTypes = userConfig.buildTypes || [];
const dependencyConfiguration = userConfig.dependencyConfiguration;
const libraryName =
userConfig.libraryName || findLibraryName(root, sourceDir);
const componentDescriptors =
userConfig.componentDescriptors || findComponentDescriptors(root);
let cmakeListsPath = userConfig.cmakeListsPath
? path.join(sourceDir, userConfig.cmakeListsPath)
: path.join(sourceDir, 'build/generated/source/codegen/jni/CMakeLists.txt');
if (process.platform === 'win32') {
cmakeListsPath = cmakeListsPath.replace(/\\/g, '/');
}
return {
sourceDir,
packageImportPath,
packageInstance,
buildTypes,
dependencyConfiguration,
libraryName,
componentDescriptors,
cmakeListsPath,
};
}