From 49c5ac5fe87301bd08e1387e90a1cfae1e5879d7 Mon Sep 17 00:00:00 2001 From: joehan Date: Fri, 22 Nov 2024 10:15:15 -0800 Subject: [PATCH] Add default value for `dataDir` in `init dataconnect` (#7989) * Add dataDir into init * Adding dataDir to init dataconnect * PR fixes * Use the right dataconnect directory every time * typo --- CHANGELOG.md | 1 + src/emulator/initEmulators.ts | 24 +++++++++++++++++++++++- src/init/features/dataconnect/index.ts | 5 +++++ src/init/features/emulators.ts | 5 +++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb2d..702fded3100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- Added default value for `emulators.dataconnect.dataDir` to `init dataconnect`. diff --git a/src/emulator/initEmulators.ts b/src/emulator/initEmulators.ts index ab9a9a04f69..deae573fad9 100644 --- a/src/emulator/initEmulators.ts +++ b/src/emulator/initEmulators.ts @@ -7,8 +7,9 @@ import { EmulatorLogger } from "./emulatorLogger"; import { Emulators } from "./types"; import { exportConfig } from "../apphosting/config"; import { detectProjectRoot } from "../detectProjectRoot"; +import { Config } from "../config"; -type InitFn = () => Promise | null>; +type InitFn = (config: Config) => Promise | null>; type AdditionalInitFnsType = Partial>; export const AdditionalInitFns: AdditionalInitFnsType = { @@ -46,6 +47,27 @@ export const AdditionalInitFns: AdditionalInitFnsType = { return mapToObject(additionalConfigs); }, + [Emulators.DATACONNECT]: async (config: Config) => { + const additionalConfig: Record = {}; + const defaultDataConnectDir = config.get("dataconnect.source", "dataconnect"); + const defaultDataDir = config.get( + "emulators.dataconnect.dataDir", + `${defaultDataConnectDir}/.dataconnect/pgliteData`, + ); + if ( + await promptOnce({ + name: "dataDir", + type: "confirm", + message: + "Do you want to persist Postgres data from the Data Connect emulator between runs? " + + `Data will be saved to ${defaultDataDir}. ` + + `You can change this directory by editing 'firebase.json#emulators.dataconnect.dataDir'.`, + }) + ) { + additionalConfig["dataDir"] = defaultDataDir; + } + return additionalConfig; + }, }; function mapToObject(map: Map): Record { diff --git a/src/init/features/dataconnect/index.ts b/src/init/features/dataconnect/index.ts index f16ce99a050..fdb68825286 100644 --- a/src/init/features/dataconnect/index.ts +++ b/src/init/features/dataconnect/index.ts @@ -79,6 +79,11 @@ export async function doSetup(setup: Setup, config: Config): Promise { isBillingEnabled ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId); } const info = await askQuestions(setup, isBillingEnabled); + // Most users will want to perist data between emulator runs, so set this to a reasonable default. + + const dir: string = config.get("dataconnect.source", "dataconnect"); + const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`); + config.set("emulators.dataconnect.dataDir", dataDir); await actuate(setup, config, info); const cwdPlatformGuess = await getPlatformFromFolder(process.cwd()); diff --git a/src/init/features/emulators.ts b/src/init/features/emulators.ts index 850a1f92a5f..7756897900f 100644 --- a/src/init/features/emulators.ts +++ b/src/init/features/emulators.ts @@ -6,13 +6,14 @@ import { Constants } from "../../emulator/constants"; import { downloadIfNecessary } from "../../emulator/downloadableEmulators"; import { Setup } from "../index"; import { AdditionalInitFns } from "../../emulator/initEmulators"; +import { Config } from "../../config"; interface EmulatorsInitSelections { emulators?: Emulators[]; download?: boolean; } -export async function doSetup(setup: Setup, config: any) { +export async function doSetup(setup: Setup, config: Config) { const choices = ALL_SERVICE_EMULATORS.map((e) => { return { value: e, @@ -57,7 +58,7 @@ export async function doSetup(setup: Setup, config: any) { const additionalInitFn = AdditionalInitFns[selected]; if (additionalInitFn) { - const additionalOptions = await additionalInitFn(); + const additionalOptions = await additionalInitFn(config); if (additionalOptions) { setup.config.emulators[selected] = { ...setup.config.emulators[selected],