Skip to content

Commit

Permalink
Add default value for dataDir in init dataconnect (#7989)
Browse files Browse the repository at this point in the history
* Add dataDir into init

* Adding dataDir to init dataconnect

* PR fixes

* Use the right dataconnect directory every time

* typo
  • Loading branch information
joehan authored Nov 22, 2024
1 parent 7674651 commit 49c5ac5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added default value for `emulators.dataconnect.dataDir` to `init dataconnect`.
24 changes: 23 additions & 1 deletion src/emulator/initEmulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, string> | null>;
type InitFn = (config: Config) => Promise<Record<string, string> | null>;
type AdditionalInitFnsType = Partial<Record<Emulators, InitFn>>;

export const AdditionalInitFns: AdditionalInitFnsType = {
Expand Down Expand Up @@ -46,6 +47,27 @@ export const AdditionalInitFns: AdditionalInitFnsType = {

return mapToObject(additionalConfigs);
},
[Emulators.DATACONNECT]: async (config: Config) => {
const additionalConfig: Record<string, string> = {};
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<string, string>): Record<string, string> {
Expand Down
5 changes: 5 additions & 0 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export async function doSetup(setup: Setup, config: Config): Promise<void> {
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());
Expand Down
5 changes: 3 additions & 2 deletions src/init/features/emulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 49c5ac5

Please sign in to comment.