-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcheckly-config-loader.ts
More file actions
236 lines (220 loc) · 6.79 KB
/
Copy pathcheckly-config-loader.ts
File metadata and controls
236 lines (220 loc) · 6.79 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
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
import * as path from 'path'
import fs from 'node:fs/promises'
import { findPlaywrightConfigPath, getDefaultChecklyConfig, writeChecklyConfigFile } from './util.js'
import { CheckProps, RuntimeCheckProps } from '../constructs/check.js'
import { PlaywrightCheckProps } from '../constructs/playwright-check.js'
import { Session } from '../constructs/index.js'
import { Construct } from '../constructs/construct.js'
import type { Region } from '../index.js'
import { ReporterType } from '../reporters/reporter.js'
import { PlaywrightConfig } from '../constructs/playwright-config.js'
import { FileLoader } from '../loader/index.js'
export type CheckConfigDefaults =
Pick<CheckProps,
| 'activated'
| 'alertChannels'
| 'alertEscalationPolicy'
| 'doubleCheck'
| 'frequency'
| 'locations'
| 'muted'
| 'privateLocations'
| 'retryStrategy'
| 'shouldFail'
| 'tags'
>
& Pick<RuntimeCheckProps,
| 'environmentVariables'
| 'runtimeId'
>
// This is used by BrowserChecks and MultiStepChecks.
& { playwrightConfig?: PlaywrightConfig }
export type PlaywrightSlimmedProp = Pick<PlaywrightCheckProps, 'name' | 'activated'
| 'muted' | 'shouldFail' | 'locations' | 'tags' | 'frequency' | 'environmentVariables'
| 'alertChannels' | 'privateLocations' | 'alertEscalationPolicy'
| 'pwProjects' | 'pwTags' | 'installCommand' | 'testCommand' | 'group' | 'groupName' | 'runParallel'
| 'engine' | 'workingDir'> & { logicalId: string, playwrightConfigPath?: string }
export type ChecklyConfig = {
/**
* Friendly name for your project.
*/
projectName: string
/**
* Unique project identifier.
*/
logicalId: string
/**
* Git repository URL.
*/
repoUrl?: string
/**
* Checks default configuration properties.
*/
checks?: CheckConfigDefaults & {
/**
* Glob pattern where the CLI looks for files containing Check constructs, i.e. all `.checks.ts` files
*/
checkMatch?: string | string[]
/**
* List of glob patterns with directories to ignore.
*/
ignoreDirectoriesMatch?: string[]
playwrightConfig?: PlaywrightConfig
/**
* Browser checks default configuration properties.
*/
browserChecks?: CheckConfigDefaults & {
/**
* Glob pattern where the CLI looks for Playwright test files, i.e. all `.spec.ts` files
*/
testMatch?: string | string[]
}
/**
* Multistep checks default configuration properties.
*/
multiStepChecks?: CheckConfigDefaults & {
/**
* Glob pattern where the CLI looks for Playwright test files, i.e. all `.spec.ts` files
*/
testMatch?: string | string[]
}
/**
* Playwright config path to be used during bundling and playwright config parsing
*/
playwrightConfigPath?: string
/**
* Extra files to be included into the playwright bundle
*/
include?: string | string[]
/**
* List of playwright checks that use the defined playwright config path
*/
playwrightChecks?: PlaywrightSlimmedProp[]
}
/**
* CLI default configuration properties.
*/
cli?: {
runLocation?: keyof Region
privateRunLocation?: string
verbose?: boolean
reporters?: ReporterType[]
retries?: number
loader?: FileLoader
}
}
function isString (obj: any) {
return (Object.prototype.toString.call(obj) === '[object String]')
}
export async function getChecklyConfigFile (): Promise<{ checklyConfig: string, fileName: string } | undefined> {
const filenames = [
'checkly.config.ts',
'checkly.config.mts',
'checkly.config.cts',
'checkly.config.js',
'checkly.config.mjs',
'checkly.config.cjs',
]
let config
for (const configFile of filenames) {
const dir = path.resolve(path.dirname(configFile))
const configFilePath = path.resolve(dir, configFile)
try {
await fs.access(configFilePath, fs.constants.R_OK)
} catch {
continue
}
const file = await fs.readFile(configFilePath)
if (file) {
config = {
checklyConfig: file.toString(),
fileName: configFile,
}
break
}
}
return config
}
export class ConfigNotFoundError extends Error {
searchPaths: string[]
configFiles: string[]
constructor (searchPaths: string[], configFiles: string[], options?: ErrorOptions) {
const message = `Unable to detect a Checkly configuration file in any of the following paths:`
+ `\n\n`
+ `${searchPaths.map(searchPath => ` ${searchPath}`).join('\n')}`
+ `\n\n`
+ `Configuration files we looked for:`
+ `\n\n`
+ `${configFiles.map(lockfile => ` ${lockfile}`).join('\n')}`
super(message, options)
this.name = 'ConfigNotFoundError'
this.searchPaths = searchPaths
this.configFiles = configFiles
}
}
export const defaultFilenames = [
'checkly.config.ts',
'checkly.config.mts',
'checkly.config.cts',
'checkly.config.js',
'checkly.config.mjs',
'checkly.config.cjs',
]
export async function loadChecklyConfig (
dir: string,
filenames = defaultFilenames,
writeChecklyConfig: boolean = true,
playwrightConfigPath?: string,
): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
Session.loadingChecklyConfigFile = true
try {
let config: ChecklyConfig | undefined
Session.checklyConfigFileConstructs = []
for (const filename of filenames) {
const filePath = path.join(dir, filename)
try {
await fs.access(filePath, fs.constants.R_OK)
} catch {
continue
}
config = await Session.loadFile<ChecklyConfig>(filePath)
break
}
if (!config) {
config = await handleMissingConfig(dir, filenames, writeChecklyConfig, playwrightConfigPath)
}
validateConfigFields(config, ['logicalId', 'projectName'] as const)
const constructs = Session.checklyConfigFileConstructs
Session.checklyConfigFileConstructs = []
if (config.cli?.loader) {
Session.loader = config.cli.loader
}
return { config, constructs }
} finally {
Session.loadingChecklyConfigFile = false
}
}
async function handleMissingConfig (
dir: string,
filenames: string[],
shouldWriteConfig: boolean = true,
pwPath?: string,
): Promise<ChecklyConfig> {
const baseName = path.basename(dir)
const playwrightConfigPath = pwPath ?? findPlaywrightConfigPath(dir)
if (playwrightConfigPath) {
const checklyConfig = getDefaultChecklyConfig(baseName, `./${path.relative(dir, playwrightConfigPath)}`)
if (shouldWriteConfig) {
await writeChecklyConfigFile(dir, checklyConfig)
}
return checklyConfig
}
throw new ConfigNotFoundError([dir], filenames)
}
function validateConfigFields (config: ChecklyConfig, fields: (keyof ChecklyConfig)[]): void {
for (const field of fields) {
if (!config?.[field] || !isString(config[field])) {
throw new Error(`Config object missing a ${field} as type string`)
}
}
}