Skip to content

Commit

Permalink
[FDC] Remove confusing prompts (#7801)
Browse files Browse the repository at this point in the history
* remove prompt at the end of init dataconnect:sdk

* Dont override gql files when picking existing empty service

* Dont override gql files when picking existing empty service

* schema default

* tests

* Update src/init/features/dataconnect/index.ts

---------

Co-authored-by: joehan <[email protected]>
  • Loading branch information
fredzqm and joehan authored Oct 7, 2024
1 parent cbc44c0 commit 36ce505
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
22 changes: 18 additions & 4 deletions src/init/features/dataconnect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,30 @@ describe("init dataconnect", () => {
requiredInfo: mockRequiredInfo(),
config: mockConfig(),
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml", "dataconnect/schema/schema.gql"],
expectedFiles: ["dataconnect/dataconnect.yaml"],
expectCSQLProvisioning: false,
},
{
desc: "should use existing directory if there is one in firebase.json",
requiredInfo: mockRequiredInfo(),
config: mockConfig({ dataconnect: { source: "not-dataconnect" } }),
expectedSource: "not-dataconnect",
expectedFiles: ["not-dataconnect/dataconnect.yaml", "not-dataconnect/schema/schema.gql"],
expectedFiles: ["not-dataconnect/dataconnect.yaml"],
expectCSQLProvisioning: false,
},
{
desc: "should write schema files",
requiredInfo: mockRequiredInfo({
schemaGql: [
{
path: "schema.gql",
content: "## Fake GQL",
},
],
}),
config: mockConfig({}),
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml", "dataconnect/schema/schema.gql"],
expectCSQLProvisioning: false,
},
{
Expand All @@ -70,7 +85,6 @@ describe("init dataconnect", () => {
expectedSource: "dataconnect",
expectedFiles: [
"dataconnect/dataconnect.yaml",
"dataconnect/schema/schema.gql",
"dataconnect/hello/connector.yaml",
"dataconnect/hello/queries.gql",
],
Expand All @@ -83,7 +97,7 @@ describe("init dataconnect", () => {
}),
config: mockConfig({}),
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml", "dataconnect/schema/schema.gql"],
expectedFiles: ["dataconnect/dataconnect.yaml"],
expectCSQLProvisioning: true,
},
];
Expand Down
23 changes: 16 additions & 7 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface RequiredInfo {
shouldProvisionCSQL: boolean;
}

const emptyConnector = {
id: "default",
path: "./connector",
files: [],
};

const defaultConnector = {
id: "default",
path: "./connector",
Expand All @@ -64,6 +70,8 @@ const defaultConnector = {
],
};

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);
Expand All @@ -77,8 +85,6 @@ export async function doSetup(setup: Setup, config: Config): Promise<void> {
`If you'd like to add the generated SDK to your app your later, run ${clc.bold("firebase init dataconnect:sdk")}`,
);
}

logger.info("");
}

// askQuestions prompts the user about the Data Connect service they want to init. Any prompting
Expand All @@ -92,7 +98,7 @@ async function askQuestions(setup: Setup): Promise<RequiredInfo> {
cloudSqlDatabase: "",
isNewDatabase: false,
connectors: [defaultConnector],
schemaGql: [],
schemaGql: [defaultSchema],
shouldProvisionCSQL: false,
};
const isBillingEnabled = setup.projectId ? await checkBillingEnabled(setup.projectId) : false;
Expand All @@ -110,7 +116,9 @@ async function askQuestions(setup: Setup): Promise<RequiredInfo> {
isBillingEnabled && requiredConfigUnset
? await confirm({
message: `Would you like to configure your backend resources now?`,
default: false,
// For Blaze Projects, configure Cloud SQL by default.
// TODO: For Spark projects, allow them to configure Cloud SQL but deploy as unlinked Postgres.
default: true,
})
: false;
if (shouldConfigureBackend) {
Expand Down Expand Up @@ -177,8 +185,6 @@ async function writeFiles(config: Config, info: RequiredInfo) {
for (const f of info.schemaGql) {
await config.askWriteProjectFile(join(dir, "schema", f.path), f.content);
}
} else {
await config.askWriteProjectFile(join(dir, "schema", "schema.gql"), SCHEMA_TEMPLATE);
}
for (const c of info.connectors) {
await writeConnectorFiles(config, c);
Expand Down Expand Up @@ -278,6 +284,9 @@ async function checkExistingInstances(
const serviceName = parseServiceName(choice.service.name);
info.serviceId = serviceName.serviceId;
info.locationId = serviceName.location;
// If the existing service has no schema, don't override any gql files.
info.schemaGql = [];
info.connectors = [emptyConnector];
if (choice.schema) {
const primaryDatasource = choice.schema.datasources.find((d) => d.postgresql);
if (primaryDatasource?.postgresql?.cloudSql.instance) {
Expand All @@ -286,7 +295,7 @@ async function checkExistingInstances(
);
info.cloudSqlInstanceId = instanceName.instanceId;
}
if (choice.schema.source.files) {
if (choice.schema.source.files?.length) {
info.schemaGql = choice.schema.source.files;
}
info.cloudSqlDatabase = primaryDatasource?.postgresql?.database ?? "";
Expand Down
14 changes: 4 additions & 10 deletions src/init/features/dataconnect/sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,17 @@ describe("init dataconnect:sdk", () => {
shouldGenerate: boolean;
}[] = [
{
desc: "should write files and generate code if shouldGenerate=true",
sdkInfo: mockSDKInfo(true),
desc: "should write files and generate code",
sdkInfo: mockSDKInfo(),
shouldGenerate: true,
},
{
desc: "should write files and not generate code if shouldGenerate=false",
sdkInfo: mockSDKInfo(false),
shouldGenerate: false,
},
];

for (const c of cases) {
it(c.desc, async () => {
generateStub.resolves();
fsStub.returns({});
await sdk.actuate(c.sdkInfo, "TEST_PROJECT");
await sdk.actuate(c.sdkInfo);
expect(generateStub.called).to.equal(c.shouldGenerate);
expect(fsStub.args).to.deep.equal([
[
Expand All @@ -61,7 +56,7 @@ describe("init dataconnect:sdk", () => {
});
});

function mockSDKInfo(shouldGenerate: boolean): sdk.SDKInfo {
function mockSDKInfo(): sdk.SDKInfo {
return {
connectorYamlContents: CONNECTOR_YAML_CONTENTS,
connectorInfo: {
Expand All @@ -74,7 +69,6 @@ function mockSDKInfo(shouldGenerate: boolean): sdk.SDKInfo {
connectorId: "app",
},
},
shouldGenerate,
displayIOSWarning: false,
};
}
28 changes: 9 additions & 19 deletions src/init/features/dataconnect/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ export const FDC_APP_FOLDER = "_FDC_APP_FOLDER";
export type SDKInfo = {
connectorYamlContents: string;
connectorInfo: ConnectorInfo;
shouldGenerate: boolean;
displayIOSWarning: boolean;
};
export async function doSetup(setup: Setup, config: Config): Promise<void> {
const sdkInfo = await askQuestions(setup, config);
await actuate(sdkInfo, setup.projectId);
await actuate(sdkInfo);
logSuccess(
`If you'd like to generate additional SDKs later, run ${clc.bold("firebase init dataconnect:sdk")}`,
`If you'd like to add more generated SDKs to your app your later, run ${clc.bold("firebase init dataconnect:sdk")} again`,
);
}

Expand Down Expand Up @@ -190,30 +189,21 @@ async function askQuestions(setup: Setup, config: Config): Promise<SDKInfo> {
newConnectorYaml.generate.kotlinSdk = kotlinSdk;
}

const shouldGenerate = !!(
setup.projectId &&
(await confirm({
message: "Would you like to generate SDK code now?",
default: true,
}))
);
// TODO: Prompt user about adding generated paths to .gitignore
const connectorYamlContents = yaml.stringify(newConnectorYaml);
connectorInfo.connectorYaml = newConnectorYaml;
return { connectorYamlContents, connectorInfo, shouldGenerate, displayIOSWarning };
return { connectorYamlContents, connectorInfo, displayIOSWarning };
}

export async function actuate(sdkInfo: SDKInfo, projectId?: string) {
export async function actuate(sdkInfo: SDKInfo) {
const connectorYamlPath = `${sdkInfo.connectorInfo.directory}/connector.yaml`;
fs.writeFileSync(connectorYamlPath, sdkInfo.connectorYamlContents, "utf8");
logBullet(`Wrote new config to ${connectorYamlPath}`);
if (projectId && sdkInfo.shouldGenerate) {
await DataConnectEmulator.generate({
configDir: sdkInfo.connectorInfo.directory,
connectorId: sdkInfo.connectorInfo.connectorYaml.connectorId,
});
logBullet(`Generated SDK code for ${sdkInfo.connectorInfo.connectorYaml.connectorId}`);
}
await DataConnectEmulator.generate({
configDir: sdkInfo.connectorInfo.directory,
connectorId: sdkInfo.connectorInfo.connectorYaml.connectorId,
});
logBullet(`Generated SDK code for ${sdkInfo.connectorInfo.connectorYaml.connectorId}`);
if (sdkInfo.connectorInfo.connectorYaml.generate?.swiftSdk && sdkInfo.displayIOSWarning) {
logBullet(
clc.bold(
Expand Down

0 comments on commit 36ce505

Please sign in to comment.