forked from cucumber/cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
133 lines (124 loc) · 4.54 KB
/
builder.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import getColorFns from './get_color_fns'
import JavascriptSnippetSyntax from './step_definition_snippet_builder/javascript_snippet_syntax'
import JsonFormatter from './json_formatter'
import MessageFormatter from './message_formatter'
import path from 'path'
import ProgressBarFormatter from './progress_bar_formatter'
import ProgressFormatter from './progress_formatter'
import RerunFormatter from './rerun_formatter'
import SnippetsFormatter from './snippets_formatter'
import StepDefinitionSnippetBuilder from './step_definition_snippet_builder'
import SummaryFormatter from './summary_formatter'
import UsageFormatter from './usage_formatter'
import UsageJsonFormatter from './usage_json_formatter'
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
import Formatter, { IFormatterCleanupFn, IFormatterLogFn } from '.'
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
import { EventEmitter } from 'events'
import EventDataCollector from './helpers/event_data_collector'
import { Writable as WritableStream } from 'stream'
import { IParsedArgvFormatOptions } from '../cli/argv_parser'
import { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax'
import HtmlFormatter from './html_formatter'
interface IGetStepDefinitionSnippetBuilderOptions {
cwd: string
snippetInterface?: SnippetInterface
snippetSyntax?: string
supportCodeLibrary: ISupportCodeLibrary
}
export interface IBuildOptions {
cwd: string
eventBroadcaster: EventEmitter
eventDataCollector: EventDataCollector
log: IFormatterLogFn
parsedArgvOptions: IParsedArgvFormatOptions
stream: WritableStream
cleanup: IFormatterCleanupFn
supportCodeLibrary: ISupportCodeLibrary
}
const FormatterBuilder = {
build(type: string, options: IBuildOptions): Formatter {
const FormatterConstructor = FormatterBuilder.getConstructorByType(
type,
options.cwd
)
const colorFns = getColorFns(options.parsedArgvOptions.colorsEnabled)
const snippetBuilder = FormatterBuilder.getStepDefinitionSnippetBuilder({
cwd: options.cwd,
snippetInterface: options.parsedArgvOptions.snippetInterface,
snippetSyntax: options.parsedArgvOptions.snippetSyntax,
supportCodeLibrary: options.supportCodeLibrary,
})
return new FormatterConstructor({
colorFns,
snippetBuilder,
...options,
})
},
getConstructorByType(type: string, cwd: string): typeof Formatter {
switch (type) {
case 'json':
return JsonFormatter
case 'message':
return MessageFormatter
case 'html':
return HtmlFormatter
case 'progress':
return ProgressFormatter
case 'progress-bar':
return ProgressBarFormatter
case 'rerun':
return RerunFormatter
case 'snippets':
return SnippetsFormatter
case 'summary':
return SummaryFormatter
case 'usage':
return UsageFormatter
case 'usage-json':
return UsageJsonFormatter
default:
return FormatterBuilder.loadCustomFormatter(type, cwd)
}
},
getStepDefinitionSnippetBuilder({
cwd,
snippetInterface,
snippetSyntax,
supportCodeLibrary,
}: IGetStepDefinitionSnippetBuilderOptions) {
if (doesNotHaveValue(snippetInterface)) {
snippetInterface = SnippetInterface.Synchronous
}
let Syntax = JavascriptSnippetSyntax
if (doesHaveValue(snippetSyntax)) {
const fullSyntaxPath = path.resolve(cwd, snippetSyntax)
Syntax = require(fullSyntaxPath) // eslint-disable-line @typescript-eslint/no-var-requires
}
return new StepDefinitionSnippetBuilder({
snippetSyntax: new Syntax(snippetInterface),
parameterTypeRegistry: supportCodeLibrary.parameterTypeRegistry,
})
},
loadCustomFormatter(customFormatterPath: string, cwd: string) {
let CustomFormatter = null
if (customFormatterPath.startsWith('.')) {
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
} else {
CustomFormatter = require(customFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
}
if (typeof CustomFormatter === 'function') {
return CustomFormatter
} else if (
doesHaveValue(CustomFormatter) &&
typeof CustomFormatter.default === 'function'
) {
return CustomFormatter.default
}
throw new Error(
`Custom formatter (${customFormatterPath}) does not export a function`
)
},
}
export default FormatterBuilder