Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Add support for NDJSON as input everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Nov 25, 2019
1 parent 113c5c6 commit ef35037
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
14 changes: 3 additions & 11 deletions src/cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import gherkin from '../index'
import { IGherkinOptions } from '../types'
import {
messages,
ProtobufBinaryStream,
ProtobufNdjsonStream,
} from 'cucumber-messages'
import { MessageToBinaryStream, MessageToNdjsonStream } from 'cucumber-messages'
import { Readable } from 'stream'
import { incrementing, uuid } from '../IdGenerator'

Expand Down Expand Up @@ -53,11 +49,7 @@ const messageStream =
: gherkin.fromPaths(paths, options)

const encodedStream = json
? messageStream.pipe(new ProtobufNdjsonStream())
: messageStream.pipe(
new ProtobufBinaryStream(
messages.Envelope.encodeDelimited.bind(messages.Envelope)
)
)
? messageStream.pipe(new MessageToNdjsonStream())
: messageStream.pipe(new MessageToBinaryStream())

encodedStream.pipe(process.stdout)
12 changes: 5 additions & 7 deletions src/external/GherkinExe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn, spawnSync } from 'child_process'
import { messages, ProtobufMessageStream } from 'cucumber-messages'
import { messages, BinaryToMessageStream } from 'cucumber-messages'
import { Readable } from 'stream'
import Dialect from '../Dialect'
import { IGherkinOptions } from '../types'
Expand Down Expand Up @@ -32,17 +32,15 @@ export default class GherkinExe {
const gherkin = spawn(this.gherkinExe, args, {
stdio: ['pipe', 'pipe', 'inherit'],
})
const protobufMessageStream = new ProtobufMessageStream(
const toMessageStream = new BinaryToMessageStream(
messages.Envelope.decodeDelimited.bind(messages.Envelope)
)
gherkin.on('error', err => {
protobufMessageStream.emit('error', err)
})
gherkin.stdout.pipe(protobufMessageStream)
gherkin.on('error', err => toMessageStream.emit('error', err))
gherkin.stdout.pipe(toMessageStream)
for (const envelope of this.envelopes) {
gherkin.stdin.write(messages.Envelope.encodeDelimited(envelope).finish())
}
gherkin.stdin.end()
return protobufMessageStream
return toMessageStream
}
}
24 changes: 11 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { gherkinOptions, IGherkinOptions } from './types'
import { PassThrough, Readable } from 'stream'
import { PassThrough, Readable, pipeline } from 'stream'
import ParserMessageStream from './stream/ParserMessageStream'
import fs from 'fs'
import SourceMessageStream from './stream/SourceMessageStream'
import { messages, ProtobufMessageStream } from 'cucumber-messages'
import { messages, BinaryToMessageStream } from 'cucumber-messages'
import DIALECTS from './gherkin-languages.json'
import Dialect from './Dialect'
import GherkinExe from './external/GherkinExe'
import { uuid, incrementing } from './IdGenerator'

export function fromStream(stream: Readable, options: IGherkinOptions = {}) {
return stream
.pipe(new ProtobufMessageStream(messages.Envelope.decodeDelimited))
.pipe(new ParserMessageStream(options))
return pipeline(
stream,
new BinaryToMessageStream(messages.Envelope.decodeDelimited),
new ParserMessageStream(options)
)
}

export function fromPaths(
Expand Down Expand Up @@ -47,10 +49,7 @@ export function fromPaths(
fs.createReadStream(path, { encoding: 'utf-8' })
.pipe(new SourceMessageStream(path))
.pipe(parserMessageStream)
.pipe(
combinedMessageStream,
{ end }
)
.pipe(combinedMessageStream, { end })
}
}
pipeSequentially()
Expand All @@ -77,10 +76,9 @@ export function fromSources(
const envelope = envelopes.shift()
if (envelope !== undefined && envelope.source) {
const parserMessageStream = new ParserMessageStream(options)
parserMessageStream.pipe(
combinedMessageStream,
{ end: envelopes.length === 0 }
)
parserMessageStream.pipe(combinedMessageStream, {
end: envelopes.length === 0,
})
parserMessageStream.on('end', pipeSequentially)
parserMessageStream.end(envelope)
}
Expand Down

0 comments on commit ef35037

Please sign in to comment.