import { PassThrough , Readable } from 'stream' ; import * as messages from '@cucumber/messages' ; import ParserMessageStream from './ParserMessageStream' ; import SourceMessageStream from './SourceMessageStream' ; import fs from 'fs' ; import { IGherkinOptions } from '@cucumber/gherkin' ; import makeGherkinOptions from './makeGherkinOptions' ; export interface IGherkinStreamOptions extends IGherkinOptions { relativeTo ? : string ; } function fromPaths ( paths , options ) { const pathsCopy = paths . slice ( ) ; options = makeGherkinOptions ( options ) ; const combinedMessageStream = new PassThrough ( { writableObjectMode : true , readableObjectMode : true , } ) ; function pipeSequentially ( ) { const path = pathsCopy . shift ( ) ; if ( path !== undefined ) { const parserMessageStream = new ParserMessageStream ( options ) ; parserMessageStream . on ( 'end' , ( ) => { pipeSequentially ( ) ; } ) ; const end = pathsCopy . length === 0 ; fs . createReadStream ( path , { encoding : 'utf-8' } ) . on ( 'error' , ( err ) => combinedMessageStream . emit ( 'error' , err ) ) . pipe ( new SourceMessageStream ( path , options . relativeTo ) ) . on ( 'error' , ( err ) => combinedMessageStream . emit ( 'error' , err ) ) . pipe ( parserMessageStream ) . on ( 'error' , ( err ) => combinedMessageStream . emit ( 'error' , err ) ) . pipe ( combinedMessageStream , { end } ) ; } } pipeSequentially ( ) ; return combinedMessageStream ; } function fromSources ( envelopes , options ) { const envelopesCopy = envelopes . slice ( ) ; options = makeGherkinOptions ( options ) ; const combinedMessageStream = new PassThrough ( { writableObjectMode : true , readableObjectMode : true , } ) ; function pipeSequentially ( ) { const envelope = envelopesCopy . shift ( ) ; if ( envelope !== undefined && envelope . source ) { const parserMessageStream = new ParserMessageStream ( options ) ; parserMessageStream . pipe ( combinedMessageStream , { end : envelopesCopy . length === 0 , } ) ; parserMessageStream . on ( 'end' , pipeSequentially ) ; parserMessageStream . end ( envelope ) ; } } pipeSequentially ( ) ; return combinedMessageStream ; } export default { fromPaths , fromSources , } ;