From 7f252c0a900caaadf3736b5a98198068ec809fa2 Mon Sep 17 00:00:00 2001 From: Scott Prue Date: Sat, 11 Apr 2020 22:51:11 -0700 Subject: [PATCH] v1.1.0 (#117) * fix(core): use GCLOUD_PROJECT as default project when not using emulator --- package.json | 2 +- src/firebase-utils.ts | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 3620cc7c..be9e0131 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cypress-firebase", - "version": "1.0.0", + "version": "1.1.0", "description": "Utilities to help testing Firebase projects with Cypress.", "main": "lib/index.js", "module": "lib/index.js", diff --git a/src/firebase-utils.ts b/src/firebase-utils.ts index 07ff7479..d54ff0f5 100644 --- a/src/firebase-utils.ts +++ b/src/firebase-utils.ts @@ -100,10 +100,11 @@ export function initializeFirebase(adminInstance: any): admin.app.App { } else { // Get service account from local file falling back to environment variables const serviceAccount = getServiceAccount(); - const projectId = serviceAccount?.project_id; + const projectId = + process.env.GCLOUD_PROJECT || serviceAccount?.project_id; if (!isString(projectId)) { const missingProjectIdErr = - 'Error project_id from service account to initialize Firebase.'; + 'Error GCLOUD_PROJECT environment variable or project_id from service account to initialize Firebase.'; console.error(`cypress-firebase: ${missingProjectIdErr}`); // eslint-disable-line no-console throw new Error(missingProjectIdErr); } @@ -111,15 +112,23 @@ export function initializeFirebase(adminInstance: any): admin.app.App { 'firebase-top-agent-int', 'top-agent-int', ); + + const appSettings: any = { + databaseURL: `https://${cleanProjectId}.firebaseio.com`, + }; + + if (serviceAccount) { + appSettings.credential = adminInstance.credential.cert( + serviceAccount as any, + ); + } + + fbInstance = adminInstance.initializeApp(appSettings); /* eslint-disable no-console */ console.log( - `cypress-firebase: Initialized with Service Account for project "${cleanProjectId}"`, + `cypress-firebase: Initialized app with project "${cleanProjectId}"`, ); /* eslint-enable no-console */ - fbInstance = adminInstance.initializeApp({ - credential: adminInstance.credential.cert(serviceAccount as any), - databaseURL: `https://${cleanProjectId}.firebaseio.com`, - }); } return fbInstance;