-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathappUtils.ts
More file actions
320 lines (291 loc) · 9.81 KB
/
appUtils.ts
File metadata and controls
320 lines (291 loc) · 9.81 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
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
import * as fs from "fs-extra";
import * as path from "path";
import { glob } from "glob";
import { PackageJSON } from "./frameworks/compose/discover/runtime/node";
/**
* Supported application platforms.
*/
export enum Platform {
ANDROID = "ANDROID",
WEB = "WEB",
IOS = "IOS",
FLUTTER = "FLUTTER",
}
/**
* Supported web frameworks.
*/
export enum Framework {
REACT = "react",
ANGULAR = "angular",
}
interface AppIdentifier {
appId: string;
bundleId?: string;
}
/**
* Represents a detected application.
*/
export interface App {
platform: Platform;
directory: string;
appId?: string;
bundleId?: string;
frameworks?: Framework[];
}
/** Returns a string description of the app */
export function appDescription(a: App): string {
return `${a.directory} (${a.platform.toLowerCase()})`;
}
/**
* Given a directory, determine the platform type.
* @param dirPath The directory to scan.
* @return A list of platforms detected.
*/
export async function getPlatformsFromFolder(dirPath: string): Promise<Platform[]> {
const apps = await detectApps(dirPath);
return [...new Set(apps.map((app) => app.platform))];
}
/**
* Detects the apps in a given directory.
* @param dirPath The current working directory to scan.
* @return A list of apps detected.
*/
export async function detectApps(dirPath: string): Promise<App[]> {
const packageJsonFiles = await detectFiles(dirPath, "package.json");
const pubSpecYamlFiles = await detectFiles(dirPath, "pubspec.yaml");
const srcMainFolders = await detectFiles(dirPath, "src/main/");
const xCodeProjects = await detectFiles(dirPath, "*.xcodeproj/");
const webApps = await Promise.all(packageJsonFiles.map((p) => packageJsonToWebApp(dirPath, p)));
const flutterAppPromises = await Promise.all(
pubSpecYamlFiles.map((f) => processFlutterDir(dirPath, f)),
);
const flutterApps = flutterAppPromises.flat();
const androidAppPromises = await Promise.all(
srcMainFolders.map((f) => processAndroidDir(dirPath, f)),
);
const androidApps = androidAppPromises
.flat()
.filter((a) => !flutterApps.some((f) => isPathInside(f.directory, a.directory)));
const iosAppPromises = await Promise.all(xCodeProjects.map((f) => processIosDir(dirPath, f)));
const iosApps = iosAppPromises
.flat()
.filter((a) => !flutterApps.some((f) => isPathInside(f.directory, a.directory)));
return [...webApps, ...flutterApps, ...androidApps, ...iosApps];
}
async function processIosDir(dirPath: string, filePath: string): Promise<App[]> {
// Search for apps in the parent directory
const iosDir = path.dirname(filePath);
const iosAppIds = await detectAppIdsForPlatform(dirPath, Platform.IOS);
if (iosAppIds.length === 0) {
return [
{
platform: Platform.IOS,
directory: iosDir,
},
];
}
const iosApps = await Promise.all(
iosAppIds.map((app) => ({
platform: Platform.IOS,
directory: iosDir,
appId: app.appId,
bundleId: app.bundleId,
})),
);
return iosApps.flat();
}
async function processAndroidDir(dirPath: string, filePath: string): Promise<App[]> {
// Search for apps in the parent directory, not in the src/main directory
const androidDir = path.dirname(path.dirname(filePath));
const androidAppIds = await detectAppIdsForPlatform(dirPath, Platform.ANDROID);
if (androidAppIds.length === 0) {
return [
{
platform: Platform.ANDROID,
directory: androidDir,
},
];
}
const androidApps = await Promise.all(
androidAppIds.map((app) => ({
platform: Platform.ANDROID,
directory: androidDir,
appId: app.appId,
bundleId: app.bundleId,
})),
);
return androidApps.flat();
}
async function processFlutterDir(dirPath: string, filePath: string): Promise<App[]> {
const flutterDir = path.dirname(filePath);
const flutterAppIds = await detectAppIdsForPlatform(dirPath, Platform.FLUTTER);
if (flutterAppIds.length === 0) {
return [
{
platform: Platform.FLUTTER,
directory: flutterDir,
},
];
}
const flutterApps = await Promise.all(
flutterAppIds.map((app) => {
const flutterApp: App = {
platform: Platform.FLUTTER,
directory: flutterDir,
appId: app.appId,
bundleId: app.bundleId,
};
return flutterApp;
}),
);
return flutterApps.flat();
}
function isPathInside(parent: string, child: string): boolean {
const relativePath = path.relative(parent, child);
return !relativePath.startsWith(`..`);
}
async function packageJsonToWebApp(dirPath: string, packageJsonFile: string): Promise<App> {
const fullPath = path.join(dirPath, packageJsonFile);
const packageJson = JSON.parse((await fs.readFile(fullPath)).toString()) as PackageJSON;
return {
platform: Platform.WEB,
directory: path.dirname(packageJsonFile),
frameworks: getFrameworksFromPackageJson(packageJson),
};
}
const WEB_FRAMEWORKS: Framework[] = Object.values(Framework);
const WEB_FRAMEWORKS_SIGNALS: { [key in Framework]: string[] } = {
react: ["react", "next"],
angular: ["@angular/core"],
};
async function detectAppIdsForPlatform(
dirPath: string,
platform: Platform,
): Promise<AppIdentifier[]> {
let appIdFiles;
let extractFunc: (fileContent: string) => AppIdentifier[];
switch (platform) {
// Leaving web out of the mix for now because we have no strong conventions
// around where to put Firebase config. It could be anywhere in your codebase.
case Platform.ANDROID:
appIdFiles = await detectFiles(dirPath, "google-services*.json*");
extractFunc = extractAppIdentifiersAndroid;
break;
case Platform.IOS:
appIdFiles = await detectFiles(dirPath, "GoogleService-*.plist*");
extractFunc = extractAppIdentifierIos;
break;
case Platform.FLUTTER:
appIdFiles = await detectFiles(dirPath, "firebase_options.dart");
extractFunc = extractAppIdentifiersFlutter;
break;
default:
return [];
}
const allAppIds = await Promise.all(
appIdFiles.map(async (file) => {
const fileContent = (await fs.readFile(path.join(dirPath, file))).toString();
return extractFunc(fileContent);
}),
);
return allAppIds.flat();
}
function getFrameworksFromPackageJson(packageJson: PackageJSON): Framework[] {
const devDependencies = Object.keys(packageJson.devDependencies ?? {});
const dependencies = Object.keys(packageJson.dependencies ?? {});
const allDeps = Array.from(new Set([...devDependencies, ...dependencies]));
return WEB_FRAMEWORKS.filter((framework) =>
WEB_FRAMEWORKS_SIGNALS[framework].find((dep) => allDeps.includes(dep)),
);
}
/**
* Reads a firebase_options.dart file and extracts all appIds and bundleIds.
* @param fileContent content of the dart file.
* @return a list of appIds and bundleIds.
*/
export function extractAppIdentifiersFlutter(fileContent: string): AppIdentifier[] {
const optionsRegex = /FirebaseOptions\(([^)]*)\)/g;
const appIdRegex = /appId: '([^']*)'/;
const bundleIdRegex = /iosBundleId: '([^']*)'/;
const matches = fileContent.matchAll(optionsRegex);
const identifiers: AppIdentifier[] = [];
for (const match of matches) {
const optionsContent = match[1];
const appIdMatch = appIdRegex.exec(optionsContent);
const bundleIdMatch = bundleIdRegex.exec(optionsContent);
if (appIdMatch?.[1]) {
identifiers.push({
appId: appIdMatch[1],
bundleId: bundleIdMatch?.[1],
});
}
}
return identifiers;
}
/**
* Reads a GoogleService-Info.plist file and extracts the GOOGLE_APP_ID and BUNDLE_ID.
* @param fileContent content of the plist file.
* @return The GOOGLE_APP_ID and BUNDLE_ID or an empty array.
*/
export function extractAppIdentifierIos(fileContent: string): AppIdentifier[] {
const appIdRegex = /<key>GOOGLE_APP_ID<\/key>\s*<string>([^<]*)<\/string>/;
const bundleIdRegex = /<key>BUNDLE_ID<\/key>\s*<string>([^<]*)<\/string>/;
const appIdMatch = fileContent.match(appIdRegex);
const bundleIdMatch = fileContent.match(bundleIdRegex);
if (appIdMatch?.[1]) {
return [
{
appId: appIdMatch[1],
bundleId: bundleIdMatch?.[1],
},
];
}
return [];
}
/**
* Reads a google-services.json file and extracts all mobilesdk_app_id and package_name values.
* @param fileContent content of the google-services.json file.
* @return a list of mobilesdk_app_id and package_name values.
*/
export function extractAppIdentifiersAndroid(fileContent: string): AppIdentifier[] {
const identifiers: AppIdentifier[] = [];
try {
const config = JSON.parse(fileContent);
if (config.client && Array.isArray(config.client)) {
for (const client of config.client) {
if (client.client_info?.mobilesdk_app_id) {
identifiers.push({
appId: client.client_info.mobilesdk_app_id,
bundleId: client.client_info.android_client_info?.package_name,
});
}
}
}
} catch (e) {
// Handle parsing errors if necessary
console.error("Error parsing google-services.json:", e);
}
return identifiers;
}
/**
* Detects files matching a pattern within a directory, ignoring common dependency and build folders.
* @param dirPath The directory to search in.
* @param filePattern The glob pattern for the files to detect (e.g., "*.json").
* @return A promise that resolves to an array of file paths relative to `dirPath`.
*/
export async function detectFiles(dirPath: string, filePattern: string): Promise<string[]> {
const options = {
cwd: dirPath,
ignore: [
"**/dataconnect*/**",
"**/node_modules/**", // Standard dependency directory
"**/dist/**", // Common build output
"**/build/**", // Common build output
"**/out/**", // Another common build output
"**/.next/**", // Next.js build directory
"**/coverage/**", // Test coverage reports
],
absolute: false,
};
return glob(`**/${filePattern}`, options);
}