import * as fse from 'fs-extra' ; import { Progress , Uri , window , workspace } from 'vscode' ; import { AzureWizardExecuteStep , callWithTelemetryAndErrorHandling , IActionContext } from 'vscode-azureextensionui' ; import { ext } from '../../extensionVariables' ; import { localize } from '../../localize' ; import { IFunctionTemplate } from '../../templates/IFunctionTemplate' ; import { nonNullProp } from '../../utils/nonNull' ; import { verifyExtensionBundle } from '../../utils/verifyExtensionBundle' ; import { getContainingWorkspace } from '../../utils/workspace' ; import { IFunctionWizardContext } from './IFunctionWizardContext' ; interface ICachedFunction { projectPath : string ; newFilePath : string ; isHttpTrigger : boolean ; } const cacheKey = 'azFuncPostFunctionCreate' ; export async function runPostFunctionCreateStepsFromCache ( ) { const cachedFunc = ext . context . globalState . get ( cacheKey ) ; if ( cachedFunc ) { try { runPostFunctionCreateSteps ( cachedFunc ) ; } finally { await ext . context . globalState . update ( cacheKey , undefined ) ; } } } export abstract class FunctionCreateStepBase < T extends IFunctionWizardContext > extends AzureWizardExecuteStep < T > { public priority : number = 220 ; /**
     * Returns the full path to the new function file
     */ public abstract executeCore ( context ) ; public async execute ( context , progress ) { const template = nonNullProp ( context , 'functionTemplate' ) ; context . telemetry . properties . projectLanguage = context . language ; context . telemetry . properties . projectRuntime = context . version ; context . telemetry . properties . templateId = template . id ; progress . report ( { message : localize ( 'creatingFunction' , 'Creating new {0}...' , template . name ) } ) ; const newFilePath = await this . executeCore ( context ) ; await verifyExtensionBundle ( context , template ) ; const cachedFunc = { projectPath : context . projectPath , newFilePath , isHttpTrigger : template . isHttpTrigger } ; if ( context . openBehavior ) { await ext . context . globalState . update ( cacheKey , cachedFunc ) ; setTimeout ( ( ) => { void ext . context . globalState . update ( cacheKey , undefined ) ; } , 5 * 1000 ) ; } runPostFunctionCreateSteps ( cachedFunc ) ; } public shouldExecute ( context ) { return ! ! context . functionTemplate ; } } function runPostFunctionCreateSteps ( func ) { void callWithTelemetryAndErrorHandling ( 'postFunctionCreate' , async ( context ) => { context . telemetry . suppressIfSuccessful = true ; if ( getContainingWorkspace ( func . projectPath ) ) { if ( await fse . pathExists ( func . newFilePath ) ) { await window . showTextDocument ( await workspace . openTextDocument ( Uri . file ( func . newFilePath ) ) ) ; } } } ) ; }