Skip to content

Commit

Permalink
print instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
fredzqm committed Oct 7, 2024
1 parent dc10b6b commit b5166cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class Config {
fs.removeSync(this.path(p));
}

askWriteProjectFile(p: string, content: any, force?: boolean) {
askWriteProjectFile(p: string, content: any, force?: boolean, confirmByDefault?: boolean) {
const writeTo = this.path(p);
let next;
if (typeof content !== "string") {
Expand All @@ -243,7 +243,7 @@ export class Config {
next = promptOnce({
type: "confirm",
message: "File " + clc.underline(p) + " already exists. Overwrite?",
default: false,
default: !!confirmByDefault,
});
} else {
next = Promise.resolve(true);
Expand Down
11 changes: 4 additions & 7 deletions src/dataconnect/freeTrial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ export function printFreeTrialUnavailable(
}

export function upgradeInstructions(projectId: string): string {
return `If you'd like to provision a CloudSQL instance on the Firebase Data Connect no-cost trial:
1. Please upgrade to the pay-as-you-go (Blaze) billing plan.
return `If you'd like to provision a CloudSQL Postgres instance on the Firebase Data Connect no-cost trial:
1. Please upgrade to the pay-as-you-go (Blaze) billing plan. Visit the following page:
https://console.firebase.google.com/project/${projectId}/usage/details
2. Run ${clc.bold("firebase init dataconnect")} again to configure the Cloud SQL instance.
3. Run ${clc.bold("firebase deploy --only dataconnect")} to deploy your Data Connect service.
To upgrade your project, visit the following URL:
https://console.firebase.google.com/project/${projectId}/usage/details`;
3. Run ${clc.bold("firebase deploy --only dataconnect")} to deploy your Data Connect service.`;
}
6 changes: 3 additions & 3 deletions src/deploy/dataconnect/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import { upgradeInstructions } from "../../dataconnect/freeTrial";
*/
export default async function (context: any, options: DeployOptions): Promise<void> {
const projectId = needProjectId(options);
if (!(await checkBillingEnabled(projectId))) {
throw new FirebaseError(upgradeInstructions(projectId));
}
await ensureApis(projectId);
await requireTosAcceptance(DATA_CONNECT_TOS_ID)(options);
const serviceCfgs = readFirebaseJson(options.config);
Expand Down Expand Up @@ -61,9 +64,6 @@ export default async function (context: any, options: DeployOptions): Promise<vo
serviceInfos,
filters,
};
if (!(await checkBillingEnabled(projectId))) {
throw new FirebaseError(upgradeInstructions(projectId));
}
utils.logLabeledBullet("dataconnect", `Successfully prepared schema and connectors`);
if (options.dryRun) {
for (const si of serviceInfos) {
Expand Down
39 changes: 19 additions & 20 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Schema, Service, File, Platform } from "../../../dataconnect/types";
import { parseCloudSQLInstanceName, parseServiceName } from "../../../dataconnect/names";
import { logger } from "../../../logger";
import { readTemplateSync } from "../../../templates";
import { logBullet, logSuccess } from "../../../utils";
import { logBullet } from "../../../utils";
import { checkBillingEnabled } from "../../../gcp/cloudbilling";
import * as sdk from "./sdk";
import { getPlatformFromFolder } from "../../../dataconnect/fileUtils";
Expand Down Expand Up @@ -74,22 +74,29 @@ const defaultSchema = { path: "schema.gql", content: SCHEMA_TEMPLATE };

// doSetup is split into 2 phases - ask questions and then actuate files and API calls based on those answers.
export async function doSetup(setup: Setup, config: Config): Promise<void> {
const info = await askQuestions(setup);
const isBillingEnabled = setup.projectId ? await checkBillingEnabled(setup.projectId) : false;
if (setup.projectId) {
isBillingEnabled ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId);
}
const info = await askQuestions(setup, isBillingEnabled);
await actuate(setup, config, info);

const cwdPlatformGuess = await getPlatformFromFolder(process.cwd());
if (cwdPlatformGuess !== Platform.NONE) {
await sdk.doSetup(setup, config);
} else {
logBullet(
`If you'd like to add the generated SDK to your app your later, run ${clc.bold("firebase init dataconnect:sdk")}`,
`If you'd like to add the generated SDK to your app your, run ${clc.bold("firebase init dataconnect:sdk")}`,
);
}
if (setup.projectId && !isBillingEnabled) {
logBullet(upgradeInstructions(setup.projectId));
}
}

// 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.
async function askQuestions(setup: Setup): Promise<RequiredInfo> {
async function askQuestions(setup: Setup, isBillingEnabled: boolean): Promise<RequiredInfo> {
let info: RequiredInfo = {
serviceId: "",
locationId: "",
Expand All @@ -101,10 +108,6 @@ async function askQuestions(setup: Setup): Promise<RequiredInfo> {
schemaGql: [defaultSchema],
shouldProvisionCSQL: false,
};
const isBillingEnabled = setup.projectId ? await checkBillingEnabled(setup.projectId) : false;
if (setup.projectId) {
isBillingEnabled ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId);
}
// Query backend and pick up any existing services quickly.
info = await promptForExistingServices(setup, info, isBillingEnabled);

Expand Down Expand Up @@ -136,19 +139,11 @@ async function askQuestions(setup: Setup): Promise<RequiredInfo> {
}))
);
} else {
if (requiredConfigUnset) {
logBullet(
`Setting placeholder values in dataconnect.yaml. You can edit these before you deploy to specify different IDs or regions.`,
);
}
info.serviceId = info.serviceId || basename(process.cwd());
info.cloudSqlInstanceId = info.cloudSqlInstanceId || `${info.serviceId || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
}
if (!isBillingEnabled && setup.projectId) {
logBullet(upgradeInstructions(setup.projectId));
}
return info;
}

Expand Down Expand Up @@ -178,12 +173,16 @@ async function writeFiles(config: Config, info: RequiredInfo) {
});

config.set("dataconnect", { source: dir });
await config.askWriteProjectFile(join(dir, "dataconnect.yaml"), subbedDataconnectYaml);
await config.askWriteProjectFile(
join(dir, "dataconnect.yaml"),
subbedDataconnectYaml,
false,
// Default to override dataconnect.yaml
// Sole purpose of `firebase init dataconnect` is to update `dataconnect.yaml`.
true,
);

if (info.schemaGql.length) {
logSuccess(
"The service you chose already has GQL files deployed. We'll use those instead of the default templates.",
);
for (const f of info.schemaGql) {
await config.askWriteProjectFile(join(dir, "schema", f.path), f.content);
}
Expand Down

0 comments on commit b5166cf

Please sign in to comment.