|
1 | 1 | import { Constants } from "./constants";
|
2 |
| -import { Emulators } from "./types"; |
3 |
| -import { EmulatorRegistry } from "./registry"; |
| 2 | +import { EmulatorInfo, Emulators } from "./types"; |
| 3 | +import { formatHost } from "./functionsEmulatorShared"; |
4 | 4 |
|
5 | 5 | /**
|
6 | 6 | * Adds or replaces emulator-related env vars (for Admin SDKs, etc.).
|
7 | 7 | * @param env a `process.env`-like object or Record to be modified
|
| 8 | + * @param emulators the emulator info to use |
8 | 9 | */
|
9 |
| -export function setEnvVarsForEmulators(env: Record<string, string | undefined>): void { |
10 |
| - if (EmulatorRegistry.isRunning(Emulators.DATABASE)) { |
11 |
| - env[Constants.FIREBASE_DATABASE_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.DATABASE).host; |
12 |
| - } |
13 |
| - |
14 |
| - if (EmulatorRegistry.isRunning(Emulators.FIRESTORE)) { |
15 |
| - const { host } = EmulatorRegistry.url(Emulators.FIRESTORE); |
16 |
| - env[Constants.FIRESTORE_EMULATOR_HOST] = host; |
17 |
| - env[Constants.FIRESTORE_EMULATOR_ENV_ALT] = host; |
18 |
| - } |
19 |
| - |
20 |
| - if (EmulatorRegistry.isRunning(Emulators.STORAGE)) { |
21 |
| - const { host } = EmulatorRegistry.url(Emulators.STORAGE); |
22 |
| - env[Constants.FIREBASE_STORAGE_EMULATOR_HOST] = host; |
23 |
| - // The protocol is required for the Google Cloud Storage Node.js Client SDK. |
24 |
| - env[Constants.CLOUD_STORAGE_EMULATOR_HOST] = `http://${host}`; |
25 |
| - } |
26 |
| - |
27 |
| - if (EmulatorRegistry.isRunning(Emulators.AUTH)) { |
28 |
| - env[Constants.FIREBASE_AUTH_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.AUTH).host; |
29 |
| - } |
30 |
| - |
31 |
| - if (EmulatorRegistry.isRunning(Emulators.HUB)) { |
32 |
| - env[Constants.FIREBASE_EMULATOR_HUB] = EmulatorRegistry.url(Emulators.HUB).host; |
33 |
| - } |
34 |
| - |
35 |
| - const pubsubEmulator = EmulatorRegistry.isRunning(Emulators.PUBSUB); |
36 |
| - if (pubsubEmulator) { |
37 |
| - env[Constants.PUBSUB_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.PUBSUB).host; |
38 |
| - } |
39 |
| - |
40 |
| - if (EmulatorRegistry.isRunning(Emulators.EVENTARC)) { |
41 |
| - // The protocol is required for the Firebase Admin Node.js SDK for Eventarc. |
42 |
| - // https://github.com/firebase/firebase-admin-node/blob/ee60cd1acb8722ba4081b9837d2f90101e2b3227/src/eventarc/eventarc-client-internal.ts#L105 |
43 |
| - env[Constants.CLOUD_EVENTARC_EMULATOR_HOST] = `http://${ |
44 |
| - EmulatorRegistry.url(Emulators.EVENTARC).host |
45 |
| - }`; |
| 10 | +export function setEnvVarsForEmulators( |
| 11 | + env: Record<string, string | undefined>, |
| 12 | + emulators: EmulatorInfo[] |
| 13 | +): void { |
| 14 | + for (const emu of emulators) { |
| 15 | + const host = formatHost(emu); |
| 16 | + switch (emu.name) { |
| 17 | + case Emulators.FIRESTORE: |
| 18 | + env[Constants.FIRESTORE_EMULATOR_HOST] = host; |
| 19 | + env[Constants.FIRESTORE_EMULATOR_ENV_ALT] = host; |
| 20 | + break; |
| 21 | + case Emulators.DATABASE: |
| 22 | + env[Constants.FIREBASE_DATABASE_EMULATOR_HOST] = host; |
| 23 | + break; |
| 24 | + case Emulators.STORAGE: |
| 25 | + env[Constants.FIREBASE_STORAGE_EMULATOR_HOST] = host; |
| 26 | + // The protocol is required for the Google Cloud Storage Node.js Client SDK. |
| 27 | + env[Constants.CLOUD_STORAGE_EMULATOR_HOST] = `http://${host}`; |
| 28 | + break; |
| 29 | + case Emulators.AUTH: |
| 30 | + env[Constants.FIREBASE_AUTH_EMULATOR_HOST] = host; |
| 31 | + break; |
| 32 | + case Emulators.HUB: |
| 33 | + env[Constants.FIREBASE_EMULATOR_HUB] = host; |
| 34 | + break; |
| 35 | + case Emulators.PUBSUB: |
| 36 | + env[Constants.PUBSUB_EMULATOR_HOST] = host; |
| 37 | + break; |
| 38 | + case Emulators.EVENTARC: |
| 39 | + env[Constants.CLOUD_EVENTARC_EMULATOR_HOST] = `http://${host}`; |
| 40 | + break; |
| 41 | + } |
46 | 42 | }
|
47 | 43 | }
|
0 commit comments