@@ -2,7 +2,7 @@ import * as clc from "colorette";
2
2
import * as repo from "./repo" ;
3
3
import * as poller from "../../../operation-poller" ;
4
4
import * as gcp from "../../../gcp/frameworks" ;
5
- import { logBullet , logSuccess } from "../../../utils" ;
5
+ import { logBullet , logSuccess , logWarning } from "../../../utils" ;
6
6
import { frameworksOrigin } from "../../../api" ;
7
7
import { Backend , BackendOutputOnlyFields } from "../../../gcp/frameworks" ;
8
8
import { Repository } from "../../../gcp/cloudbuild" ;
@@ -26,32 +26,40 @@ export async function doSetup(setup: any, projectId: string): Promise<void> {
26
26
27
27
logBullet ( "First we need a few details to create your backend." ) ;
28
28
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" ,
32
45
type : "input" ,
33
46
default : "acme-inc-web" ,
34
47
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 | undefined = await onboardBackend ( projectId , location , backendId ) ;
55
63
56
64
if ( backend ) {
57
65
logSuccess ( `Successfully created backend:\n\t${ backend . name } ` ) ;
@@ -74,75 +82,23 @@ function toBackend(cloudBuildConnRepo: Repository): Omit<Backend, BackendOutputO
74
82
}
75
83
76
84
/**
77
- * Creates backend if it doesn't exist .
85
+ * Walks thorugh creating a new backend .
78
86
*/
79
- export async function getOrCreateBackend (
87
+ export async function onboardBackend (
80
88
projectId : string ,
81
- setup : any
89
+ location : string ,
90
+ backendId : string
82
91
) : 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 (
111
- projectId : string ,
112
- setup : any ,
113
- location : string
114
- ) : 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
+ const backendDetails = toBackend ( cloudBuildConnRepo ) ;
101
+ return await createBackend ( projectId , location , backendDetails , backendId ) ;
146
102
}
147
103
148
104
/**
0 commit comments