-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.ts
201 lines (192 loc) · 6.45 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
import { EventDataCollector } from '../formatter/helpers'
import { getExpandedArgv, 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 bluebird from 'bluebird'
import ParallelRuntimeMaster from '../runtime/parallel/master'
import Runtime from '../runtime'
import supportCodeLibraryBuilder from '../support_code_library_builder'
import { IdGenerator } from 'cucumber-messages'
import { IFormatterStream } from '../formatter'
import { WriteStream as TtyWriteStream } from 'tty'
import { doesNotHaveValue } from '../value_checker'
import Gherkin from 'gherkin'
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
import { IParsedArgvFormatOptions } from './argv_parser'
import { WriteStream } from 'fs'
const { incrementing, 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 ConfigurationBuilder.build({ argv: fullArgv, cwd: this.cwd })
}
async initializeFormatters({
eventBroadcaster,
eventDataCollector,
formatOptions,
formats,
supportCodeLibrary,
}: IInitializeFormattersRequest): Promise<() => Promise<void>> {
const streamsToClose: WriteStream[] = []
await bluebird.map(formats, async ({ type, outputTo }) => {
let stream: IFormatterStream = this.stdout
if (outputTo !== '') {
const fd = await fs.open(path.resolve(this.cwd, outputTo), 'w')
stream = fs.createWriteStream(null, { fd })
streamsToClose.push(stream)
}
const typeOptions = {
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
log: stream.write.bind(stream),
parsedArgvOptions: formatOptions,
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 FormatterBuilder.build(type, typeOptions)
})
return async function() {
await bluebird.each(streamsToClose, stream =>
bluebird.promisify(stream.end.bind(stream))()
)
}
}
getSupportCodeLibrary({
newId,
supportCodeRequiredModules,
supportCodePaths,
}: IGetSupportCodeLibraryRequest): ISupportCodeLibrary {
supportCodeRequiredModules.map(module => require(module))
supportCodeLibraryBuilder.reset(this.cwd, newId)
supportCodePaths.forEach(codePath => require(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 =
configuration.predictableIds && configuration.parallel <= 1
? incrementing()
: uuid()
const supportCodeLibrary = 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,
})
const gherkinMessageStream = Gherkin.fromPaths(configuration.featurePaths, {
defaultDialect: configuration.featureDefaultLanguage,
newId,
})
const pickleIds = await parseGherkinMessageStream({
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
gherkinMessageStream,
order: configuration.order,
pickleFilter: new PickleFilter(configuration.pickleFilterOptions),
})
let success
if (configuration.parallel > 1) {
const parallelRuntimeMaster = new ParallelRuntimeMaster({
cwd: this.cwd,
eventBroadcaster,
eventDataCollector,
options: configuration.runtimeOptions,
pickleIds,
supportCodeLibrary,
supportCodePaths: configuration.supportCodePaths,
supportCodeRequiredModules: configuration.supportCodeRequiredModules,
})
await new Promise(resolve => {
parallelRuntimeMaster.run(configuration.parallel, s => {
success = s
resolve()
})
})
} else {
const runtime = new Runtime({
eventBroadcaster,
eventDataCollector,
options: configuration.runtimeOptions,
newId,
pickleIds,
supportCodeLibrary,
})
success = await runtime.start()
}
await cleanup()
return {
shouldExitImmediately: configuration.shouldExitImmediately,
success,
}
}
}