firebase-tools-with-isolate 14.10.1 → 14.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/lib/api.js +5 -1
  2. package/lib/apphosting/backend.js +40 -15
  3. package/lib/apptesting/ensureProjectConfigured.js +56 -0
  4. package/lib/apptesting/invokeTests.js +40 -0
  5. package/lib/apptesting/parseTestFiles.js +75 -0
  6. package/lib/apptesting/types.js +18 -0
  7. package/lib/auth.js +37 -2
  8. package/lib/command.js +24 -5
  9. package/lib/commands/apphosting-backends-create.js +8 -5
  10. package/lib/commands/apptesting-execute.js +101 -0
  11. package/lib/commands/dataconnect-sdk-generate.js +28 -26
  12. package/lib/commands/dataconnect-services-list.js +1 -1
  13. package/lib/commands/dataconnect-sql-diff.js +2 -2
  14. package/lib/commands/dataconnect-sql-grant.js +7 -2
  15. package/lib/commands/dataconnect-sql-migrate.js +2 -2
  16. package/lib/commands/dataconnect-sql-setup.js +3 -5
  17. package/lib/commands/dataconnect-sql-shell.js +2 -2
  18. package/lib/commands/ext-export.js +1 -1
  19. package/lib/commands/firestore-bulkdelete.js +73 -0
  20. package/lib/commands/firestore-databases-create.js +11 -0
  21. package/lib/commands/firestore-operations-cancel.js +44 -0
  22. package/lib/commands/firestore-operations-describe.js +29 -0
  23. package/lib/commands/firestore-operations-list.js +29 -0
  24. package/lib/commands/firestore-utils.js +15 -0
  25. package/lib/commands/functions-config-clone.js +2 -0
  26. package/lib/commands/functions-config-export.js +5 -2
  27. package/lib/commands/functions-config-get.js +2 -0
  28. package/lib/commands/functions-config-set.js +2 -0
  29. package/lib/commands/functions-config-unset.js +2 -0
  30. package/lib/commands/index.js +9 -0
  31. package/lib/commands/init.js +24 -4
  32. package/lib/commands/internaltesting-functions-discover.js +20 -5
  33. package/lib/commands/login.js +13 -9
  34. package/lib/commands/use.js +5 -0
  35. package/lib/config.js +16 -4
  36. package/lib/crashlytics/addNote.js +27 -0
  37. package/lib/crashlytics/buildToolsJarHelper.js +1 -2
  38. package/lib/crashlytics/deleteNote.js +23 -0
  39. package/lib/crashlytics/getIssueDetails.js +26 -0
  40. package/lib/crashlytics/getSampleCrash.js +34 -0
  41. package/lib/crashlytics/listNotes.js +29 -0
  42. package/lib/crashlytics/listTopDevices.js +33 -0
  43. package/lib/crashlytics/listTopIssues.js +8 -23
  44. package/lib/crashlytics/listTopOperatingSystems.js +32 -0
  45. package/lib/crashlytics/listTopVersions.js +32 -0
  46. package/lib/crashlytics/updateIssue.js +35 -0
  47. package/lib/crashlytics/utils.js +38 -0
  48. package/lib/dataconnect/appFinder.js +102 -0
  49. package/lib/dataconnect/client.js +23 -15
  50. package/lib/dataconnect/ensureApis.js +6 -9
  51. package/lib/dataconnect/errors.js +7 -1
  52. package/lib/dataconnect/freeTrial.js +16 -38
  53. package/lib/dataconnect/load.js +105 -6
  54. package/lib/dataconnect/provisionCloudSql.js +67 -70
  55. package/lib/dataconnect/schemaMigration.js +219 -169
  56. package/lib/deploy/dataconnect/deploy.js +7 -9
  57. package/lib/deploy/dataconnect/prepare.js +7 -12
  58. package/lib/deploy/dataconnect/release.js +43 -31
  59. package/lib/deploy/firestore/deploy.js +10 -0
  60. package/lib/deploy/functions/backend.js +7 -1
  61. package/lib/deploy/functions/build.js +28 -14
  62. package/lib/deploy/functions/deploy.js +3 -2
  63. package/lib/deploy/functions/ensure.js +1 -1
  64. package/lib/deploy/functions/functionsDeployHelper.js +8 -1
  65. package/lib/deploy/functions/params.js +4 -0
  66. package/lib/deploy/functions/prepare.js +25 -13
  67. package/lib/deploy/functions/pricing.js +12 -5
  68. package/lib/deploy/functions/release/fabricator.js +25 -3
  69. package/lib/deploy/functions/release/reporter.js +1 -0
  70. package/lib/deploy/functions/runtimes/discovery/index.js +52 -5
  71. package/lib/deploy/functions/runtimes/node/index.js +83 -51
  72. package/lib/emulator/auth/operations.js +10 -1
  73. package/lib/emulator/commandUtils.js +7 -1
  74. package/lib/emulator/controller.js +22 -34
  75. package/lib/emulator/dataconnectEmulator.js +27 -24
  76. package/lib/emulator/downloadableEmulatorInfo.json +18 -18
  77. package/lib/emulator/env.js +2 -1
  78. package/lib/emulator/functionsEmulator.js +12 -2
  79. package/lib/emulator/hub.js +11 -8
  80. package/lib/emulator/tasksEmulator.js +1 -1
  81. package/lib/emulator/ui.js +3 -1
  82. package/lib/env.js +18 -0
  83. package/lib/experiments.js +20 -0
  84. package/lib/extensions/extensionsHelper.js +4 -15
  85. package/lib/extensions/runtimes/common.js +3 -2
  86. package/lib/extensions/utils.js +1 -12
  87. package/lib/firestore/api-sort.js +96 -3
  88. package/lib/firestore/api-types.js +20 -1
  89. package/lib/firestore/api.js +144 -15
  90. package/lib/firestore/pretty-print.js +35 -1
  91. package/lib/firestore/validator.js +1 -1
  92. package/lib/functional.js +7 -1
  93. package/lib/functions/deprecationWarnings.js +21 -0
  94. package/lib/functions/env.js +31 -16
  95. package/lib/functions/projectConfig.js +87 -8
  96. package/lib/functions/secrets.js +3 -0
  97. package/lib/gcp/cloudfunctions.js +1 -6
  98. package/lib/gcp/cloudfunctionsv2.js +3 -39
  99. package/lib/gcp/cloudscheduler.js +1 -1
  100. package/lib/gcp/cloudsql/cloudsqladmin.js +4 -16
  101. package/lib/gcp/cloudsql/connect.js +4 -3
  102. package/lib/gcp/cloudsql/permissionsSetup.js +23 -16
  103. package/lib/gcp/k8s.js +32 -0
  104. package/lib/gcp/runv2.js +181 -0
  105. package/lib/gemini/fdcExperience.js +5 -3
  106. package/lib/init/features/aitools/claude.js +43 -0
  107. package/lib/init/features/aitools/cursor.js +61 -0
  108. package/lib/init/features/aitools/gemini.js +58 -0
  109. package/lib/init/features/aitools/index.js +28 -0
  110. package/lib/init/features/aitools/promptUpdater.js +111 -0
  111. package/lib/init/features/aitools/studio.js +17 -0
  112. package/lib/init/features/aitools/types.js +2 -0
  113. package/lib/init/features/aitools.js +83 -0
  114. package/lib/init/features/apptesting/index.js +34 -0
  115. package/lib/init/features/dataconnect/create_app.js +53 -0
  116. package/lib/init/features/dataconnect/index.js +280 -186
  117. package/lib/init/features/dataconnect/sdk.js +232 -144
  118. package/lib/init/features/index.js +8 -3
  119. package/lib/init/features/project.js +4 -0
  120. package/lib/init/index.js +18 -2
  121. package/lib/management/apps.js +3 -3
  122. package/lib/management/studio.js +120 -0
  123. package/lib/mcp/errors.js +5 -7
  124. package/lib/mcp/index.js +179 -61
  125. package/lib/mcp/logging-transport.js +23 -0
  126. package/lib/mcp/prompt.js +10 -0
  127. package/lib/mcp/prompts/core/deploy.js +101 -0
  128. package/lib/mcp/prompts/core/index.js +5 -0
  129. package/lib/mcp/prompts/crashlytics/connect.js +114 -0
  130. package/lib/mcp/prompts/crashlytics/index.js +5 -0
  131. package/lib/mcp/prompts/dataconnect/index.js +9 -0
  132. package/lib/mcp/prompts/dataconnect/schema.js +67 -0
  133. package/lib/mcp/prompts/index.js +47 -0
  134. package/lib/mcp/tools/apphosting/fetch_logs.js +22 -22
  135. package/lib/mcp/tools/apphosting/list_backends.js +10 -10
  136. package/lib/mcp/tools/auth/disable_user.js +7 -7
  137. package/lib/mcp/tools/auth/get_user.js +13 -6
  138. package/lib/mcp/tools/auth/index.js +10 -10
  139. package/lib/mcp/tools/auth/list_users.js +6 -6
  140. package/lib/mcp/tools/auth/set_claims.js +7 -7
  141. package/lib/mcp/tools/auth/set_sms_region_policy.js +6 -6
  142. package/lib/mcp/tools/core/consult_assistant.js +13 -8
  143. package/lib/mcp/tools/core/create_android_sha.js +9 -9
  144. package/lib/mcp/tools/core/create_app.js +7 -7
  145. package/lib/mcp/tools/core/create_project.js +13 -13
  146. package/lib/mcp/tools/core/get_admin_sdk_config.js +7 -7
  147. package/lib/mcp/tools/core/get_environment.js +8 -8
  148. package/lib/mcp/tools/core/get_project.js +5 -5
  149. package/lib/mcp/tools/core/get_sdk_config.js +19 -9
  150. package/lib/mcp/tools/core/index.js +28 -24
  151. package/lib/mcp/tools/core/init.js +39 -16
  152. package/lib/mcp/tools/core/list_apps.js +6 -6
  153. package/lib/mcp/tools/core/list_projects.js +6 -6
  154. package/lib/mcp/tools/core/login.js +46 -0
  155. package/lib/mcp/tools/core/logout.js +62 -0
  156. package/lib/mcp/tools/core/update_environment.js +10 -10
  157. package/lib/mcp/tools/crashlytics/add_note.js +32 -0
  158. package/lib/mcp/tools/crashlytics/constants.js +11 -0
  159. package/lib/mcp/tools/crashlytics/delete_note.js +35 -0
  160. package/lib/mcp/tools/crashlytics/get_issue_details.js +31 -0
  161. package/lib/mcp/tools/crashlytics/get_sample_crash.js +43 -0
  162. package/lib/mcp/tools/crashlytics/index.js +22 -2
  163. package/lib/mcp/tools/crashlytics/list_notes.js +37 -0
  164. package/lib/mcp/tools/crashlytics/list_top_devices.js +33 -0
  165. package/lib/mcp/tools/crashlytics/list_top_issues.js +12 -13
  166. package/lib/mcp/tools/crashlytics/list_top_operating_systems.js +33 -0
  167. package/lib/mcp/tools/crashlytics/list_top_versions.js +33 -0
  168. package/lib/mcp/tools/crashlytics/update_issue.js +37 -0
  169. package/lib/mcp/tools/database/get_data.js +49 -0
  170. package/lib/mcp/tools/database/get_rules.js +39 -0
  171. package/lib/mcp/tools/database/index.js +8 -0
  172. package/lib/mcp/tools/database/set_data.js +57 -0
  173. package/lib/mcp/tools/database/validate_rules.js +41 -0
  174. package/lib/mcp/tools/dataconnect/compile.js +43 -0
  175. package/lib/mcp/tools/dataconnect/execute.js +71 -0
  176. package/lib/mcp/tools/dataconnect/generate_operation.js +8 -8
  177. package/lib/mcp/tools/dataconnect/generate_schema.js +6 -6
  178. package/lib/mcp/tools/dataconnect/index.js +10 -18
  179. package/lib/mcp/tools/dataconnect/list_services.js +107 -11
  180. package/lib/mcp/tools/firestore/delete_document.js +13 -13
  181. package/lib/mcp/tools/firestore/get_documents.js +13 -13
  182. package/lib/mcp/tools/firestore/list_collections.js +7 -10
  183. package/lib/mcp/tools/firestore/query_collection.js +13 -13
  184. package/lib/mcp/tools/index.js +24 -18
  185. package/lib/mcp/tools/messaging/index.js +2 -2
  186. package/lib/mcp/tools/messaging/send_message.js +7 -7
  187. package/lib/mcp/tools/remoteconfig/get_template.js +5 -5
  188. package/lib/mcp/tools/remoteconfig/index.js +4 -4
  189. package/lib/mcp/tools/remoteconfig/publish_template.js +7 -7
  190. package/lib/mcp/tools/remoteconfig/rollback_template.js +6 -6
  191. package/lib/mcp/tools/rules/get_rules.js +8 -8
  192. package/lib/mcp/tools/rules/validate_rules.js +10 -10
  193. package/lib/mcp/tools/storage/get_download_url.js +8 -8
  194. package/lib/mcp/tools/storage/get_rules.js +8 -8
  195. package/lib/mcp/types.js +2 -0
  196. package/lib/mcp/util/dataconnect/compile.js +20 -0
  197. package/lib/mcp/util/dataconnect/content.js +655 -0
  198. package/lib/mcp/util/dataconnect/emulator.js +16 -0
  199. package/lib/mcp/util.js +5 -18
  200. package/lib/requireAuth.js +14 -4
  201. package/lib/rtdb.js +10 -6
  202. package/lib/scopes.js +2 -1
  203. package/lib/serve/functions.js +4 -3
  204. package/lib/timeout.js +21 -0
  205. package/lib/track.js +18 -1
  206. package/lib/unzip.js +13 -0
  207. package/lib/utils.js +76 -2
  208. package/package.json +5 -3
  209. package/prompts/FIREBASE.md +121 -0
  210. package/prompts/FIREBASE_FUNCTIONS.md +221 -0
  211. package/schema/apptesting-yaml.json +64 -0
  212. package/schema/firebase-config.json +163 -53
  213. package/templates/init/aitools/cursor-rules-header.txt +8 -0
  214. package/templates/init/aitools/gemini-extension.json +11 -0
  215. package/templates/init/apptesting/smoke_test.yaml +6 -0
  216. package/templates/init/dataconnect/connector.yaml +0 -16
  217. package/templates/init/dataconnect/dataconnect.yaml +2 -1
  218. package/templates/init/dataconnect/mutations.gql +29 -29
  219. package/templates/init/dataconnect/queries.gql +73 -73
  220. package/templates/init/dataconnect/schema.gql +48 -48
  221. package/templates/init/firestore/firestore.indexes.json +26 -1
  222. package/lib/dataconnect/fileUtils.js +0 -168
  223. package/lib/emulator/adminSdkConfig.test.js +0 -16
  224. package/lib/extensions/resolveSource.js +0 -23
  225. package/lib/firestore/api-sort.test.js +0 -74
  226. package/lib/firestore/backupUtils.test.js +0 -18
  227. package/lib/firestore/pretty-print.test.js +0 -61
  228. package/lib/firestore/util.test.js +0 -42
  229. package/lib/mcp/tools/dataconnect/emulator.js +0 -16
  230. package/lib/mcp/tools/dataconnect/execute_graphql.js +0 -48
  231. package/lib/mcp/tools/dataconnect/execute_graphql_read.js +0 -48
  232. package/lib/mcp/tools/dataconnect/execute_mutation.js +0 -62
  233. package/lib/mcp/tools/dataconnect/execute_query.js +0 -62
  234. package/lib/mcp/tools/dataconnect/get_connector.js +0 -31
  235. package/lib/mcp/tools/dataconnect/get_schema.js +0 -31
  236. /package/lib/mcp/{tools → util}/dataconnect/converter.js +0 -0
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toDNSCompatibleId = exports.postSetup = exports.actuate = exports.askQuestions = void 0;
3
+ exports.newUniqueId = exports.toDNSCompatibleId = exports.actuate = exports.askQuestions = void 0;
4
4
  const path_1 = require("path");
5
5
  const clc = require("colorette");
6
6
  const fs = require("fs-extra");
@@ -15,76 +15,69 @@ const names_1 = require("../../../dataconnect/names");
15
15
  const logger_1 = require("../../../logger");
16
16
  const templates_1 = require("../../../templates");
17
17
  const utils_1 = require("../../../utils");
18
+ Object.defineProperty(exports, "newUniqueId", { enumerable: true, get: function () { return utils_1.newUniqueId; } });
18
19
  const cloudbilling_1 = require("../../../gcp/cloudbilling");
19
20
  const sdk = require("./sdk");
20
- const fileUtils_1 = require("../../../dataconnect/fileUtils");
21
21
  const fdcExperience_1 = require("../../../gemini/fdcExperience");
22
22
  const configstore_1 = require("../../../configstore");
23
+ const track_1 = require("../../../track");
23
24
  const DATACONNECT_YAML_TEMPLATE = (0, templates_1.readTemplateSync)("init/dataconnect/dataconnect.yaml");
24
25
  const CONNECTOR_YAML_TEMPLATE = (0, templates_1.readTemplateSync)("init/dataconnect/connector.yaml");
25
26
  const SCHEMA_TEMPLATE = (0, templates_1.readTemplateSync)("init/dataconnect/schema.gql");
26
27
  const QUERIES_TEMPLATE = (0, templates_1.readTemplateSync)("init/dataconnect/queries.gql");
27
28
  const MUTATIONS_TEMPLATE = (0, templates_1.readTemplateSync)("init/dataconnect/mutations.gql");
28
- const emptyConnector = {
29
- id: "default",
30
- path: "./connector",
31
- files: [],
32
- };
33
- const defaultConnector = {
34
- id: "default",
35
- path: "./connector",
36
- files: [
37
- {
38
- path: "queries.gql",
39
- content: QUERIES_TEMPLATE,
40
- },
29
+ const templateServiceInfo = {
30
+ schemaGql: [{ path: "schema.gql", content: SCHEMA_TEMPLATE }],
31
+ connectors: [
41
32
  {
42
- path: "mutations.gql",
43
- content: MUTATIONS_TEMPLATE,
33
+ id: "example",
34
+ path: "./example",
35
+ files: [
36
+ {
37
+ path: "queries.gql",
38
+ content: QUERIES_TEMPLATE,
39
+ },
40
+ {
41
+ path: "mutations.gql",
42
+ content: MUTATIONS_TEMPLATE,
43
+ },
44
+ ],
44
45
  },
45
46
  ],
46
47
  };
47
- const defaultSchema = { path: "schema.gql", content: SCHEMA_TEMPLATE };
48
48
  async function askQuestions(setup) {
49
- const hasBilling = await (0, cloudbilling_1.isBillingEnabled)(setup);
50
- if (setup.projectId) {
51
- hasBilling ? await (0, ensureApis_1.ensureApis)(setup.projectId) : await (0, ensureApis_1.ensureSparkApis)(setup.projectId);
52
- }
53
- let info = {
49
+ const info = {
50
+ analyticsFlow: "cli",
51
+ appDescription: "",
54
52
  serviceId: "",
55
53
  locationId: "",
56
54
  cloudSqlInstanceId: "",
57
- isNewInstance: false,
58
55
  cloudSqlDatabase: "",
59
- isNewDatabase: false,
60
- connectors: [],
61
- schemaGql: [],
62
56
  shouldProvisionCSQL: false,
63
57
  };
64
- info = await promptForExistingServices(setup, info);
65
- const requiredConfigUnset = info.serviceId === "" ||
66
- info.cloudSqlInstanceId === "" ||
67
- info.locationId === "" ||
68
- info.cloudSqlDatabase === "";
69
- const shouldConfigureBackend = hasBilling &&
70
- requiredConfigUnset &&
71
- (await (0, prompt_1.confirm)({
72
- message: `Would you like to configure your Cloud SQL datasource now?`,
73
- default: true,
74
- }));
75
- if (shouldConfigureBackend) {
76
- info = await promptForSchema(setup, info);
77
- info = await promptForCloudSQL(setup, info);
78
- info.shouldProvisionCSQL = !!(setup.projectId &&
79
- (info.isNewInstance || info.isNewDatabase) &&
80
- hasBilling &&
81
- (await (0, prompt_1.confirm)({
82
- message: `Would you like to provision your Cloud SQL instance and database now?${info.isNewInstance ? " This will take several minutes." : ""}.`,
83
- default: true,
84
- })));
58
+ if (setup.projectId) {
59
+ const hasBilling = await (0, cloudbilling_1.isBillingEnabled)(setup);
60
+ await (0, ensureApis_1.ensureApis)(setup.projectId);
61
+ await promptForExistingServices(setup, info);
62
+ if (!info.serviceGql) {
63
+ if (!configstore_1.configstore.get("gemini")) {
64
+ (0, utils_1.logBullet)("Learn more about Gemini in Firebase and how it uses your data: https://firebase.google.com/docs/gemini-in-firebase#how-gemini-in-firebase-uses-your-data");
65
+ }
66
+ info.appDescription = await (0, prompt_1.input)({
67
+ message: `Describe your app to automatically generate a schema with Gemini [Enter to skip]:`,
68
+ });
69
+ if (info.appDescription) {
70
+ configstore_1.configstore.set("gemini", true);
71
+ await (0, ensureApis_1.ensureGIFApis)(setup.projectId);
72
+ }
73
+ }
74
+ if (hasBilling) {
75
+ await promptForCloudSQL(setup, info);
76
+ }
85
77
  }
86
78
  setup.featureInfo = setup.featureInfo || {};
87
79
  setup.featureInfo.dataconnect = info;
80
+ await sdk.askQuestions(setup);
88
81
  }
89
82
  exports.askQuestions = askQuestions;
90
83
  async function actuate(setup, config, options) {
@@ -95,68 +88,182 @@ async function actuate(setup, config, options) {
95
88
  if (!info) {
96
89
  throw new Error("Data Connect feature RequiredInfo is not provided");
97
90
  }
98
- const defaultServiceId = toDNSCompatibleId((0, path_1.basename)(process.cwd()));
99
- info.serviceId = info.serviceId || defaultServiceId;
100
- info.cloudSqlInstanceId =
101
- info.cloudSqlInstanceId || `${info.serviceId.toLowerCase() || "app"}-fdc`;
91
+ info.serviceId = info.serviceId || defaultServiceId();
92
+ info.cloudSqlInstanceId = info.cloudSqlInstanceId || `${info.serviceId.toLowerCase()}-fdc`;
102
93
  info.locationId = info.locationId || `us-central1`;
103
94
  info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
104
- if (!info.schemaGql.length && !info.connectors.flatMap((r) => r.files).length) {
105
- info.schemaGql = [defaultSchema];
106
- info.connectors = [defaultConnector];
107
- }
108
- await writeFiles(config, info, options);
109
- if (setup.projectId && info.shouldProvisionCSQL) {
110
- await (0, provisionCloudSql_1.provisionCloudSql)({
111
- projectId: setup.projectId,
95
+ try {
96
+ await actuateWithInfo(setup, config, info, options);
97
+ await sdk.actuate(setup, config);
98
+ }
99
+ finally {
100
+ void (0, track_1.trackGA4)("dataconnect_init", {
101
+ project_status: setup.projectId ? (setup.isBillingEnabled ? "blaze" : "spark") : "missing",
102
+ flow: info.analyticsFlow,
103
+ provision_cloud_sql: String(info.shouldProvisionCSQL),
104
+ });
105
+ }
106
+ if (info.appDescription) {
107
+ setup.instructions.push(`You can visualize the Data Connect Schema in Firebase Console:
108
+
109
+ https://console.firebase.google.com/project/${setup.projectId}/dataconnect/locations/${info.locationId}/services/${info.serviceId}/schema`);
110
+ }
111
+ if (!setup.isBillingEnabled) {
112
+ setup.instructions.push((0, freeTrial_1.upgradeInstructions)(setup.projectId || "your-firebase-project"));
113
+ }
114
+ setup.instructions.push(`Install the Data Connect VS Code Extensions. You can explore Data Connect Query on local pgLite and Cloud SQL Postgres Instance.`);
115
+ }
116
+ exports.actuate = actuate;
117
+ async function actuateWithInfo(setup, config, info, options) {
118
+ const projectId = setup.projectId;
119
+ if (!projectId) {
120
+ info.analyticsFlow += "_save_template";
121
+ return await writeFiles(config, info, templateServiceInfo, options);
122
+ }
123
+ await (0, ensureApis_1.ensureApis)(projectId, true);
124
+ const provisionCSQL = info.shouldProvisionCSQL && (await (0, cloudbilling_1.isBillingEnabled)(setup));
125
+ if (provisionCSQL) {
126
+ await (0, provisionCloudSql_1.setupCloudSql)({
127
+ projectId: projectId,
112
128
  location: info.locationId,
113
129
  instanceId: info.cloudSqlInstanceId,
114
130
  databaseId: info.cloudSqlDatabase,
115
- enableGoogleMlIntegration: false,
116
- waitForCreation: false,
131
+ requireGoogleMlIntegration: false,
117
132
  });
118
133
  }
119
- }
120
- exports.actuate = actuate;
121
- async function postSetup(setup, config) {
122
- const cwdPlatformGuess = await (0, fileUtils_1.getPlatformFromFolder)(process.cwd());
123
- if (cwdPlatformGuess !== types_1.Platform.NONE || (0, utils_1.envOverride)("FDC_CONNECTOR", "")) {
124
- await sdk.doSetup(setup, config);
134
+ const serviceName = `projects/${projectId}/locations/${info.locationId}/services/${info.serviceId}`;
135
+ if (!info.appDescription) {
136
+ if (!info.serviceGql) {
137
+ await downloadService(info, serviceName);
138
+ }
139
+ if (info.serviceGql) {
140
+ info.analyticsFlow += "_save_downloaded";
141
+ return await writeFiles(config, info, info.serviceGql, options);
142
+ }
143
+ info.analyticsFlow += "_save_template";
144
+ return await writeFiles(config, info, templateServiceInfo, options);
125
145
  }
126
- else {
127
- (0, utils_1.logBullet)(`If you'd like to add the generated SDK to your app later, run ${clc.bold("firebase init dataconnect:sdk")}`);
146
+ const serviceAlreadyExists = !(await (0, client_1.createService)(projectId, info.locationId, info.serviceId));
147
+ const schemaGql = await (0, utils_1.promiseWithSpinner)(() => (0, fdcExperience_1.generateSchema)(info.appDescription, projectId), "Generating the Data Connect Schema...");
148
+ const schemaFiles = [{ path: "schema.gql", content: schemaGql }];
149
+ if (serviceAlreadyExists) {
150
+ (0, utils_1.logLabeledError)("dataconnect", `Data Connect Service ${serviceName} already exists. Skip saving them...`);
151
+ info.analyticsFlow += "_save_gemini_service_already_exists";
152
+ return await writeFiles(config, info, { schemaGql: schemaFiles, connectors: [] }, options);
153
+ }
154
+ await (0, utils_1.promiseWithSpinner)(async () => {
155
+ const [saveSchemaGql, waitForCloudSQLProvision] = schemasDeploySequence(projectId, info, schemaFiles, provisionCSQL);
156
+ await (0, client_1.upsertSchema)(saveSchemaGql);
157
+ if (waitForCloudSQLProvision) {
158
+ void (0, client_1.upsertSchema)(waitForCloudSQLProvision);
159
+ }
160
+ }, "Saving the Data Connect Schema...");
161
+ try {
162
+ const [operationGql, seedDataGql] = await (0, utils_1.promiseWithSpinner)(() => Promise.all([
163
+ (0, fdcExperience_1.generateOperation)(fdcExperience_1.PROMPT_GENERATE_CONNECTOR, serviceName, projectId),
164
+ (0, fdcExperience_1.generateOperation)(fdcExperience_1.PROMPT_GENERATE_SEED_DATA, serviceName, projectId),
165
+ ]), "Generating the Data Connect Operations...");
166
+ const connectors = [
167
+ {
168
+ id: "example",
169
+ path: "./example",
170
+ files: [
171
+ {
172
+ path: "queries.gql",
173
+ content: operationGql,
174
+ },
175
+ ],
176
+ },
177
+ ];
178
+ info.analyticsFlow += "_save_gemini";
179
+ await writeFiles(config, info, { schemaGql: schemaFiles, connectors: connectors, seedDataGql: seedDataGql }, options);
180
+ }
181
+ catch (err) {
182
+ (0, utils_1.logLabeledError)("dataconnect", `Operation Generation failed...`);
183
+ info.analyticsFlow += "_save_gemini_operation_error";
184
+ await writeFiles(config, info, { schemaGql: schemaFiles, connectors: [] }, options);
185
+ throw err;
128
186
  }
129
- if (setup.projectId && !setup.isBillingEnabled) {
130
- (0, utils_1.logBullet)((0, freeTrial_1.upgradeInstructions)(setup.projectId));
187
+ }
188
+ function schemasDeploySequence(projectId, info, schemaFiles, linkToCloudSql) {
189
+ const serviceName = `projects/${projectId}/locations/${info.locationId}/services/${info.serviceId}`;
190
+ if (!linkToCloudSql) {
191
+ return [
192
+ {
193
+ name: `${serviceName}/schemas/${types_1.SCHEMA_ID}`,
194
+ datasources: [{ postgresql: {} }],
195
+ source: {
196
+ files: schemaFiles,
197
+ },
198
+ },
199
+ ];
131
200
  }
201
+ return [
202
+ {
203
+ name: `${serviceName}/schemas/${types_1.SCHEMA_ID}`,
204
+ datasources: [
205
+ {
206
+ postgresql: {
207
+ database: info.cloudSqlDatabase,
208
+ cloudSql: {
209
+ instance: `projects/${projectId}/locations/${info.locationId}/instances/${info.cloudSqlInstanceId}`,
210
+ },
211
+ schemaValidation: "NONE",
212
+ },
213
+ },
214
+ ],
215
+ source: {
216
+ files: schemaFiles,
217
+ },
218
+ },
219
+ {
220
+ name: `${serviceName}/schemas/${types_1.SCHEMA_ID}`,
221
+ datasources: [
222
+ {
223
+ postgresql: {
224
+ database: info.cloudSqlDatabase,
225
+ cloudSql: {
226
+ instance: `projects/${projectId}/locations/${info.locationId}/instances/${info.cloudSqlInstanceId}`,
227
+ },
228
+ schemaMigration: "MIGRATE_COMPATIBLE",
229
+ },
230
+ },
231
+ ],
232
+ source: {
233
+ files: schemaFiles,
234
+ },
235
+ },
236
+ ];
132
237
  }
133
- exports.postSetup = postSetup;
134
- async function writeFiles(config, info, options) {
238
+ async function writeFiles(config, info, serviceGql, options) {
135
239
  const dir = config.get("dataconnect.source") || "dataconnect";
136
240
  const subbedDataconnectYaml = subDataconnectYamlValues({
137
241
  ...info,
138
- connectorDirs: info.connectors.map((c) => c.path),
242
+ connectorDirs: serviceGql.connectors.map((c) => c.path),
139
243
  });
140
244
  config.set("dataconnect", { source: dir });
141
245
  await config.askWriteProjectFile((0, path_1.join)(dir, "dataconnect.yaml"), subbedDataconnectYaml, !!options.force, true);
142
- if (info.schemaGql.length) {
143
- for (const f of info.schemaGql) {
246
+ if (serviceGql.seedDataGql) {
247
+ await config.askWriteProjectFile((0, path_1.join)(dir, "seed_data.gql"), serviceGql.seedDataGql, !!options.force);
248
+ }
249
+ if (serviceGql.schemaGql.length) {
250
+ for (const f of serviceGql.schemaGql) {
144
251
  await config.askWriteProjectFile((0, path_1.join)(dir, "schema", f.path), f.content, !!options.force);
145
252
  }
146
253
  }
147
254
  else {
148
255
  fs.ensureFileSync((0, path_1.join)(dir, "schema", "schema.gql"));
149
256
  }
150
- for (const c of info.connectors) {
151
- await writeConnectorFiles(config, c);
257
+ for (const c of serviceGql.connectors) {
258
+ await writeConnectorFiles(config, c, options);
152
259
  }
153
260
  }
154
- async function writeConnectorFiles(config, connectorInfo) {
261
+ async function writeConnectorFiles(config, connectorInfo, options) {
155
262
  const subbedConnectorYaml = subConnectorYamlValues({ connectorId: connectorInfo.id });
156
263
  const dir = config.get("dataconnect.source") || "dataconnect";
157
- await config.askWriteProjectFile((0, path_1.join)(dir, connectorInfo.path, "connector.yaml"), subbedConnectorYaml);
264
+ await config.askWriteProjectFile((0, path_1.join)(dir, connectorInfo.path, "connector.yaml"), subbedConnectorYaml, !!options.force, true);
158
265
  for (const f of connectorInfo.files) {
159
- await config.askWriteProjectFile((0, path_1.join)(dir, connectorInfo.path, f.path), f.content);
266
+ await config.askWriteProjectFile((0, path_1.join)(dir, connectorInfo.path, f.path), f.content, !!options.force);
160
267
  }
161
268
  }
162
269
  function subDataconnectYamlValues(replacementValues) {
@@ -185,55 +292,72 @@ function subConnectorYamlValues(replacementValues) {
185
292
  }
186
293
  async function promptForExistingServices(setup, info) {
187
294
  if (!setup.projectId) {
188
- return info;
295
+ return;
189
296
  }
190
297
  const existingServices = await (0, client_1.listAllServices)(setup.projectId);
191
- const existingServicesAndSchemas = await Promise.all(existingServices.map(async (s) => {
192
- return { service: s, schema: await (0, client_1.getSchema)(s.name) };
193
- }));
194
- if (existingServicesAndSchemas.length) {
195
- const choice = await chooseExistingService(existingServicesAndSchemas);
196
- if (choice) {
197
- const serviceName = (0, names_1.parseServiceName)(choice.service.name);
198
- info.serviceId = serviceName.serviceId;
199
- info.locationId = serviceName.location;
200
- info.schemaGql = [];
201
- info.connectors = [emptyConnector];
202
- if (choice.schema) {
203
- const primaryDatasource = choice.schema.datasources.find((d) => d.postgresql);
204
- if (primaryDatasource?.postgresql?.cloudSql.instance) {
205
- const instanceName = (0, names_1.parseCloudSQLInstanceName)(primaryDatasource.postgresql.cloudSql.instance);
206
- info.cloudSqlInstanceId = instanceName.instanceId;
207
- }
208
- if (choice.schema.source.files?.length) {
209
- info.schemaGql = choice.schema.source.files;
210
- }
211
- info.cloudSqlDatabase = primaryDatasource?.postgresql?.database ?? "";
212
- const connectors = await (0, client_1.listConnectors)(choice.service.name, [
213
- "connectors.name",
214
- "connectors.source.files",
215
- ]);
216
- if (connectors.length) {
217
- info.connectors = connectors.map((c) => {
218
- const id = c.name.split("/").pop();
219
- return {
220
- id,
221
- path: connectors.length === 1 ? "./connector" : `./${id}`,
222
- files: c.source.files || [],
223
- };
224
- });
225
- }
226
- }
227
- }
298
+ if (!existingServices.length) {
299
+ return;
300
+ }
301
+ const choice = await chooseExistingService(existingServices);
302
+ if (!choice) {
303
+ const existingServiceIds = existingServices.map((s) => s.name.split("/").pop());
304
+ info.serviceId = (0, utils_1.newUniqueId)(defaultServiceId(), existingServiceIds);
305
+ info.analyticsFlow += "_pick_new_service";
306
+ return;
307
+ }
308
+ info.analyticsFlow += "_pick_existing_service";
309
+ const serviceName = (0, names_1.parseServiceName)(choice.name);
310
+ info.serviceId = serviceName.serviceId;
311
+ info.locationId = serviceName.location;
312
+ await downloadService(info, choice.name);
313
+ }
314
+ async function downloadService(info, serviceName) {
315
+ const schema = await (0, client_1.getSchema)(serviceName);
316
+ if (!schema) {
317
+ return;
318
+ }
319
+ info.serviceGql = {
320
+ schemaGql: [],
321
+ connectors: [
322
+ {
323
+ id: "example",
324
+ path: "./example",
325
+ files: [],
326
+ },
327
+ ],
328
+ };
329
+ const primaryDatasource = schema.datasources.find((d) => d.postgresql);
330
+ if (primaryDatasource?.postgresql?.cloudSql?.instance) {
331
+ const instanceName = (0, names_1.parseCloudSQLInstanceName)(primaryDatasource.postgresql.cloudSql.instance);
332
+ info.cloudSqlInstanceId = instanceName.instanceId;
333
+ }
334
+ if (schema.source.files?.length) {
335
+ info.serviceGql.schemaGql = schema.source.files;
336
+ }
337
+ info.cloudSqlDatabase = primaryDatasource?.postgresql?.database ?? "";
338
+ const connectors = await (0, client_1.listConnectors)(serviceName, [
339
+ "connectors.name",
340
+ "connectors.source.files",
341
+ ]);
342
+ if (connectors.length) {
343
+ info.serviceGql.connectors = connectors.map((c) => {
344
+ const id = c.name.split("/").pop();
345
+ return {
346
+ id,
347
+ path: connectors.length === 1 ? "./example" : `./${id}`,
348
+ files: c.source.files || [],
349
+ };
350
+ });
228
351
  }
229
- return info;
230
352
  }
231
353
  async function chooseExistingService(existing) {
232
- const serviceEnvVar = (0, utils_1.envOverride)("FDC_CONNECTOR", "") || (0, utils_1.envOverride)("FDC_SERVICE", "");
354
+ const fdcConnector = (0, utils_1.envOverride)("FDC_CONNECTOR", "");
355
+ const fdcService = (0, utils_1.envOverride)("FDC_SERVICE", "");
356
+ const serviceEnvVar = fdcConnector || fdcService;
233
357
  if (serviceEnvVar) {
234
358
  const [serviceLocationFromEnvVar, serviceIdFromEnvVar] = serviceEnvVar.split("/");
235
359
  const serviceFromEnvVar = existing.find((s) => {
236
- const serviceName = (0, names_1.parseServiceName)(s.service.name);
360
+ const serviceName = (0, names_1.parseServiceName)(s.name);
237
361
  return (serviceName.serviceId === serviceIdFromEnvVar &&
238
362
  serviceName.location === serviceLocationFromEnvVar);
239
363
  });
@@ -241,10 +365,11 @@ async function chooseExistingService(existing) {
241
365
  (0, utils_1.logBullet)(`Picking up the existing service ${clc.bold(serviceLocationFromEnvVar + "/" + serviceIdFromEnvVar)}.`);
242
366
  return serviceFromEnvVar;
243
367
  }
244
- (0, utils_1.logWarning)(`Unable to pick up an existing service based on FDC_SERVICE=${serviceEnvVar}.`);
368
+ const envVarName = fdcConnector ? "FDC_CONNECTOR" : "FDC_SERVICE";
369
+ (0, utils_1.logWarning)(`Unable to pick up an existing service based on ${envVarName}=${serviceEnvVar}.`);
245
370
  }
246
371
  const choices = existing.map((s) => {
247
- const serviceName = (0, names_1.parseServiceName)(s.service.name);
372
+ const serviceName = (0, names_1.parseServiceName)(s.name);
248
373
  return {
249
374
  name: `${serviceName.location}/${serviceName.serviceId}`,
250
375
  value: s,
@@ -257,7 +382,10 @@ async function chooseExistingService(existing) {
257
382
  });
258
383
  }
259
384
  async function promptForCloudSQL(setup, info) {
260
- if (info.cloudSqlInstanceId === "" && setup.projectId) {
385
+ if (!setup.projectId) {
386
+ return;
387
+ }
388
+ if (info.cloudSqlInstanceId === "") {
261
389
  const instances = await cloudsql.listInstances(setup.projectId);
262
390
  let choices = instances.map((i) => {
263
391
  let display = `${i.name} (${i.region})`;
@@ -279,78 +407,41 @@ async function promptForCloudSQL(setup, info) {
279
407
  choices,
280
408
  });
281
409
  if (info.cloudSqlInstanceId !== "") {
410
+ info.analyticsFlow += "_pick_existing_csql";
282
411
  info.locationId = choices.find((c) => c.value === info.cloudSqlInstanceId).location;
283
412
  }
413
+ else {
414
+ info.analyticsFlow += "_pick_new_csql";
415
+ info.cloudSqlInstanceId = await (0, prompt_1.input)({
416
+ message: `What ID would you like to use for your new CloudSQL instance?`,
417
+ default: (0, utils_1.newUniqueId)(`${defaultServiceId().toLowerCase()}-fdc`, instances.map((i) => i.name)),
418
+ });
419
+ }
284
420
  }
285
421
  }
286
- if (info.cloudSqlInstanceId === "") {
287
- info.isNewInstance = true;
288
- info.cloudSqlInstanceId = await (0, prompt_1.input)({
289
- message: `What ID would you like to use for your new CloudSQL instance?`,
290
- default: `${info.serviceId.toLowerCase() || "app"}-fdc`,
291
- });
292
- }
293
422
  if (info.locationId === "") {
294
423
  const choices = await locationChoices(setup);
295
424
  info.locationId = await (0, prompt_1.select)({
296
425
  message: "What location would like to use?",
297
426
  choices,
427
+ default: "us-central1",
428
+ });
429
+ info.shouldProvisionCSQL = await (0, prompt_1.confirm)({
430
+ message: `Would you like to provision your Cloud SQL instance and database now?`,
431
+ default: true,
298
432
  });
299
433
  }
300
- if (info.cloudSqlDatabase === "" && setup.projectId) {
434
+ if (info.cloudSqlInstanceId !== "" && info.cloudSqlDatabase === "") {
301
435
  try {
302
436
  const dbs = await cloudsql.listDatabases(setup.projectId, info.cloudSqlInstanceId);
303
- const choices = dbs.map((d) => {
304
- return { name: d.name, value: d.name };
305
- });
306
- choices.push({ name: "Create a new database", value: "" });
307
- if (dbs.length) {
308
- info.cloudSqlDatabase = await (0, prompt_1.select)({
309
- message: `Which database in ${info.cloudSqlInstanceId} would you like to use?`,
310
- choices,
311
- });
312
- }
437
+ const existing = dbs.map((d) => d.name);
438
+ info.cloudSqlDatabase = (0, utils_1.newUniqueId)("fdcdb", existing);
313
439
  }
314
440
  catch (err) {
315
441
  logger_1.logger.debug(`[dataconnect] Cannot list databases during init: ${err}`);
316
442
  }
317
443
  }
318
- if (info.cloudSqlDatabase === "") {
319
- info.isNewDatabase = true;
320
- info.cloudSqlDatabase = await (0, prompt_1.input)({
321
- message: `What ID would you like to use for your new database in ${info.cloudSqlInstanceId}?`,
322
- default: `fdcdb`,
323
- });
324
- }
325
- return info;
326
- }
327
- async function promptForSchema(setup, info) {
328
- if (info.serviceId === "") {
329
- info.serviceId = await (0, prompt_1.input)({
330
- message: "What ID would you like to use for this service?",
331
- default: (0, path_1.basename)(process.cwd()),
332
- });
333
- if (setup.projectId) {
334
- if (!configstore_1.configstore.get("gemini")) {
335
- (0, utils_1.logBullet)("Learn more about Gemini in Firebase and how it uses your data: https://firebase.google.com/docs/gemini-in-firebase#how-gemini-in-firebase-uses-your-data");
336
- }
337
- if (await (0, prompt_1.confirm)({
338
- message: `Do you want Gemini in Firebase to help generate a schema for your service?`,
339
- default: false,
340
- })) {
341
- configstore_1.configstore.set("gemini", true);
342
- await (0, ensureApis_1.ensureGIFApis)(setup.projectId);
343
- const prompt = await (0, prompt_1.input)({
344
- message: "Describe the app you are building:",
345
- default: "movie rating app",
346
- });
347
- const schema = await (0, utils_1.promiseWithSpinner)(() => (0, fdcExperience_1.generateSchema)(prompt, setup.projectId), "Generating the Data Connect Schema...");
348
- info.schemaGql = [{ path: "schema.gql", content: (0, fdcExperience_1.extractCodeBlock)(schema) }];
349
- info.connectors = [emptyConnector];
350
- }
351
- }
352
- }
353
- return info;
444
+ return;
354
445
  }
355
446
  async function locationChoices(setup) {
356
447
  if (setup.projectId) {
@@ -372,17 +463,20 @@ async function locationChoices(setup) {
372
463
  ];
373
464
  }
374
465
  }
466
+ function defaultServiceId() {
467
+ return toDNSCompatibleId((0, path_1.basename)(process.cwd()));
468
+ }
375
469
  function toDNSCompatibleId(id) {
376
- let defaultServiceId = (0, path_1.basename)(id)
470
+ id = (0, path_1.basename)(id)
377
471
  .toLowerCase()
378
472
  .replaceAll(/[^a-z0-9-]/g, "")
379
473
  .slice(0, 63);
380
- while (defaultServiceId.endsWith("-") && defaultServiceId.length) {
381
- defaultServiceId = defaultServiceId.slice(0, defaultServiceId.length - 1);
474
+ while (id.endsWith("-") && id.length) {
475
+ id = id.slice(0, id.length - 1);
382
476
  }
383
- while (defaultServiceId.startsWith("-") && defaultServiceId.length) {
384
- defaultServiceId = defaultServiceId.slice(1, defaultServiceId.length);
477
+ while (id.startsWith("-") && id.length) {
478
+ id = id.slice(1, id.length);
385
479
  }
386
- return defaultServiceId || "app";
480
+ return id || "app";
387
481
  }
388
482
  exports.toDNSCompatibleId = toDNSCompatibleId;