Skip to content

Commit 3629a57

Browse files
authored
Merge pull request #55 from AJFrio/feature/account-selection-setup-5265924464207450351
Add interactive Cloudflare account selection to setup script
2 parents 8bc2dc2 + 7851ae6 commit 3629a57

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

scripts/setup.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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-f0-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-f0-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-f0-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

Comments
 (0)