@@ -5,7 +5,8 @@ import { existsSync, promises as fs } from "node:fs";
55import path from "node:path" ;
66import { logger , logGitHubIssueMessageAndExit } from "../../../core/utils/logger.js" ;
77import { authenticateWithAzureIdentity , listSubscriptions , listTenants } from "../../../core/account.js" ;
8- import { ENV_FILENAME } from "../../../core/constants.js" ;
8+ import { AZURE_LOGIN_CONFIG , ENV_FILENAME } from "../../../core/constants.js" ;
9+ import { pathExists , safeReadJson } from "../../../core/utils/file.js" ;
910import { updateGitIgnore } from "../../../core/git.js" ;
1011import { chooseSubscription , chooseTenant } from "../../../core/prompts.js" ;
1112import { Environment } from "../../../core/swa-cli-persistence-plugin/impl/azure-environment.js" ;
@@ -43,6 +44,7 @@ export async function login(options: SWACLIConfig): Promise<any> {
4344 logger . log ( chalk . green ( `✔ Successfully logged into Azure!` ) ) ;
4445 }
4546
47+ options = await tryGetAzTenantAndSubscription ( options ) ;
4648 return await setupProjectCredentials ( options , credentialChain ) ;
4749}
4850
@@ -149,3 +151,24 @@ async function storeProjectCredentialsInEnvFile(
149151 await updateGitIgnore ( ENV_FILENAME ) ;
150152 }
151153}
154+
155+ async function tryGetAzTenantAndSubscription ( options : SWACLIConfig ) {
156+ const doesAzureConfigExist = await pathExists ( AZURE_LOGIN_CONFIG ) ;
157+ if ( ! doesAzureConfigExist ) {
158+ return options ;
159+ } else {
160+ logger . silly ( `Found an existing Azure config file, getting Tenant and Subscription Id from ${ AZURE_LOGIN_CONFIG } ` ) ;
161+
162+ const azureProfile = await safeReadJson ( AZURE_LOGIN_CONFIG ) ;
163+ if ( azureProfile ) {
164+ const allSubscriptions = ( azureProfile as AzureProfile ) . subscriptions ;
165+ const defaultAzureInfo = allSubscriptions . find ( ( subscription ) => subscription . isDefault == true ) ;
166+ if ( defaultAzureInfo ) {
167+ options . tenantId = defaultAzureInfo . tenantId ;
168+ options . subscriptionId = defaultAzureInfo . id ;
169+ }
170+ }
171+
172+ return options ;
173+ }
174+ }
0 commit comments