-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.ts
More file actions
185 lines (157 loc) · 6.59 KB
/
config.ts
File metadata and controls
185 lines (157 loc) · 6.59 KB
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
import fs from 'node:fs/promises'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { parse, print, visit, types as t } from 'recast'
// @ts-ignore
import typescriptParser from 'recast/parsers/typescript'
const reporterIdentifierName = 'VscodeJsonReporter'
// This file is bundle as parser/ats.js at the package of vscode-webdriverio
// So, the correct reporter path is parent directory
const VSCODE_REPORTER_PATH = path.resolve(__dirname, '../reporter.cjs')
/**
* Create AST nodes using ast-types builders
*/
const b = t.builders
/**
* Since Windows cannot import by reporter file path due to issues with
* the `initializePlugin` method of wdio-utils, the policy is to create a temporary configuration file.
*/
export async function createTempConfigFile(filename: string, outDir: string) {
const source = await fs.readFile(filename, { encoding: 'utf8' })
const ast = parse(source, {
parser: typescriptParser,
})
const reporterIdentifier = b.identifier(reporterIdentifierName)
const reporterConfigIdentifier = b.identifier(`${reporterIdentifierName}.default || ${reporterIdentifierName}`)
const reporterElement = b.arrayExpression([
reporterConfigIdentifier,
b.objectExpression([
b.property('init', b.identifier('stdout'), b.literal(true)),
b.property('init', b.identifier('outputDir'), b.literal(outDir)),
]),
])
let hasReporterImport = false
function addOrUpdateReporters(configObject: any) {
if (!t.namedTypes.ObjectExpression.check(configObject)) {
return
}
// Find existing reporters property
let reportersProp = null
for (let i = 0; i < configObject.properties.length; i++) {
const prop = configObject.properties[i]
// Check for both Property and ObjectProperty nodes
if (t.namedTypes.Property.check(prop) || t.namedTypes.ObjectProperty?.check?.(prop)) {
const isReportersKey =
(t.namedTypes.Identifier.check(prop.key) && prop.key.name === 'reporters') ||
(t.namedTypes.Literal.check(prop.key) && prop.key.value === 'reporters')
if (isReportersKey) {
reportersProp = prop
break
}
}
}
if (reportersProp && t.namedTypes.ArrayExpression.check(reportersProp.value)) {
// Add to existing reporters array
reportersProp.value.elements.push(reporterElement)
} else if (reportersProp) {
// Replace existing non-array reporters with array including existing value
const existingValue = reportersProp.value
//@ts-ignore
reportersProp.value = b.arrayExpression([existingValue, reporterElement])
} else {
// Add new reporters property
configObject.properties.push(
b.property('init', b.identifier('reporters'), b.arrayExpression([reporterElement]))
)
}
}
visit(ast, {
visitImportDeclaration(path) {
const { source, specifiers } = path.node
if (
source.value === pathToFileURL(VSCODE_REPORTER_PATH).href &&
specifiers &&
//@ts-ignore
specifiers.some(
//@ts-ignore
(s: any) => t.namedTypes.ImportDefaultSpecifier.check(s) && s.local.name === reporterIdentifierName
)
) {
hasReporterImport = true
}
this.traverse(path)
},
visitExportNamedDeclaration(path) {
const decl = path.node.declaration
if (t.namedTypes.VariableDeclaration.check(decl)) {
const first = decl.declarations[0]
if (t.namedTypes.VariableDeclarator.check(first)) {
const id = first.id
const init = first.init
if (t.namedTypes.Identifier.check(id) && id.name === 'config') {
if (t.namedTypes.ObjectExpression.check(init)) {
addOrUpdateReporters(init)
} else if (
t.namedTypes.CallExpression.check(init) &&
init.arguments.length > 0 &&
t.namedTypes.ObjectExpression.check(init.arguments[0])
) {
const configObject = init.arguments[0]
addOrUpdateReporters(configObject)
}
}
}
}
this.traverse(path)
},
visitAssignmentExpression(path) {
const { left, right } = path.node
if (!left || !right) {
this.traverse(path)
return
}
if (
t.namedTypes.MemberExpression.check(left) &&
t.namedTypes.Identifier.check(left.object) &&
t.namedTypes.Identifier.check(left.property) &&
t.namedTypes.ObjectExpression.check(right)
) {
const leftName = `${left.object.name}.${left.property.name}`
if (['module.exports', 'exports.config'].includes(leftName)) {
addOrUpdateReporters(right)
}
}
this.traverse(path)
},
visitCallExpression(path) {
const node = path.node
if (
t.namedTypes.CallExpression.check(node) &&
t.namedTypes.Identifier.check(node.callee) &&
node.callee.name === 'require' &&
node.arguments.length === 1 &&
t.namedTypes.Literal.check(node.arguments[0]) &&
typeof node.arguments[0].value === 'string' &&
node.arguments[0].value === pathToFileURL(VSCODE_REPORTER_PATH).href
) {
hasReporterImport = true
}
this.traverse(path)
},
})
if (!hasReporterImport) {
const importedModule = b.importDeclaration(
[b.importDefaultSpecifier(reporterIdentifier)],
b.literal(pathToFileURL(VSCODE_REPORTER_PATH).href)
)
ast.program.body.unshift(importedModule)
}
const output = print(ast).code
const ext = path.extname(filename)
const _filename = path.join(path.dirname(filename), `wdio-vscode-${new Date().getTime()}${ext}`)
await fs.writeFile(_filename, output)
return _filename
}
export function isWindows() {
return process.platform === 'win32'
}