@@ -2,7 +2,7 @@ import * as clc from "colorette";
22import * as repo from "./repo" ;
33import * as poller from "../../../operation-poller" ;
44import * as gcp from "../../../gcp/frameworks" ;
5- import { logBullet , logSuccess } from "../../../utils" ;
5+ import { logBullet , logSuccess , logWarning } from "../../../utils" ;
66import { frameworksOrigin } from "../../../api" ;
77import { Backend , BackendOutputOnlyFields } from "../../../gcp/frameworks" ;
88import { Repository } from "../../../gcp/cloudbuild" ;
@@ -26,32 +26,40 @@ export async function doSetup(setup: any, projectId: string): Promise<void> {
2626
2727 logBullet ( "First we need a few details to create your backend." ) ;
2828
29- await promptOnce (
30- {
31- name : "serviceName" ,
29+ const location = await promptOnce ( {
30+ name : "region" ,
31+ type : "list" ,
32+ default : DEFAULT_REGION ,
33+ message :
34+ "Please select a region " +
35+ `(${ clc . yellow ( "info" ) } : Your region determines where your backend is located):\n` ,
36+ choices : ALLOWED_REGIONS ,
37+ } ) ;
38+
39+ logSuccess ( `Region set to ${ location } .\n` ) ;
40+
41+ let backendId : string ;
42+ while ( true ) {
43+ backendId = await promptOnce ( {
44+ name : "backendId" ,
3245 type : "input" ,
3346 default : "acme-inc-web" ,
3447 message : "Create a name for your backend [1-30 characters]" ,
35- } ,
36- setup . frameworks
37- ) ;
38-
39- await promptOnce (
40- {
41- name : "region" ,
42- type : "list" ,
43- default : DEFAULT_REGION ,
44- message :
45- "Please select a region " +
46- `(${ clc . yellow ( "info" ) } : Your region determines where your backend is located):\n` ,
47- choices : ALLOWED_REGIONS ,
48- } ,
49- setup . frameworks
50- ) ;
51-
52- logSuccess ( `Region set to ${ setup . frameworks . region } .\n` ) ;
53-
54- const backend : Backend | undefined = await getOrCreateBackend ( projectId , setup ) ;
48+ } ) ;
49+ try {
50+ await gcp . getBackend ( projectId , location , backendId ) ;
51+ } catch ( err : any ) {
52+ if ( err . status === 404 ) {
53+ break ;
54+ }
55+ throw new FirebaseError (
56+ `Failed to check if backend with id ${ backendId } already exists in ${ location } ` ,
57+ { original : err }
58+ ) ;
59+ }
60+ logWarning ( `Backend with id ${ backendId } already exists in ${ location } ` ) ;
61+ }
62+ const backend : Backend = await onboardBackend ( projectId , location , backendId ) ;
5563
5664 if ( backend ) {
5765 logSuccess ( `Successfully created backend:\n\t${ backend . name } ` ) ;
@@ -74,75 +82,24 @@ function toBackend(cloudBuildConnRepo: Repository): Omit<Backend, BackendOutputO
7482}
7583
7684/**
77- * Creates backend if it doesn't exist .
85+ * Walkthrough the flow for creating a new backend .
7886 */
79- export async function getOrCreateBackend (
80- projectId : string ,
81- setup : any
82- ) : Promise < Backend | undefined > {
83- const location : string = setup . frameworks . region ;
84- try {
85- return await getExistingBackend ( projectId , setup , location ) ;
86- } catch ( err : unknown ) {
87- if ( ( err as FirebaseError ) . status === 404 ) {
88- const cloudBuildConnRepo = await repo . linkGitHubRepository ( projectId , location ) ;
89- await promptOnce (
90- {
91- name : "branchName" ,
92- type : "input" ,
93- default : "main" ,
94- message : "Which branch do you want to deploy?" ,
95- } ,
96- setup . frameworks
97- ) ;
98- const backendDetails = toBackend ( cloudBuildConnRepo ) ;
99- return await createBackend ( projectId , location , backendDetails , setup . frameworks . serviceName ) ;
100- } else {
101- throw new FirebaseError (
102- `Failed to get or create a backend using the given initialization details: ${ err } `
103- ) ;
104- }
105- }
106-
107- return undefined ;
108- }
109-
110- async function getExistingBackend (
87+ export async function onboardBackend (
11188 projectId : string ,
112- setup : any ,
113- location : string
89+ location : string ,
90+ backendId : string
11491) : Promise < Backend > {
115- let backend = await gcp . getBackend ( projectId , location , setup . frameworks . serviceName ) ;
116- while ( backend ) {
117- setup . frameworks . serviceName = undefined ;
118- await promptOnce (
119- {
120- name : "existingBackend" ,
121- type : "confirm" ,
122- default : true ,
123- message :
124- "A backend already exists for the given serviceName, do you want to use existing backend? (yes/no)" ,
125- } ,
126- setup . frameworks
127- ) ;
128- if ( setup . frameworks . existingBackend ) {
129- logBullet ( "Using the existing backend." ) ;
130- return backend ;
131- }
132- await promptOnce (
133- {
134- name : "serviceName" ,
135- type : "input" ,
136- default : "acme-inc-web" ,
137- message : "Please enter a new service name [1-30 characters]" ,
138- } ,
139- setup . frameworks
140- ) ;
141- backend = await gcp . getBackend ( projectId , location , setup . frameworks . serviceName ) ;
142- setup . frameworks . existingBackend = undefined ;
143- }
144-
145- return backend ;
92+ const cloudBuildConnRepo = await repo . linkGitHubRepository ( projectId , location ) ;
93+ const barnchName = await promptOnce ( {
94+ name : "branchName" ,
95+ type : "input" ,
96+ default : "main" ,
97+ message : "Which branch do you want to deploy?" ,
98+ } ) ;
99+ // branchName unused for now.
100+ void barnchName ;
101+ const backendDetails = toBackend ( cloudBuildConnRepo ) ;
102+ return await createBackend ( projectId , location , backendDetails , backendId ) ;
146103}
147104
148105/**
0 commit comments