-
-
Notifications
You must be signed in to change notification settings - Fork 698
/
cli-main.tsx
105 lines (96 loc) · 2.36 KB
/
cli-main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { readFile } from 'fs'
import {
BinaryToMessageStream,
messages,
NdjsonToMessageStream,
} from 'cucumber-messages'
import React from 'react'
import program from 'commander'
import p from '../package.json'
import { pipeline, Transform, TransformCallback } from 'stream'
import { GherkinDocumentList } from 'cucumber-react'
import { renderToString } from 'react-dom/server'
import CucumberQuery from 'cucumber-query'
class CucumberHtmlStream extends Transform {
private readonly envelopes: messages.IEnvelope[] = []
constructor() {
super({ objectMode: true })
}
public _transform(
envelope: messages.IEnvelope,
encoding: string,
callback: TransformCallback
): void {
this.envelopes.push(envelope)
callback()
}
public _flush(callback: TransformCallback): void {
readFile(__dirname + '/../main.js', (err: Error, js: Buffer) => {
if (err) {
return callback(err)
}
const gherkinDocuments = this.envelopes
.filter(e => e.gherkinDocument)
.map(e => e.gherkinDocument)
const cucumberQuery = this.envelopes.reduce(
(q, e) => q.update(e),
new CucumberQuery()
)
this.push(`<!DOCTYPE html>
<html lang="en">
<head>
<title>Cucumber</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="content">
`)
this.push(
renderToString(
<GherkinDocumentList
gherkinDocuments={gherkinDocuments}
cucumberQuery={cucumberQuery}
/>
)
)
this.push(`
</div>
<script>
window.CUCUMBER_MESSAGES = ${JSON.stringify(this.envelopes)}
</script>
<script>
${js.toString('utf8')}
</script>
</body>
</html>
`)
callback()
})
}
}
program.version(p.version)
program.option(
'-f, --format <format>',
'output format: ndjson|protobuf',
'protobuf'
)
program.parse(process.argv)
const toMessageStream =
program.format === 'ndjson'
? new NdjsonToMessageStream(
messages.Envelope.fromObject.bind(messages.Envelope)
)
: new BinaryToMessageStream(
messages.Envelope.decodeDelimited.bind(messages.Envelope)
)
pipeline(
process.stdin,
toMessageStream,
new CucumberHtmlStream(),
process.stdout,
(err: any) => {
// tslint:disable-next-line:no-console
console.error(err)
process.exit(1)
}
)