-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.ts
258 lines (244 loc) · 8.06 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { EventDataCollector } from '../formatter/helpers'
import {
emitMetaMessage,
emitSupportCodeMessages,
getExpandedArgv,
isJavaScript,
parseGherkinMessageStream,
} from './helpers'
import { validateInstall } from './install_validator'
import * as I18n from './i18n'
import ConfigurationBuilder, {
IConfiguration,
IConfigurationFormat,
} from './configuration_builder'
import { EventEmitter } from 'events'
import FormatterBuilder from '../formatter/builder'
import fs from 'mz/fs'
import path from 'path'
import PickleFilter from '../pickle_filter'
import ParallelRuntimeCoordinator from '../runtime/parallel/coordinator'
import Runtime from '../runtime'
import supportCodeLibraryBuilder from '../support_code_library_builder'
import { IdGenerator } from '@cucumber/messages'
import Formatter, { IFormatterStream } from '../formatter'
import { WriteStream as TtyWriteStream } from 'tty'
import { doesNotHaveValue } from '../value_checker'
import { GherkinStreams } from '@cucumber/gherkin-streams'
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
import { IParsedArgvFormatOptions } from './argv_parser'
import HttpStream from '../formatter/http_stream'
import { promisify } from 'util'
import { Writable } from 'stream'
import { pathToFileURL } from 'url'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { importer } = require('../importer')
const { uuid } = IdGenerator
export interface ICliRunResult {
shouldExitImmediately: boolean
success: boolean
}
interface IInitializeFormattersRequest {
eventBroadcaster: EventEmitter
eventDataCollector: EventDataCollector
formatOptions: IParsedArgvFormatOptions
formats: IConfigurationFormat[]
supportCodeLibrary: ISupportCodeLibrary
}
interface IGetSupportCodeLibraryRequest {
newId: IdGenerator.NewId
supportCodeRequiredModules: string[]
supportCodePaths: string[]
}
export default class Cli {
private readonly argv: string[]
private readonly cwd: string
private readonly stdout: IFormatterStream
constructor({
argv,
cwd,
stdout,
}: {
argv: string[]
cwd: string
stdout: IFormatterStream
}) {
this.argv = argv
this.cwd = cwd
this.stdout = stdout
}
async getConfiguration(): Promise<IConfiguration> {
const fullArgv = await getExpandedArgv({
argv: this.argv,
cwd: this.cwd,
})
return await ConfigurationBuilder.build({
argv: fullArgv,
cwd: this.cwd,
})
}
async initializeFormatters({
eventBroadcaster,
eventDataCollector,
formatOptions,
formats,
supportCodeLibrary,
}: IInitializeFormattersRequest): Promise<() => Promise<void>> {
const formatters: Formatter[] = await Promise.all(
formats.map(async ({ type, outputTo }) => {
let stream: IFormatterStream = this.stdout
if (outputTo !== '') {
if (outputTo.match(/^https?:\/\//) !== null) {
const headers: { [key: string]: string } = {}
if (process.env.CUCUMBER_PUBLISH_TOKEN !== undefined) {
headers.Authorization = `Bearer ${process.env.CUCUMBER_PUBLISH_TOKEN}`
}
stream = new HttpStream(outputTo, 'GET', headers)
const readerStream = new Writable({
objectMode: true,
write: function (responseBody: string, encoding, writeCallback) {
console.error(responseBody)
writeCallback()
},
})
stream.pipe(readerStream)
} else {
const fd = await fs.open(path.resolve(this.cwd, outputTo), 'w')
stream = fs.createWriteStream(null, { fd })
}
}
stream.on('error', (error) => {
console.error(error.message)
process.exit(1)
})
const typeOptions = {
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
log: stream.write.bind(stream),
parsedArgvOptions: formatOptions,
stream,
cleanup:
stream === this.stdout
? async () => await Promise.resolve()
: promisify<any>(stream.end.bind(stream)),
supportCodeLibrary,
}
if (doesNotHaveValue(formatOptions.colorsEnabled)) {
typeOptions.parsedArgvOptions.colorsEnabled = (
stream as TtyWriteStream
).isTTY
}
if (type === 'progress-bar' && !(stream as TtyWriteStream).isTTY) {
const outputToName = outputTo === '' ? 'stdout' : outputTo
console.warn(
`Cannot use 'progress-bar' formatter for output to '${outputToName}' as not a TTY. Switching to 'progress' formatter.`
)
type = 'progress'
}
return await FormatterBuilder.build(type, typeOptions)
})
)
return async function () {
await Promise.all(formatters.map(async (f) => await f.finished()))
}
}
async getSupportCodeLibrary({
newId,
supportCodeRequiredModules,
supportCodePaths,
}: IGetSupportCodeLibraryRequest): Promise<ISupportCodeLibrary> {
supportCodeRequiredModules.map((module) => require(module))
supportCodeLibraryBuilder.reset(this.cwd, newId)
for (const codePath of supportCodePaths) {
if (supportCodeRequiredModules.length || !isJavaScript(codePath)) {
require(codePath)
} else {
await importer(pathToFileURL(codePath))
}
}
return supportCodeLibraryBuilder.finalize()
}
async run(): Promise<ICliRunResult> {
await validateInstall(this.cwd)
const configuration = await this.getConfiguration()
if (configuration.listI18nLanguages) {
this.stdout.write(I18n.getLanguages())
return { shouldExitImmediately: true, success: true }
}
if (configuration.listI18nKeywordsFor !== '') {
this.stdout.write(I18n.getKeywords(configuration.listI18nKeywordsFor))
return { shouldExitImmediately: true, success: true }
}
const newId = uuid()
const supportCodeLibrary = await this.getSupportCodeLibrary({
newId,
supportCodePaths: configuration.supportCodePaths,
supportCodeRequiredModules: configuration.supportCodeRequiredModules,
})
const eventBroadcaster = new EventEmitter()
const eventDataCollector = new EventDataCollector(eventBroadcaster)
const cleanup = await this.initializeFormatters({
eventBroadcaster,
eventDataCollector,
formatOptions: configuration.formatOptions,
formats: configuration.formats,
supportCodeLibrary,
})
await emitMetaMessage(eventBroadcaster)
const gherkinMessageStream = GherkinStreams.fromPaths(
configuration.featurePaths,
{
defaultDialect: configuration.featureDefaultLanguage,
newId,
relativeTo: this.cwd,
}
)
let pickleIds: string[] = []
if (configuration.featurePaths.length > 0) {
pickleIds = await parseGherkinMessageStream({
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
gherkinMessageStream,
order: configuration.order,
pickleFilter: new PickleFilter(configuration.pickleFilterOptions),
})
}
emitSupportCodeMessages({
eventBroadcaster,
supportCodeLibrary,
newId,
})
let success
if (configuration.parallel > 1) {
const parallelRuntimeCoordinator = new ParallelRuntimeCoordinator({
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
options: configuration.runtimeOptions,
newId,
pickleIds,
supportCodeLibrary,
supportCodePaths: configuration.supportCodePaths,
supportCodeRequiredModules: configuration.supportCodeRequiredModules,
})
success = await parallelRuntimeCoordinator.run(configuration.parallel)
} else {
const runtime = new Runtime({
eventBroadcaster,
eventDataCollector,
options: configuration.runtimeOptions,
newId,
pickleIds,
supportCodeLibrary,
})
success = await runtime.start()
}
await cleanup()
return {
shouldExitImmediately: configuration.shouldExitImmediately,
success,
}
}
}