@@ -4355,21 +4355,31 @@ async function runAuthLogin(): Promise<number> {
43554355 }
43564356
43574357 const refreshedStorage = await loadAccounts ( ) ;
4358- const existingCount = refreshedStorage ?. accounts . length ?? 0 ;
4358+ let existingCount = refreshedStorage ?. accounts . length ?? 0 ;
43594359 let forceNewLogin = existingCount > 0 ;
4360- let namedBackups : NamedBackupSummary [ ] = [ ] ;
4361- if ( existingCount === 0 ) {
4360+ const loadNamedBackupsForOnboarding = async ( ) : Promise < NamedBackupSummary [ ] > => {
4361+ if ( existingCount > 0 ) {
4362+ return [ ] ;
4363+ }
43624364 try {
4363- namedBackups = await getNamedBackups ( ) ;
4365+ return await getNamedBackups ( ) ;
43644366 } catch ( error ) {
4367+ const code = ( error as NodeJS . ErrnoException ) . code ;
43654368 log . debug ( "getNamedBackups failed, skipping restore option" , {
4369+ code,
43664370 error : error instanceof Error ? error . message : String ( error ) ,
43674371 } ) ;
4368- namedBackups = [ ] ;
4372+ if ( code && code !== "ENOENT" ) {
4373+ console . warn (
4374+ "Named backup discovery failed. Continuing with browser or manual sign-in only." ,
4375+ ) ;
4376+ }
4377+ return [ ] ;
43694378 }
4370- }
4371- const latestNamedBackup = namedBackups [ 0 ] ?? null ;
4379+ } ;
4380+ let namedBackups = await loadNamedBackupsForOnboarding ( ) ;
43724381 while ( true ) {
4382+ const latestNamedBackup = namedBackups [ 0 ] ?? null ;
43734383 const signInMode = await promptOAuthSignInMode ( latestNamedBackup ) ;
43744384 if ( signInMode === "cancel" ) {
43754385 if ( existingCount > 0 ) {
@@ -4379,15 +4389,20 @@ async function runAuthLogin(): Promise<number> {
43794389 console . log ( "Cancelled." ) ;
43804390 return 0 ;
43814391 }
4382- if ( signInMode === "restore-backup" && latestNamedBackup ) {
4383- const restoreMode = await promptBackupRestoreMode ( latestNamedBackup ) ;
4392+ if ( signInMode === "restore-backup" ) {
4393+ namedBackups = await loadNamedBackupsForOnboarding ( ) ;
4394+ const latestAvailableBackup = namedBackups [ 0 ] ?? null ;
4395+ if ( ! latestAvailableBackup ) {
4396+ continue ;
4397+ }
4398+ const restoreMode = await promptBackupRestoreMode ( latestAvailableBackup ) ;
43844399 if ( restoreMode === "back" ) {
43854400 continue ;
43864401 }
43874402
43884403 const selectedBackup = restoreMode === "manual"
43894404 ? await promptManualBackupSelection ( namedBackups )
4390- : latestNamedBackup ;
4405+ : latestAvailableBackup ;
43914406 if ( ! selectedBackup ) {
43924407 continue ;
43934408 }
@@ -4473,6 +4488,8 @@ async function runAuthLogin(): Promise<number> {
44734488
44744489 const latestStorage = await loadAccounts ( ) ;
44754490 const count = latestStorage ?. accounts . length ?? 1 ;
4491+ existingCount = count ;
4492+ namedBackups = [ ] ;
44764493 console . log ( `Added account. Total: ${ count } ` ) ;
44774494 if ( count >= ACCOUNT_LIMITS . MAX_ACCOUNTS ) {
44784495 console . log ( `Reached maximum account limit (${ ACCOUNT_LIMITS . MAX_ACCOUNTS } ).` ) ;
0 commit comments