import * as fse from 'fs-extra' ; import { ParsedFunctionJson } from '../../../funcConfig/function' ; import { localize } from '../../../localize' ; import { IBindingWizardContext } from '../IBindingWizardContext' ; import { StringPromptStep } from './StringPromptStep' ; export class BindingNameStep extends StringPromptStep { private _functionJson : ParsedFunctionJson | undefined ; public async getDefaultValue ( context ) { const defaultValue = await super . getDefaultValue ( context ) ; if ( defaultValue ) { let uniqueValue = defaultValue ; let count = 1 ; const maxTries = 1000 ; while ( count < maxTries ) { if ( ! await this . bindingExists ( context , uniqueValue ) ) { return uniqueValue ; } else { count += 1 ; uniqueValue = defaultValue + count . toString ( ) ; } } } return defaultValue ; } public async validateInput ( context , val ) { if ( ! val ) { return localize ( 'emptyTemplateNameError' , 'The binding name cannot be empty.' ) ; } else if ( await this . bindingExists ( context , val ) ) { return localize ( 'existingBindingError' , 'A binding with the name "{0}" already exists.' , val ) ; } else { return super . validateInput ( context , val ) ; } } private async bindingExists ( context , val ) { try { if ( ! this . _functionJson ) { this . _functionJson = new ParsedFunctionJson ( await fse . readJSON ( context . functionJsonPath ) ) ; } return ! ! this . _functionJson . bindings . find ( b => b . name === val ) ; } catch { return false ; } } }