@@ -19,7 +19,7 @@ import { Schema, Service, File, Platform } from "../../../dataconnect/types";
1919import { parseCloudSQLInstanceName , parseServiceName } from "../../../dataconnect/names" ;
2020import { logger } from "../../../logger" ;
2121import { readTemplateSync } from "../../../templates" ;
22- import { logBullet , envOverride } from "../../../utils" ;
22+ import { logBullet , logWarning , envOverride } from "../../../utils" ;
2323import { isBillingEnabled } from "../../../gcp/cloudbilling" ;
2424import * as sdk from "./sdk" ;
2525import { getPlatformFromFolder } from "../../../dataconnect/fileUtils" ;
@@ -30,11 +30,6 @@ const SCHEMA_TEMPLATE = readTemplateSync("init/dataconnect/schema.gql");
3030const QUERIES_TEMPLATE = readTemplateSync ( "init/dataconnect/queries.gql" ) ;
3131const MUTATIONS_TEMPLATE = readTemplateSync ( "init/dataconnect/mutations.gql" ) ;
3232
33- // serviceEnvVar is used by Firebase Console to specify which service to import.
34- // It should be in the form <location>/<serviceId>
35- // It must be an existing service - if set to anything else, we'll ignore it.
36- const serviceEnvVar = ( ) => envOverride ( "FDC_CONNECTOR" , "" ) || envOverride ( "FDC_SERVICE" , "" ) ;
37-
3833export interface RequiredInfo {
3934 serviceId : string ;
4035 locationId : string ;
@@ -318,23 +313,37 @@ interface serviceAndSchema {
318313 schema ?: Schema ;
319314}
320315
316+ /**
317+ * Picks create new service or an existing service from the list of services.
318+ *
319+ * Firebase Console can provide `FDC_CONNECTOR` or `FDC_SERVICE` environment variable.
320+ * If either is present, chooseExistingService try to match it with any existing service
321+ * and short-circuit the prompt.
322+ *
323+ * `FDC_SERVICE` should have the format `<location>/<serviceId>`.
324+ * `FDC_CONNECTOR` should have the same `<location>/<serviceId>/<connectorId>`.
325+ * @param existing
326+ */
321327async function chooseExistingService (
322328 existing : serviceAndSchema [ ] ,
323329) : Promise < serviceAndSchema | undefined > {
324- const [ serviceLocationFromEnvVar , serviceIdFromEnvVar ] = serviceEnvVar ( ) . split ( "/" ) ;
325- const serviceFromEnvVar = existing . find ( ( s ) => {
326- const serviceName = parseServiceName ( s . service . name ) ;
327- return (
328- serviceName . serviceId === serviceIdFromEnvVar &&
329- serviceName . location === serviceLocationFromEnvVar
330- ) ;
331- } ) ;
332- if ( serviceFromEnvVar ) {
333- // FDC_CONNECTOR or FDC_SERVICE env var match an existing service.
334- logBullet (
335- `Picking up the existing service ${ clc . bold ( serviceLocationFromEnvVar + "/" + serviceIdFromEnvVar ) } .` ,
336- ) ;
337- return serviceFromEnvVar ;
330+ const serviceEnvVar = envOverride ( "FDC_CONNECTOR" , "" ) || envOverride ( "FDC_SERVICE" , "" ) ;
331+ if ( serviceEnvVar ) {
332+ const [ serviceLocationFromEnvVar , serviceIdFromEnvVar ] = serviceEnvVar . split ( "/" ) ;
333+ const serviceFromEnvVar = existing . find ( ( s ) => {
334+ const serviceName = parseServiceName ( s . service . name ) ;
335+ return (
336+ serviceName . serviceId === serviceIdFromEnvVar &&
337+ serviceName . location === serviceLocationFromEnvVar
338+ ) ;
339+ } ) ;
340+ if ( serviceFromEnvVar ) {
341+ logBullet (
342+ `Picking up the existing service ${ clc . bold ( serviceLocationFromEnvVar + "/" + serviceIdFromEnvVar ) } .` ,
343+ ) ;
344+ return serviceFromEnvVar ;
345+ }
346+ logWarning ( `Unable to pick up an existing service based on FDC_SERVICE=${ serviceEnvVar } .` ) ;
338347 }
339348 const choices : Array < { name : string ; value : serviceAndSchema | undefined } > = existing . map (
340349 ( s ) => {
0 commit comments