-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
builder.ts
150 lines (140 loc) · 4.44 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import getColorFns from './get_color_fns'
import JavascriptSnippetSyntax from './step_definition_snippet_builder/javascript_snippet_syntax'
import path from 'path'
import StepDefinitionSnippetBuilder from './step_definition_snippet_builder'
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
import Formatter, {
FormatOptions,
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 { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax'
import { fileURLToPath, pathToFileURL } from 'url'
import Formatters from './helpers/formatters'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { importer } = require('../importer')
interface IGetStepDefinitionSnippetBuilderOptions {
cwd: string
snippetInterface?: SnippetInterface
snippetSyntax?: string
supportCodeLibrary: ISupportCodeLibrary
}
export interface IBuildOptions {
cwd: string
eventBroadcaster: EventEmitter
eventDataCollector: EventDataCollector
log: IFormatterLogFn
parsedArgvOptions: FormatOptions
stream: WritableStream
cleanup: IFormatterCleanupFn
supportCodeLibrary: ISupportCodeLibrary
}
const FormatterBuilder = {
async build(type: string, options: IBuildOptions): Promise<Formatter> {
const FormatterConstructor = await FormatterBuilder.getConstructorByType(
type,
options.cwd
)
const colorFns = getColorFns(options.parsedArgvOptions.colorsEnabled)
const snippetBuilder =
await FormatterBuilder.getStepDefinitionSnippetBuilder({
cwd: options.cwd,
snippetInterface: options.parsedArgvOptions.snippetInterface,
snippetSyntax: options.parsedArgvOptions.snippetSyntax,
supportCodeLibrary: options.supportCodeLibrary,
})
return new FormatterConstructor({
colorFns,
snippetBuilder,
...options,
})
},
async getConstructorByType(
type: string,
cwd: string
): Promise<typeof Formatter> {
const formatters: Record<string, typeof Formatter> =
Formatters.getFormatters()
return formatters[type]
? formatters[type]
: await FormatterBuilder.loadCustomClass('formatter', type, cwd)
},
async getStepDefinitionSnippetBuilder({
cwd,
snippetInterface,
snippetSyntax,
supportCodeLibrary,
}: IGetStepDefinitionSnippetBuilderOptions) {
if (doesNotHaveValue(snippetInterface)) {
snippetInterface = SnippetInterface.Synchronous
}
let Syntax = JavascriptSnippetSyntax
if (doesHaveValue(snippetSyntax)) {
Syntax = await FormatterBuilder.loadCustomClass(
'syntax',
snippetSyntax,
cwd
)
}
return new StepDefinitionSnippetBuilder({
snippetSyntax: new Syntax(snippetInterface),
parameterTypeRegistry: supportCodeLibrary.parameterTypeRegistry,
})
},
async loadCustomClass(
type: 'formatter' | 'syntax',
descriptor: string,
cwd: string
) {
let normalised: URL | string = descriptor
if (descriptor.startsWith('.')) {
normalised = pathToFileURL(path.resolve(cwd, descriptor))
} else if (descriptor.startsWith('file://')) {
normalised = new URL(descriptor)
}
let CustomClass = await FormatterBuilder.loadFile(normalised)
CustomClass = FormatterBuilder.resolveConstructor(CustomClass)
if (doesHaveValue(CustomClass)) {
return CustomClass
} else {
throw new Error(
`Custom ${type} (${descriptor}) does not export a function/class`
)
}
},
async loadFile(urlOrName: URL | string) {
let result
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
result = require(typeof urlOrName === 'string'
? urlOrName
: fileURLToPath(urlOrName))
} catch (error) {
if (error.code === 'ERR_REQUIRE_ESM') {
result = await importer(urlOrName)
} else {
throw error
}
}
return result
},
resolveConstructor(ImportedCode: any) {
if (doesNotHaveValue(ImportedCode)) {
return null
}
if (typeof ImportedCode === 'function') {
return ImportedCode
} else if (
typeof ImportedCode === 'object' &&
typeof ImportedCode.default === 'function'
) {
return ImportedCode.default
}
return null
},
}
export default FormatterBuilder