import { IPipe } from './IPipe' ; import { InlinePowerShell } from './PipeDefinitions/InlinePowerShell' ; import { EscapeDoubleQuotes } from './PipeDefinitions/EscapeDoubleQuotes' ; const RegisteredPipes = [ new EscapeDoubleQuotes ( ) , new InlinePowerShell ( ) , ] ; export interface IPipeFactory { get ( pipeName ) : IPipe ; } export class PipeFactory implements IPipeFactory { private readonly pipes = new Map < string , IPipe > ( ) ; constructor ( pipes = RegisteredPipes ) { if ( pipes . some ( ( pipe ) => ! pipe ) ) { throw new Error ( 'undefined pipe in list' ) ; } for ( const pipe of pipes ) { this . registerPipe ( pipe ) ; } } public get ( pipeName ) { validatePipeName ( pipeName ) ; if ( ! this . pipes . has ( pipeName ) ) { throw new Error ( `Unknown pipe: "${ pipeName }"` ) ; } return this . pipes . get ( pipeName ) ; } private registerPipe ( pipe ) { validatePipeName ( pipe . name ) ; if ( this . pipes . has ( pipe . name ) ) { throw new Error ( `Pipe name must be unique: "${ pipe . name }"` ) ; } this . pipes . set ( pipe . name , pipe ) ; } } function validatePipeName ( name ) { if ( ! name ) { throw new Error ( 'empty pipe name' ) ; } if ( ! /^[a-z][A-Za-z]*$/ . test ( name ) ) { throw new Error ( `Pipe name should be camelCase: "${ name }"` ) ; } }