Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import { configstore } from "../../../configstore";
import { trackGA4 } from "../../../track";

// Default GCP region for Data Connect
const DEFAULT_GCP_REGION = "us-east4";

const DATACONNECT_YAML_TEMPLATE = readTemplateSync("init/dataconnect/dataconnect.yaml");
const CONNECTOR_YAML_TEMPLATE = readTemplateSync("init/dataconnect/connector.yaml");
const SCHEMA_TEMPLATE = readTemplateSync("init/dataconnect/schema.gql");
Expand Down Expand Up @@ -94,7 +97,7 @@

// askQuestions prompts the user about the Data Connect service they want to init. Any prompting
// logic should live here, and _no_ actuation logic should live here.
export async function askQuestions(setup: Setup): Promise<void> {

Check warning on line 100 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const info: RequiredInfo = {
analyticsFlow: "cli",
appDescription: "",
Expand Down Expand Up @@ -125,6 +128,8 @@
}
if (hasBilling) {
await promptForCloudSQL(setup, info);
} else if (info.appDescription) {
await promptForLocation(setup, info);
}
}
setup.featureInfo = setup.featureInfo || {};
Expand All @@ -135,10 +140,10 @@

// actuate writes product specific files and makes product specifc API calls.
// It does not handle writing firebase.json and .firebaserc
export async function actuate(setup: Setup, config: Config, options: any): Promise<void> {

Check warning on line 143 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 143 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
// Most users will want to persist data between emulator runs, so set this to a reasonable default.
const dir: string = config.get("dataconnect.source", "dataconnect");

Check warning on line 145 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`);

Check warning on line 146 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
config.set("emulators.dataconnect.dataDir", dataDir);

const info = setup.featureInfo?.dataconnect;
Expand All @@ -148,7 +153,7 @@
// Populate the default values of required fields.
info.serviceId = info.serviceId || defaultServiceId();
info.cloudSqlInstanceId = info.cloudSqlInstanceId || `${info.serviceId.toLowerCase()}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.locationId = info.locationId || DEFAULT_GCP_REGION;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;

try {
Expand All @@ -166,7 +171,7 @@
setup.instructions.push(
`You can visualize the Data Connect Schema in Firebase Console:

https://console.firebase.google.com/project/${setup.projectId!}/dataconnect/locations/${info.locationId}/services/${info.serviceId}/schema`,

Check warning on line 174 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
);
}
if (!setup.isBillingEnabled) {
Expand All @@ -181,7 +186,7 @@
setup: Setup,
config: Config,
info: RequiredInfo,
options: any,

Check warning on line 189 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
): Promise<void> {
const projectId = setup.projectId;
if (!projectId) {
Expand Down Expand Up @@ -287,7 +292,7 @@
{ schemaGql: schemaFiles, connectors: connectors, seedDataGql: seedDataGql },
options,
);
} catch (err: any) {

Check warning on line 295 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
logLabeledError("dataconnect", `Operation Generation failed...`);
// GiF generate operation API has stability concerns.
// Fallback to save only the generated schema.
Expand Down Expand Up @@ -361,9 +366,9 @@
config: Config,
info: RequiredInfo,
serviceGql: ServiceGQL,
options: any,

Check warning on line 369 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
): Promise<void> {
const dir: string = config.get("dataconnect.source") || "dataconnect";

Check warning on line 371 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const subbedDataconnectYaml = subDataconnectYamlValues({
...info,
connectorDirs: serviceGql.connectors.map((c) => c.path),
Expand Down Expand Up @@ -435,10 +440,10 @@
}): string {
const replacements: Record<string, string> = {
serviceId: "__serviceId__",
locationId: "__location__",
cloudSqlDatabase: "__cloudSqlDatabase__",
cloudSqlInstanceId: "__cloudSqlInstanceId__",
connectorDirs: "__connectorDirs__",
locationId: "__location__",
};
let replaced = DATACONNECT_YAML_TEMPLATE;
for (const [k, v] of Object.entries(replacementValues)) {
Expand Down Expand Up @@ -614,12 +619,7 @@
}

if (info.locationId === "") {
const choices = await locationChoices(setup);
info.locationId = await select<string>({
message: "What location would like to use?",
choices,
default: "us-east4",
});
await promptForLocation(setup, info);
info.shouldProvisionCSQL = await confirm({
message: `Would you like to provision your Cloud SQL instance and database now?`,
default: true,
Expand All @@ -642,6 +642,17 @@
return;
}

async function promptForLocation(setup: Setup, info: RequiredInfo): Promise<void> {
if (info.locationId === "") {
const choices = await locationChoices(setup);
info.locationId = await select<string>({
message: "What location would like to use?",
choices,
default: DEFAULT_GCP_REGION,
});
}
}

async function locationChoices(setup: Setup) {
if (setup.projectId) {
const locations = await listLocations(setup.projectId);
Expand Down
Loading