import { WebSiteManagementModels } from "@azure/arm-appservice" ; import * as fse from 'fs-extra' ; import * as vscode from 'vscode' ; import { AppSettingsTreeItem , confirmOverwriteSettings , IAppSettingsClient } from "vscode-azureappservice" ; import { IActionContext } from "vscode-azureextensionui" ; import { localSettingsFileName , viewOutput } from "../../constants" ; import { ext } from "../../extensionVariables" ; import { ILocalSettingsJson } from "../../funcConfig/local.settings" ; import { localize } from "../../localize" ; import * as api from '../../vscode-azurefunctions.api' ; import { decryptLocalSettings } from "./decryptLocalSettings" ; import { encryptLocalSettings } from "./encryptLocalSettings" ; import { getLocalSettingsFile } from "./getLocalSettingsFile" ; export async function uploadAppSettings ( context , node ? , workspaceFolder ? , exclude ? ) { context . telemetry . eventVersion = 2 ; if ( ! node ) { node = await ext . tree . showTreeItemPicker < AppSettingsTreeItem > ( AppSettingsTreeItem . contextValue , context ) ; } const client = await node . clientProvider . createClient ( context ) ; await node . runWithTemporaryDescription ( context , localize ( 'uploading' , 'Uploading...' ) , async ( ) => { await uploadAppSettingsInternal ( context , client , workspaceFolder , exclude ) ; } ) ; } export async function uploadAppSettingsInternal ( context , client , workspaceFolder ? , exclude ? ) { const message = localize ( 'selectLocalSettings' , 'Select the local settings file to upload.' ) ; const localSettingsPath = await getLocalSettingsFile ( context , message , workspaceFolder ) ; const localSettingsUri = vscode . Uri . file ( localSettingsPath ) ; let localSettings = < ILocalSettingsJson > await fse . readJson ( localSettingsPath ) ; if ( localSettings . IsEncrypted ) { await decryptLocalSettings ( context , localSettingsUri ) ; try { localSettings = < ILocalSettingsJson > await fse . readJson ( localSettingsPath ) ; } finally { await encryptLocalSettings ( context , localSettingsUri ) ; } } if ( localSettings . Values ) { const remoteSettings = await client . listApplicationSettings ( ) ; if ( ! remoteSettings . properties ) { remoteSettings . properties = { } ; } const excludedAppSettings = [ ] ; if ( exclude ) { Object . keys ( localSettings . Values ) . forEach ( ( settingName ) => { if ( exclude . some ( ( exclusion ) => typeof exclusion === 'string' ? settingName . toLowerCase ( ) === exclusion . toLowerCase ( ) : settingName . match ( new RegExp ( exclusion , 'i' ) ) ) ) { delete localSettings . Values ?. [ settingName ] ; excludedAppSettings . push ( settingName ) ; } } ) ; } const uploadSettings = localize ( 'uploadingSettings' , 'Uploading settings...' ) ; ext . outputChannel . appendLog ( uploadSettings , { resourceName : client . fullName } ) ; await confirmOverwriteSettings ( context , localSettings . Values , remoteSettings . properties , client . fullName ) ; if ( excludedAppSettings . length ) { ext . outputChannel . appendLog ( localize ( 'excludedSettings' , 'Excluded the following settings:' ) ) ; excludedAppSettings . forEach ( ( key ) => ext . outputChannel . appendLine ( `- ${ key }` ) ) ; } await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : localize ( 'uploadingSettingsTo' , 'Uploading settings to "{0}"...' , client . fullName ) } , async ( ) => { await client . updateApplicationSettings ( remoteSettings ) ; } ) ; ext . outputChannel . appendLog ( localize ( 'uploadedSettings' , 'Successfully uploaded settings.' ) , { resourceName : client . fullName } ) ; void vscode . window . showInformationMessage ( localize ( 'uploadedSettingsTo' , 'Successfully uploaded settings to "{0}".' , client . fullName ) , viewOutput ) . then ( result => { if ( result === viewOutput ) { ext . outputChannel . show ( ) ; } } ) ; } else { throw new Error ( localize ( 'noSettings' , 'No settings found in "{0}".' , localSettingsFileName ) ) ; } }