-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathplaywright.ts
More file actions
88 lines (81 loc) · 2.86 KB
/
playwright.ts
File metadata and controls
88 lines (81 loc) · 2.86 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
import * as chalk from 'chalk'
import { spinner } from '../utils/terminal'
import { loadPlaywrightConfig } from '../utils/directory'
import * as recast from 'recast'
import * as path from 'path'
import * as fs from 'fs'
import * as ora from 'ora'
import PlaywrightConfigTemplate from '../utils/playwright-config-template'
export async function copyPlaywrightConfig (dirPath: string, playwrightConfigFileName: string) {
const copySpinner = spinner('Copying your playwright config')
try {
const config = await loadPlaywrightConfig(path.join(dirPath, playwrightConfigFileName))
const pwtConfig = new PlaywrightConfigTemplate(config).getConfigTemplate()
if (!pwtConfig) {
return
}
const checklyConfig = getChecklyConfigFile()
if (!checklyConfig) {
return handleError(copySpinner, 'Could not find your checkly config file')
}
const checklyAst = recast.parse(checklyConfig.checklyConfig)
const checksAst = findPropertyByName(checklyAst, 'checks')
if (!checksAst) {
return handleError(copySpinner, 'Could not parse you checkly file correctly')
}
const pwtConfigAst = findPropertyByName(recast.parse(pwtConfig), 'playwrightConfig')
addOrReplacePlaywrightConfig(checksAst.value, pwtConfigAst)
fs.writeFileSync(path.join(dirPath, checklyConfig.fileName), recast.print(checklyAst, { tabWidth: 2 }).code)
} catch (e) {
handleError(copySpinner, e)
}
copySpinner.text = chalk.green('Playwright config copied!')
copySpinner.succeed()
}
function handleError (copySpinner: ora.Ora, message: string | unknown) {
copySpinner.text = chalk.red('Something went wrong when copying your playwrightConfig file')
copySpinner.fail()
// eslint-disable-next-line
console.error(message)
process.exit(1)
}
function getChecklyConfigFile (): {checklyConfig: string, fileName: string} | undefined {
const filenames: string[] = ['checkly.config.ts', 'checkly.config.js', 'checkly.config.mts', 'checkly.config.mjs']
let config
for (const configFile of filenames) {
const dir = path.resolve(path.dirname(configFile))
if (!fs.existsSync(path.resolve(dir, configFile))) {
continue
}
const file = fs.readFileSync(path.resolve(dir, configFile))
if (file) {
config = {
checklyConfig: file.toString(),
fileName: configFile,
}
break
}
}
return config
}
function findPropertyByName (ast: any, name: string):
recast.types.namedTypes.Property | undefined {
let node
recast.visit(ast, {
visitProperty (path: any) {
if (path.node.key.name === name) {
node = path.node
}
return false
},
})
return node
}
function addOrReplacePlaywrightConfig (ast: any, node: any) {
const playWrightConfig = findPropertyByName(ast, 'playwrightConfig')
if (playWrightConfig) {
playWrightConfig.value = node.value
} else {
ast.properties.push(node)
}
}