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 { getLocalSettingsJson , 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 downloadAppSettings ( context , node ? ) { if ( ! node ) { node = await ext . tree . showTreeItemPicker < AppSettingsTreeItem > ( AppSettingsTreeItem . contextValue , context ) ; } const client = await node . clientProvider . createClient ( context ) ; await node . runWithTemporaryDescription ( context , localize ( 'downloading' , 'Downloading...' ) , async ( ) => { await downloadAppSettingsInternal ( context , client ) ; } ) ; } export async function downloadAppSettingsInternal ( context , client ) { const message = localize ( 'selectLocalSettings' , 'Select the destination file for your downloaded settings.' ) ; const localSettingsPath = await getLocalSettingsFile ( context , message ) ; const localSettingsUri = vscode . Uri . file ( localSettingsPath ) ; let localSettings = await getLocalSettingsJson ( context , localSettingsPath , true ) ; const isEncrypted = localSettings . IsEncrypted ; if ( localSettings . IsEncrypted ) { await decryptLocalSettings ( context , localSettingsUri ) ; localSettings = < ILocalSettingsJson > await fse . readJson ( localSettingsPath ) ; } try { if ( ! localSettings . Values ) { localSettings . Values = { } ; } const remoteSettings = await client . listApplicationSettings ( ) ; ext . outputChannel . appendLog ( localize ( 'downloadingSettings' , 'Downloading settings...' ) , { resourceName : client . fullName } ) ; if ( remoteSettings . properties ) { await confirmOverwriteSettings ( context , remoteSettings . properties , localSettings . Values , localSettingsFileName ) ; } await fse . ensureFile ( localSettingsPath ) ; await fse . writeJson ( localSettingsPath , localSettings , { spaces : 2 } ) ; } finally { if ( isEncrypted ) { await encryptLocalSettings ( context , localSettingsUri ) ; } } ext . outputChannel . appendLog ( localize ( 'downloadedSettings' , 'Successfully downloaded settings.' ) , { resourceName : client . fullName } ) ; const openFile = localize ( 'openFile' , 'Open File' ) ; void vscode . window . showInformationMessage ( localize ( 'downloadedSettingsFrom' , 'Successfully downloaded settings from "{0}".' , client . fullName ) , openFile , viewOutput ) . then ( async ( result ) => { if ( result === openFile ) { const doc = await vscode . workspace . openTextDocument ( localSettingsUri ) ; await vscode . window . showTextDocument ( doc ) ; } else if ( result === viewOutput ) { ext . outputChannel . show ( ) ; } } ) ; }