import * as fse from 'fs-extra' ; import * as path from 'path' ; import { AzureWizardPromptStep } from 'vscode-azureextensionui' ; import { localize } from '../../localize' ; import { IFunctionTemplate } from '../../templates/IFunctionTemplate' ; import { nonNullProp } from '../../utils/nonNull' ; import { IFunctionWizardContext } from './IFunctionWizardContext' ; export abstract class FunctionNameStepBase < T extends IFunctionWizardContext > extends AzureWizardPromptStep < T > { public async prompt ( context ) { const template = nonNullProp ( context , 'functionTemplate' ) ; const uniqueFunctionName = await this . getUniqueFunctionName ( context ) ; context . functionName = await context . ui . showInputBox ( { placeHolder : localize ( 'funcNamePlaceholder' , 'Function name' ) , prompt : localize ( 'funcNamePrompt' , 'Provide a function name' ) , validateInput : async ( s ) : Promise < string | undefined > => await this . validateFunctionName ( context , s ) , value : uniqueFunctionName || template . defaultFunctionName } ) ; } public shouldPrompt ( context ) { return ! context . functionName ; } protected abstract getUniqueFunctionName ( context ) ; protected abstract validateFunctionNameCore ( context , name ) ; /**
     * NOTE: This will always at least add `1` to the default value to (hopefully) make clear the function name is an instance of the template
     */ protected async getUniqueFsPath ( folderPath , defaultValue , fileExtension ? ) { let count = 1 ; const maxCount = 1024 ; while ( count < maxCount ) { const fileName = defaultValue + count . toString ( ) ; if ( ! ( await fse . pathExists ( path . join ( folderPath , fileExtension ? fileName + fileExtension : fileName ) ) ) ) { return fileName ; } count += 1 ; } return undefined ; } private async validateFunctionName ( context , name ) { if ( ! name ) { return localize ( 'emptyTemplateNameError' , 'The function name cannot be empty.' ) ; } else if ( ! /^[a-z][a-z\d_\-]*$/i . test ( name ) ) { return localize ( 'functionNameInvalidMessage' , 'Function name must start with a letter and can only contain letters, digits, "_" and "-".' ) ; } else { return await this . validateFunctionNameCore ( context , name ) ; } } }