import * as fse from 'fs-extra' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { DialogResponses , IActionContext , parseError } from 'vscode-azureextensionui' ; import { localSettingsFileName } from '../constants' ; import { localize } from '../localize' ; import * as fsUtil from '../utils/fs' ; import { parseJson } from '../utils/parseJson' ; export interface ILocalSettingsJson { IsEncrypted ? : boolean ; Values ? : { [ key ] : string ; } ; Host ? : { [ key ] : string ; } ; ConnectionStrings ? : { [ key ] : string ; } ; } export const azureWebJobsStorageKey = 'AzureWebJobsStorage' ; export async function getAzureWebJobsStorage ( context , projectPath ) { if ( process . env [ azureWebJobsStorageKey ] ) { return process . env [ azureWebJobsStorageKey ] ; } const settings = await getLocalSettingsJson ( context , path . join ( projectPath , localSettingsFileName ) ) ; return settings . Values && settings . Values [ azureWebJobsStorageKey ] ; } export enum MismatchBehavior { /**
     * Asks the user if they want to overwrite
     */ Prompt , /**
     * Overwrites without prompting
     */ Overwrite , /**
     * Returns without changing anything
     */ DontChange } export async function setLocalAppSetting ( context , functionAppPath , key , value , behavior = MismatchBehavior . Prompt ) { const localSettingsPath = path . join ( functionAppPath , localSettingsFileName ) ; const settings = await getLocalSettingsJson ( context , localSettingsPath ) ; settings . Values = settings . Values || { } ; if ( settings . Values [ key ] === value ) { return ; } else if ( settings . Values [ key ] ) { if ( behavior === MismatchBehavior . Prompt ) { const message = localize ( 'SettingAlreadyExists' , 'Local app setting \'{0}\' already exists. Overwrite?' , key ) ; if ( await context . ui . showWarningMessage ( message , { modal : true , stepName : 'overwriteLocalSetting' } , DialogResponses . yes ) !== DialogResponses . yes ) { return ; } } else if ( behavior === MismatchBehavior . DontChange ) { return ; } } settings . Values [ key ] = value ; await fsUtil . writeFormattedJson ( localSettingsPath , settings ) ; } export async function getLocalSettingsJson ( context , localSettingsPath , allowOverwrite = false ) { if ( await fse . pathExists ( localSettingsPath ) ) { const data = ( await fse . readFile ( localSettingsPath ) ) . toString ( ) ; if ( /[^\s]/ . test ( data ) ) { try { return parseJson ( data ) ; } catch ( error ) { if ( allowOverwrite ) { const message = localize ( 'failedToParseWithOverwrite' , 'Failed to parse "{0}": {1}. Overwrite?' , localSettingsFileName , parseError ( error ) . message ) ; const overwriteButton = { title : localize ( 'overwrite' , 'Overwrite' ) } ; await context . ui . showWarningMessage ( message , { modal : true , stepName : 'overwriteLocalSettings' } , overwriteButton ) ; } else { const message = localize ( 'failedToParse' , 'Failed to parse "{0}": {1}.' , localSettingsFileName , parseError ( error ) . message ) ; throw new Error ( message ) ; } } } } return { IsEncrypted : false } ; }