Skip to content

Commit f160329

Browse files
committed
Support kit backends in the emulator
1 parent 189d869 commit f160329

3 files changed

Lines changed: 71 additions & 26 deletions

File tree

src/emulator/controller.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as clc from "colorette";
22
import * as fs from "fs";
33
import * as path from "path";
44
import * as fsConfig from "../firestore/fsConfig";
5-
import * as proto from "../gcp/proto";
65

76
import { logger } from "../logger";
87
import { trackEmulator, trackGA4 } from "../track";
@@ -43,7 +42,7 @@ import { getProjectDefaultAccount } from "../auth";
4342
import { Options } from "../options";
4443
import { ParsedTriggerDefinition } from "./functionsEmulatorShared";
4544
import { ExtensionsEmulator } from "./extensionsEmulator";
46-
import { normalizeAndValidate, requireLocal } from "../functions/projectConfig";
45+
import { isKitConfig, normalizeAndValidate, requireLocal } from "../functions/projectConfig";
4746
import { requiresJava } from "./downloadableEmulators";
4847
import { prepareFrameworks } from "../frameworks";
4948
import * as experiments from "../experiments";
@@ -542,22 +541,39 @@ export async function startAll(
542541
`Cannot load functions from ${functionsDir} because it has invalid runtime ${runtime as string}`,
543542
);
544543
}
545-
const backend: EmulatableBackend = {
546-
functionsDir,
547-
runtime,
548-
codebase: localCfg.codebase,
549-
prefix: localCfg.prefix,
550-
env: {
551-
...options.extDevEnv,
552-
},
553-
secretEnv: [], // CF3 secrets are bound to specific functions, so we'll get them during trigger discovery.
554-
// TODO(b/213335255): predefinedTriggers and nodeMajorVersion are here to support ext:dev:emulators:* commands.
555-
// Ideally, we should handle that case via ExtensionEmulator.
556-
predefinedTriggers: options.extDevTriggers as ParsedTriggerDefinition[] | undefined,
557-
ignore: localCfg.ignore,
558-
};
559-
proto.convertIfPresent(backend, localCfg, "configDir", (cd) => path.join(projectDir, cd));
560-
emulatableBackends.push(backend);
544+
545+
if (isKitConfig(localCfg)) {
546+
for (const [instanceId, configDir] of Object.entries(localCfg.instances)) {
547+
const backend: EmulatableBackend = {
548+
functionsDir,
549+
runtime,
550+
codebase: instanceId,
551+
configDir: path.join(projectDir, configDir),
552+
env: {
553+
...options.extDevEnv,
554+
},
555+
secretEnv: [],
556+
predefinedTriggers: options.extDevTriggers as ParsedTriggerDefinition[] | undefined,
557+
ignore: localCfg.ignore,
558+
};
559+
emulatableBackends.push(backend);
560+
}
561+
} else {
562+
const backend: EmulatableBackend = {
563+
functionsDir,
564+
runtime,
565+
codebase: localCfg.codebase,
566+
...(localCfg.prefix ? { prefix: localCfg.prefix } : {}),
567+
...(localCfg.configDir ? { configDir: path.join(projectDir, localCfg.configDir) } : {}),
568+
env: {
569+
...options.extDevEnv,
570+
},
571+
secretEnv: [],
572+
predefinedTriggers: options.extDevTriggers as ParsedTriggerDefinition[] | undefined,
573+
ignore: localCfg.ignore,
574+
};
575+
emulatableBackends.push(backend);
576+
}
561577
}
562578
}
563579

src/functions/projectConfig.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,19 @@ export function isKitConfig(c: ValidatedSingle): c is ValidatedKitSingle {
346346
* @param c The validated functions config entry.
347347
* @param purpose Optional message to use in the error.
348348
*/
349-
export function requireLocal(c: ValidatedSingle, purpose?: string): ValidatedLocalSingle | ValidatedKitSingle {
349+
export function requireLocal(c: ValidatedKitSingle, purpose?: string): ValidatedKitSingle;
350+
export function requireLocal(
351+
c: ValidatedLocalSingle | ValidatedRemoteSingle,
352+
purpose?: string,
353+
): ValidatedLocalSingle;
354+
export function requireLocal(
355+
c: ValidatedSingle,
356+
purpose?: string,
357+
): ValidatedLocalSingle | ValidatedKitSingle;
358+
export function requireLocal(
359+
c: ValidatedSingle,
360+
purpose?: string,
361+
): ValidatedLocalSingle | ValidatedKitSingle {
350362
if (isRemoteConfig(c)) {
351363
const msg =
352364
purpose ??

src/serve/functions.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,30 @@ export class FunctionsServer {
3434
"Remote sources are not supported in the Functions emulator.",
3535
);
3636
const functionsDir = path.join(options.config.projectDir, localCfg.source);
37-
backends.push({
38-
functionsDir,
39-
codebase: localCfg.codebase,
40-
runtime: localCfg.runtime,
41-
env: {},
42-
secretEnv: [],
43-
});
37+
38+
if (projectConfig.isKitConfig(localCfg)) {
39+
for (const [instanceId, configDir] of Object.entries(localCfg.instances)) {
40+
backends.push({
41+
functionsDir,
42+
codebase: instanceId,
43+
configDir: path.join(options.config.projectDir, configDir),
44+
runtime: localCfg.runtime,
45+
env: {},
46+
secretEnv: [],
47+
});
48+
}
49+
} else {
50+
backends.push({
51+
functionsDir,
52+
codebase: localCfg.codebase,
53+
...(localCfg.configDir
54+
? { configDir: path.join(options.config.projectDir, localCfg.configDir) }
55+
: {}),
56+
runtime: localCfg.runtime,
57+
env: {},
58+
secretEnv: [],
59+
});
60+
}
4461
}
4562
this.backends = backends;
4663

0 commit comments

Comments
 (0)