-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
configuration_builder.ts
189 lines (176 loc) · 5.76 KB
/
configuration_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
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
import _ from 'lodash'
import ArgvParser, {
IParsedArgvOptions,
IParsedArgvFormatOptions,
} from './argv_parser'
import fs from 'mz/fs'
import path from 'path'
import OptionSplitter from './option_splitter'
import bluebird from 'bluebird'
import glob from 'glob'
import { promisify } from 'util'
import { IPickleFilterOptions } from '../pickle_filter'
import { IRuntimeOptions } from '../runtime'
export interface IConfigurationFormat {
outputTo: string
type: string
}
export interface IConfiguration {
featureDefaultLanguage: string
featurePaths: string[]
formats: IConfigurationFormat[]
formatOptions: IParsedArgvFormatOptions
listI18nKeywordsFor: string
listI18nLanguages: boolean
order: string
parallel: number
pickleFilterOptions: IPickleFilterOptions
predictableIds: boolean
profiles: string[]
runtimeOptions: IRuntimeOptions
shouldExitImmediately: boolean
supportCodePaths: string[]
supportCodeRequiredModules: string[]
}
export interface INewConfigurationBuilderOptions {
argv: string[]
cwd: string
}
export default class ConfigurationBuilder {
static async build(
options: INewConfigurationBuilderOptions
): Promise<IConfiguration> {
const builder = new ConfigurationBuilder(options)
return builder.build()
}
private readonly cwd: string
private readonly args: string[]
private readonly options: IParsedArgvOptions
constructor({ argv, cwd }: INewConfigurationBuilderOptions) {
this.cwd = cwd
const parsedArgv = ArgvParser.parse(argv)
this.args = parsedArgv.args
this.options = parsedArgv.options
}
async build(): Promise<IConfiguration> {
const listI18nKeywordsFor = this.options.i18nKeywords
const listI18nLanguages = this.options.i18nLanguages
const unexpandedFeaturePaths = await this.getUnexpandedFeaturePaths()
let featurePaths: string[] = []
let supportCodePaths: string[] = []
if (listI18nKeywordsFor === '' && !listI18nLanguages) {
featurePaths = await this.expandFeaturePaths(unexpandedFeaturePaths)
let unexpandedSupportCodePaths = this.options.require
if (unexpandedSupportCodePaths.length === 0) {
unexpandedSupportCodePaths = this.getFeatureDirectoryPaths(featurePaths)
}
supportCodePaths = await this.expandPaths(
unexpandedSupportCodePaths,
'.js'
)
}
return {
featureDefaultLanguage: this.options.language,
featurePaths,
formats: this.getFormats(),
formatOptions: this.options.formatOptions,
listI18nKeywordsFor,
listI18nLanguages,
order: this.options.order,
parallel: this.options.parallel,
pickleFilterOptions: {
cwd: this.cwd,
featurePaths: unexpandedFeaturePaths,
names: this.options.name,
tagExpression: this.options.tags,
},
predictableIds: this.options.predictableIds,
profiles: this.options.profile,
runtimeOptions: {
dryRun: this.options.dryRun,
failFast: this.options.failFast,
filterStacktraces: !this.options.backtrace,
retry: this.options.retry,
retryTagFilter: this.options.retryTagFilter,
strict: this.options.strict,
worldParameters: this.options.worldParameters,
},
shouldExitImmediately: this.options.exit,
supportCodePaths,
supportCodeRequiredModules: this.options.requireModule,
}
}
async expandPaths(
unexpandedPaths: string[],
defaultExtension: string
): Promise<string[]> {
const expandedPaths = await bluebird.map(
unexpandedPaths,
async unexpandedPath => {
const matches = await promisify(glob)(unexpandedPath, {
absolute: true,
cwd: this.cwd,
})
const expanded = await bluebird.map(matches, async match => {
if (path.extname(match) === '') {
return promisify(glob)(`${match}/**/*${defaultExtension}`)
}
return [match]
})
return _.flatten(expanded)
}
)
return _.flatten(expandedPaths).map(x => path.normalize(x))
}
async expandFeaturePaths(featurePaths: string[]): Promise<string[]> {
featurePaths = featurePaths.map(p => p.replace(/(:\d+)*$/g, '')) // Strip line numbers
return this.expandPaths(featurePaths, '.feature')
}
getFeatureDirectoryPaths(featurePaths: string[]): string[] {
const featureDirs = featurePaths.map(featurePath => {
let featureDir = path.dirname(featurePath)
let childDir: string
let parentDir = featureDir
while (childDir !== parentDir) {
childDir = parentDir
parentDir = path.dirname(childDir)
if (path.basename(parentDir) === 'features') {
featureDir = parentDir
break
}
}
return path.relative(this.cwd, featureDir)
})
return _.uniq(featureDirs)
}
getFormats(): IConfigurationFormat[] {
const mapping: { [key: string]: string } = { '': 'progress' }
this.options.format.forEach(format => {
const [type, outputTo] = OptionSplitter.split(format)
mapping[outputTo] = type
})
return _.map(mapping, (type, outputTo) => ({ outputTo, type }))
}
async getUnexpandedFeaturePaths(): Promise<string[]> {
if (this.args.length > 0) {
const nestedFeaturePaths = await bluebird.map(this.args, async arg => {
const filename = path.basename(arg)
if (filename[0] === '@') {
const filePath = path.join(this.cwd, arg)
const content = await fs.readFile(filePath, 'utf8')
return _.chain(content)
.split('\n')
.map(_.trim)
.compact()
.value()
}
return [arg]
})
const featurePaths = _.flatten(nestedFeaturePaths)
if (featurePaths.length > 0) {
return featurePaths
}
}
return ['features/**/*.feature']
}
}