import { ICodeBuilder } from './ICodeBuilder' ; const NewLine = '\n' ; const TotalFunctionSeparatorChars = 58 ; export abstract class CodeBuilder implements ICodeBuilder { private readonly lines = new Array < string > ( ) ; public get currentLine ( ) : number { return this . lines . length ; } public appendLine ( code ? ) { if ( ! code ) { this . lines . push ( '' ) ; return this ; } const lines = code . match ( /[^\r\n]+/g ) ; this . lines . push ( ... lines ) ; return this ; } public appendTrailingHyphensCommentLine ( totalRepeatHyphens = TotalFunctionSeparatorChars ) { return this . appendCommentLine ( '-' . repeat ( totalRepeatHyphens ) ) ; } public appendCommentLine ( commentLine ? ) { this . lines . push ( `${ this . getCommentDelimiter ( ) } ${ commentLine }` ) ; return this ; } public appendFunction ( name , code ) { if ( ! name ) { throw new Error ( 'name cannot be empty or null' ) ; } if ( ! code ) { throw new Error ( 'code cannot be empty or null' ) ; } return this . appendCommentLineWithHyphensAround ( name ) . appendLine ( this . writeStandardOut ( `--- ${ name }` ) ) . appendLine ( code ) . appendTrailingHyphensCommentLine ( ) ; } public appendCommentLineWithHyphensAround ( sectionName , totalRepeatHyphens = TotalFunctionSeparatorChars ) { if ( ! sectionName ) { throw new Error ( 'sectionName cannot be empty or null' ) ; } if ( sectionName . length >= totalRepeatHyphens ) { return this . appendCommentLine ( sectionName ) ; } const firstHyphens = '-' . repeat ( Math . floor ( ( totalRepeatHyphens - sectionName . length ) / 2 ) ) ; const secondHyphens = '-' . repeat ( Math . ceil ( ( totalRepeatHyphens - sectionName . length ) / 2 ) ) ; return this . appendTrailingHyphensCommentLine ( ) . appendCommentLine ( firstHyphens + sectionName + secondHyphens ) . appendTrailingHyphensCommentLine ( TotalFunctionSeparatorChars ) ; } public toString ( ) { return this . lines . join ( NewLine ) ; } protected abstract getCommentDelimiter ( ) ; protected abstract writeStandardOut ( text ) ; }