Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5814b1b

Browse files
committedOct 24, 2021
feat(core): support initializing app from path
1 parent 528e1ba commit 5814b1b

File tree

53 files changed

+1160
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1160
-14
lines changed
 

‎packages/firebase-core/index.android.ts

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IFirebaseOptions, FirebaseConfig } from './common';
2-
import { Utils } from '@nativescript/core';
2+
import { knownFolders, Utils, File } from '@nativescript/core';
33
export * from './utils';
44

55
export class FirebaseError extends Error {
@@ -257,6 +257,117 @@ export class Firebase {
257257

258258
return fbApp;
259259
}
260+
261+
262+
initializeAppWithPath(path: string, options: FirebaseOptions = null, config?: FirebaseConfig) {
263+
let json;
264+
const ctx = Utils.android.getApplicationContext() as android.content.Context;
265+
if (path.startsWith('res://')) {
266+
const jsonStr = (<any>org).nativescript.firebase.core.FirebaseCore.readRawAsset(ctx, path);
267+
console.log('jsonStr', jsonStr);
268+
json = JSON.parse(jsonStr);
269+
} else {
270+
if (path.startsWith('~/')) {
271+
path = knownFolders.currentApp().path + '/' + path.replace('~/', '');
272+
}
273+
json = require(path);
274+
}
275+
276+
277+
// always use first client
278+
279+
const client = json['client'][0];
280+
const oauth_clients = client['oauth_client'];
281+
const project_info = json['project_info'];
282+
const client_info = client['client_info'];
283+
284+
285+
let default_web_client_id = null;
286+
const firebase_database_url = project_info['firebase_url'] || null;
287+
const gcm_defaultSenderId = project_info['project_number'] || null;
288+
const google_api_key = client['api_key']?.['current_key'] ?? null;
289+
const google_app_id = client_info['mobilesdk_app_id'] || null;
290+
const google_crash_reporting_api_key = google_app_id;
291+
const google_storage_bucket = project_info['storage_bucket'] || null;
292+
const project_id = project_info['project_id'] || null;
293+
294+
for (let i = 0; i < oauth_clients.length; i++) {
295+
const oauth_client = oauth_clients[i];
296+
if (oauth_client.client_type === 3) {
297+
default_web_client_id = oauth_client['client_id'];
298+
}
299+
}
300+
301+
const nativeOptions = new com.google.firebase.FirebaseOptions.Builder();
302+
if (google_api_key) {
303+
nativeOptions.setApiKey(google_api_key);
304+
}
305+
306+
if (google_app_id) {
307+
nativeOptions.setApplicationId(google_app_id);
308+
}
309+
310+
if (firebase_database_url) {
311+
nativeOptions.setDatabaseUrl(firebase_database_url);
312+
}
313+
314+
if (gcm_defaultSenderId) {
315+
nativeOptions.setGcmSenderId(gcm_defaultSenderId);
316+
}
317+
318+
if (project_id) {
319+
nativeOptions.setProjectId(project_id);
320+
}
321+
322+
323+
if (google_storage_bucket) {
324+
nativeOptions.setStorageBucket(google_storage_bucket);
325+
}
326+
327+
328+
if (options?.apiKey) {
329+
nativeOptions.setApiKey(options.apiKey);
330+
}
331+
332+
if (options?.gcmSenderId) {
333+
nativeOptions.setGcmSenderId(options.gcmSenderId);
334+
}
335+
336+
if (options?.databaseURL) {
337+
nativeOptions.setDatabaseUrl(options.databaseURL);
338+
}
339+
340+
if (options?.googleAppId) {
341+
nativeOptions.setApplicationId(options.googleAppId);
342+
}
343+
344+
if (options?.projectId) {
345+
nativeOptions.setProjectId(options.projectId);
346+
}
347+
348+
if (options?.storageBucket) {
349+
nativeOptions.setStorageBucket(options.storageBucket);
350+
}
351+
352+
if (options?.trackingId) {
353+
nativeOptions.setGaTrackingId(options.trackingId);
354+
}
355+
356+
357+
const app = com.google.firebase.FirebaseApp.initializeApp(ctx, nativeOptions.build());
358+
359+
if (app && typeof config === 'object' && typeof config.automaticResourceManagement === 'boolean') {
360+
app.setAutomaticResourceManagementEnabled(config.automaticDataCollectionEnabled);
361+
}
362+
363+
364+
const fbApp = FirebaseApp.fromNative(app);
365+
366+
if (!defaultApp) {
367+
defaultApp = fbApp;
368+
}
369+
return fbApp;
370+
}
260371
}
261372

262373
export function firebase() {

‎packages/firebase-core/index.d.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FirebaseConfig, IFirebaseOptions} from './common';
1+
import { FirebaseConfig, IFirebaseOptions } from './common';
22

33

44
declare function serialize(data: any): any;
@@ -12,27 +12,27 @@ declare class FirebaseOptions implements IFirebaseOptions {
1212

1313
apiKey?: string;
1414

15-
gcmSenderId?: string;
15+
gcmSenderId?: string;
1616

17-
androidClientId?: string;
17+
androidClientId?: string;
1818

19-
appGroupId?: string;
19+
appGroupId?: string;
2020

21-
bundleId?: string;
21+
bundleId?: string;
2222

23-
clientId?: string;
23+
clientId?: string;
2424

25-
databaseURL?: string;
25+
databaseURL?: string;
2626

27-
deepLinkURLScheme?: string;
27+
deepLinkURLScheme?: string;
2828

29-
googleAppId?: string;
29+
googleAppId?: string;
3030

31-
projectId?: string;
31+
projectId?: string;
3232

33-
storageBucket?: string;
33+
storageBucket?: string;
3434

35-
trackingId?: string;
35+
trackingId?: string;
3636
}
3737

3838
declare class FirebaseApp {
@@ -50,6 +50,8 @@ export interface Firebase {
5050
app(name?: string): FirebaseApp;
5151

5252
initializeApp(options?: FirebaseOptions, configOrName?: FirebaseConfig | string): FirebaseApp;
53+
54+
initializeAppWithPath(path: string, options?: FirebaseOptions, config?: FirebaseConfig): FirebaseApp;
5355
}
5456

5557
export function firebase(): Firebase;

0 commit comments

Comments
 (0)
Please sign in to comment.