import * as fse from 'fs-extra' ; import * as path from 'path' ; import { IActionContext } from 'vscode-azureextensionui' ; import { IBindingWizardContext } from '../commands/addBinding/IBindingWizardContext' ; import { IFunctionWizardContext } from '../commands/createFunction/IFunctionWizardContext' ; import { extensionsCsprojFileName , extInstallCommand , hostFileName , ProjectLanguage , settingsFileName , tasksFileName , vscodeFolderName } from '../constants' ; import { IHostJsonV2 } from '../funcConfig/host' ; import { FuncVersion } from '../FuncVersion' ; import { localize } from '../localize' ; import { IBindingTemplate } from '../templates/IBindingTemplate' ; import { IFunctionTemplate } from '../templates/IFunctionTemplate' ; import { promptToReinitializeProject } from '../vsCodeConfig/promptToReinitializeProject' ; import { bundleFeedUtils } from './bundleFeedUtils' ; import { writeFormattedJson } from './fs' ; export async function verifyExtensionBundle ( context , template ) { if ( context . version === FuncVersion . v1 || ! bundleFeedUtils . isBundleTemplate ( template ) || context . language === ProjectLanguage . CSharp || context . language === ProjectLanguage . FSharp ) { context . telemetry . properties . bundleResult = 'n/a' ; return ; } if ( await hasExtensionsVSCodeConfig ( context , context . workspacePath ) || await hasExtensionsCsproj ( context , context . projectPath ) ) { context . telemetry . properties . bundleResult = 'hasExtensionsConfig' ; } else { const hostFilePath = path . join ( context . projectPath , hostFileName ) ; if ( ! ( await fse . pathExists ( hostFilePath ) ) ) { context . telemetry . properties . bundleResult = 'missingHostJson' ; } else { let hostJson ; try { hostJson = < IHostJsonV2 > await fse . readJSON ( hostFilePath ) ; } catch ( error ) { context . telemetry . properties . bundleResult = 'failedToParseHostJson' ; return ; } if ( ! hostJson . extensionBundle ) { context . telemetry . properties . bundleResult = 'addedBundle' ; await bundleFeedUtils . addDefaultBundle ( context , hostJson ) ; await writeFormattedJson ( hostFilePath , hostJson ) ; } else { context . telemetry . properties . bundleResult = 'alreadyHasBundle' ; } } } } export async function verifyExtensionsConfig ( context , workspacePath , projectPath ) { if ( await hasExtensionsCsproj ( context , projectPath ) && ! await hasExtensionsVSCodeConfig ( context , workspacePath ) ) { const message = localize ( 'mismatchExtensionsCsproj' , 'Your project is not configured to work with "extensions.csproj".' ) ; await promptToReinitializeProject ( projectPath , 'showExtensionsCsprojWarning' , message , 'https://aka.ms/AA8wo2c' , context ) ; } } async function hasExtensionsVSCodeConfig ( context , workspacePath ) { let result = false ; try { const filesToCheck = [ tasksFileName , settingsFileName ] ; for ( const file of filesToCheck ) { const filePath = path . join ( workspacePath , vscodeFolderName , file ) ; if ( await fse . pathExists ( filePath ) ) { const contents = ( await fse . readFile ( filePath ) ) . toString ( ) ; if ( contents . includes ( extInstallCommand ) ) { result = true ; break ; } } } } catch { } context . telemetry . properties . hasExtensionsVSCodeConfig = String ( result ) ; return result ; } async function hasExtensionsCsproj ( context , projectPath ) { let result = false ; try { result = await fse . pathExists ( path . join ( projectPath , extensionsCsprojFileName ) ) ; } catch { } context . telemetry . properties . hasExtensionsCsproj = String ( result ) ; return result ; }