@@ -15,6 +15,25 @@ function question(prompt) {
1515 } )
1616}
1717
18+ /**
19+ * Parse Wrangler whoami output to find available accounts
20+ */
21+ function parseAccounts ( output ) {
22+ const accounts = [ ]
23+ const lines = output . split ( '\n' )
24+ for ( const line of lines ) {
25+ const match = line . match ( / │ \s * ( [ ^ │ ] + ) \s * │ \s * ( [ a - f 0 - 9 ] { 32 } ) \s * │ / )
26+ if ( match ) {
27+ const name = match [ 1 ] . trim ( )
28+ const id = match [ 2 ] . trim ( )
29+ if ( id !== 'Account ID' ) {
30+ accounts . push ( { name, id } )
31+ }
32+ }
33+ }
34+ return accounts
35+ }
36+
1837/**
1938 * Parse command-line arguments into an object
2039 */
@@ -101,15 +120,28 @@ async function setup() {
101120
102121 // Check if Wrangler is authenticated
103122 let isWranglerAuthenticated = false
123+ let availableAccounts = [ ]
124+
104125 try {
105126 const whoamiOutput = execSync ( 'wrangler whoami' , { encoding : 'utf8' , stdio : [ 'ignore' , 'pipe' , 'ignore' ] } )
106127 isWranglerAuthenticated = true
107128 console . log ( '✅ Wrangler is already authenticated' )
108129
109- const accountIdMatch = whoamiOutput . match ( / ( [ a - f 0 - 9 ] { 32 } ) / )
110- if ( accountIdMatch && accountIdMatch [ 1 ] ) {
111- cloudflareAccountId = accountIdMatch [ 1 ]
130+ availableAccounts = parseAccounts ( whoamiOutput )
131+
132+ // Fallback if table parsing failed but simple regex works
133+ if ( availableAccounts . length === 0 ) {
134+ const accountIdMatch = whoamiOutput . match ( / ( [ a - f 0 - 9 ] { 32 } ) / )
135+ if ( accountIdMatch && accountIdMatch [ 1 ] ) {
136+ availableAccounts . push ( { name : 'Default Account' , id : accountIdMatch [ 1 ] } )
137+ }
138+ }
139+
140+ if ( availableAccounts . length === 1 ) {
141+ cloudflareAccountId = availableAccounts [ 0 ] . id
112142 console . log ( `✅ Using Cloudflare Account ID: ${ cloudflareAccountId } ` )
143+ } else if ( availableAccounts . length > 1 ) {
144+ console . log ( `✅ Found ${ availableAccounts . length } available Cloudflare accounts` )
113145 }
114146 } catch ( e ) {
115147 // Not authenticated
@@ -138,6 +170,26 @@ async function setup() {
138170 cloudflareAccountId = await question ( 'Cloudflare Account ID: ' )
139171 } else {
140172 console . log ( '✅ Using existing Wrangler authentication\n' )
173+
174+ if ( availableAccounts . length > 1 ) {
175+ console . log ( '📋 Select a Cloudflare account to use:' )
176+ availableAccounts . forEach ( ( acc , index ) => {
177+ console . log ( ` ${ index + 1 } . ${ acc . name } (${ acc . id } )` )
178+ } )
179+
180+ let validSelection = false
181+ while ( ! validSelection ) {
182+ const selection = await question ( `\nEnter number (1-${ availableAccounts . length } ): ` )
183+ const index = parseInt ( selection ) - 1
184+ if ( index >= 0 && index < availableAccounts . length ) {
185+ cloudflareAccountId = availableAccounts [ index ] . id
186+ console . log ( `✅ Selected account: ${ availableAccounts [ index ] . name } ` )
187+ validSelection = true
188+ } else {
189+ console . log ( '❌ Invalid selection, please try again.' )
190+ }
191+ }
192+ }
141193 }
142194
143195 console . log ( '🔑 Stripe Configuration:\n' )
@@ -167,6 +219,10 @@ async function setup() {
167219 console . log ( '✅ Using API Token authentication (skipping OAuth login)' )
168220 } else {
169221 console . log ( '\n🔐 Using existing Wrangler authentication...' )
222+ if ( cloudflareAccountId ) {
223+ process . env . CLOUDFLARE_ACCOUNT_ID = cloudflareAccountId
224+ console . log ( `✅ Set account context to ID: ${ cloudflareAccountId } ` )
225+ }
170226 }
171227
172228 // Verify Wrangler can access account
0 commit comments