Skip to content

Commit cdb225f

Browse files
authored
chore: minor bug fixes/features for playwright checks (#1144)
* fix: allow passing root dit lock file detector * fix: pass detectors and proper lock file resolution pw native * feat: allow relative paths for playwright.config file * feat: allow passing custom config path when --create-check for pw-test * fix: pr comments * fix: glob for package.json only
1 parent 3ef604b commit cdb225f

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

packages/cli/src/commands/pw-test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ export default class PwTestCommand extends AuthCommand {
110110
'stream-logs': streamLogs,
111111
} = flags
112112
const { configDirectory, configFilenames } = splitConfigFilePath(configFilename)
113+
const pwPathFlag = this.getConfigPath(playwrightFlags)
113114
const {
114115
config: checklyConfig,
115116
constructs: checklyConfigConstructs,
116-
} = await loadChecklyConfig(configDirectory, configFilenames, false)
117-
const playwrightConfigPath = this.getConfigPath(playwrightFlags) ?? checklyConfig.checks?.playwrightConfigPath
117+
} = await loadChecklyConfig(configDirectory, configFilenames, false, pwPathFlag)
118+
const playwrightConfigPath = checklyConfig.checks?.playwrightConfigPath
118119
const dir = path.dirname(playwrightConfigPath || '.')
119120
const playwrightCheck = await PwTestCommand.createPlaywrightCheck(
120121
playwrightFlags,

packages/cli/src/services/check-parser/package-files/package-manager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ export const knownPackageManagers: PackageManagerDetector[] = [
364364

365365
export interface DetectOptions {
366366
detectors?: PackageManagerDetector[]
367+
root?: string
367368
}
368369

369370
export async function detectPackageManager (
@@ -393,6 +394,7 @@ export async function detectPackageManager (
393394
try {
394395
const { packageManager } = await detectNearestLockfile(dir, {
395396
detectors,
397+
root: options?.root,
396398
})
397399

398400
return packageManager
@@ -451,7 +453,7 @@ export async function detectNearestLockfile (
451453
const searchPaths: string[] = []
452454

453455
// Next, try to find a lockfile.
454-
for (const searchPath of lineage(dir)) {
456+
for (const searchPath of lineage(dir, { root: options?.root })) {
455457
try {
456458
searchPaths.push(searchPath)
457459

packages/cli/src/services/checkly-config-loader.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function getChecklyConfigFile (): Promise<{ checklyConfig: string,
146146

147147
export class ConfigNotFoundError extends Error {}
148148

149-
export async function loadChecklyConfig (dir: string, filenames = ['checkly.config.ts', 'checkly.config.mts', 'checkly.config.cts', 'checkly.config.js', 'checkly.config.mjs', 'checkly.config.cjs'], writeChecklyConfig: boolean = true): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
149+
export async function loadChecklyConfig (dir: string, filenames = ['checkly.config.ts', 'checkly.config.mts', 'checkly.config.cts', 'checkly.config.js', 'checkly.config.mjs', 'checkly.config.cjs'], writeChecklyConfig: boolean = true, playwrightConfigPath?: string): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
150150
let config: ChecklyConfig | undefined
151151
Session.loadingChecklyConfigFile = true
152152
Session.checklyConfigFileConstructs = []
@@ -161,7 +161,7 @@ export async function loadChecklyConfig (dir: string, filenames = ['checkly.conf
161161
break
162162
}
163163
if (!config) {
164-
config = await handleMissingConfig(dir, filenames, writeChecklyConfig)
164+
config = await handleMissingConfig(dir, filenames, writeChecklyConfig, playwrightConfigPath)
165165
}
166166
validateConfigFields(config, ['logicalId', 'projectName'] as const)
167167

@@ -178,9 +178,10 @@ async function handleMissingConfig (
178178
dir: string,
179179
filenames: string[],
180180
shouldWriteConfig: boolean = true,
181+
pwPath?: string,
181182
): Promise<ChecklyConfig> {
182183
const baseName = path.basename(dir)
183-
const playwrightConfigPath = findPlaywrightConfigPath(dir)
184+
const playwrightConfigPath = pwPath ?? findPlaywrightConfigPath(dir)
184185
if (playwrightConfigPath) {
185186
const checklyConfig = getDefaultChecklyConfig(baseName, `./${path.relative(dir, playwrightConfigPath)}`)
186187
if (shouldWriteConfig) {

packages/cli/src/services/project-parser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ async function loadPlaywrightChecks (
125125
return
126126
}
127127

128+
// Resolve the playwrightConfigPath relative to the project directory
129+
const resolvedPlaywrightConfigPath = path.resolve(directory, playwrightConfigPath)
130+
128131
if (playwrightChecks?.length) {
129132
try {
130133
setCheckFilePaths(playwrightConfigPath, directory)
131134
for (const playwrightCheckProps of playwrightChecks) {
132135
// eslint-disable-next-line @typescript-eslint/no-unused-vars
133136
const playwrightCheck = new PlaywrightCheck(playwrightCheckProps.logicalId, {
134137
...playwrightCheckProps,
135-
playwrightConfigPath,
138+
playwrightConfigPath: resolvedPlaywrightConfigPath,
136139
include,
137140
})
138141
}
@@ -142,11 +145,11 @@ async function loadPlaywrightChecks (
142145
} else {
143146
try {
144147
setCheckFilePaths(playwrightConfigPath, directory)
145-
const basePath = path.basename(playwrightConfigPath)
148+
const basePath = path.basename(resolvedPlaywrightConfigPath)
146149
// eslint-disable-next-line @typescript-eslint/no-unused-vars
147150
const playwrightCheck = new PlaywrightCheck(basePath, {
148151
name: basePath,
149-
playwrightConfigPath,
152+
playwrightConfigPath: resolvedPlaywrightConfigPath,
150153
include,
151154
})
152155
} finally {

packages/cli/src/services/util.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import { readFile } from 'fs/promises'
2020
import { createHash } from 'crypto'
2121
import { Session } from '../constructs'
2222
import semver from 'semver'
23-
import { detectNearestLockfile } from './check-parser/package-files/package-manager'
23+
import {
24+
detectNearestLockfile,
25+
NpmDetector,
26+
PNpmDetector,
27+
YarnDetector,
28+
} from './check-parser/package-files/package-manager'
2429

2530
export interface GitInformation {
2631
commitId: string
@@ -195,6 +200,10 @@ export async function bundlePlayWrightProject (
195200
}> {
196201
const dir = path.resolve(path.dirname(playwrightConfig))
197202
const filePath = path.resolve(dir, playwrightConfig)
203+
const supportedDetectors = [new NpmDetector(), new YarnDetector(), new PNpmDetector()]
204+
const { lockfile } = await detectNearestLockfile(dir, { detectors: supportedDetectors, root: Session.basePath })
205+
206+
// No need of loading everything if there is no lockfile
198207
const pwtConfig = await Session.loadFile(filePath)
199208
const outputFolder = await fs.mkdtemp(path.join(os.tmpdir(), 'cli-'))
200209
const outputFile = path.join(outputFolder, 'playwright-project.tar.gz')
@@ -209,12 +218,10 @@ export async function bundlePlayWrightProject (
209218
archive.pipe(output)
210219

211220
const pwConfigParsed = new PlaywrightConfig(filePath, pwtConfig)
212-
const { lockfile } = await detectNearestLockfile(dir)
213-
214221
const [cacheHash, playwrightVersion] = await Promise.all([
215222
getCacheHash(lockfile),
216223
getPlaywrightVersion(dir),
217-
loadPlaywrightProjectFiles(dir, pwConfigParsed, include, archive),
224+
loadPlaywrightProjectFiles(dir, pwConfigParsed, include, archive, lockfile),
218225
])
219226

220227
await archive.finalize()
@@ -263,6 +270,7 @@ export async function getPlaywrightVersion (projectDir: string): Promise<string
263270

264271
export async function loadPlaywrightProjectFiles (
265272
dir: string, pwConfigParsed: PlaywrightConfig, include: string[], archive: Archiver,
273+
lockFile: string,
266274
) {
267275
const ignoredFiles = ['**/node_modules/**', '.git/**']
268276
const parser = new Parser({})
@@ -275,10 +283,11 @@ export async function loadPlaywrightProjectFiles (
275283
const relativePath = path.relative(dir, file)
276284
archive.file(file, { name: relativePath, mode })
277285
}
278-
// TODO: This code below should be a single glob
279-
archive.glob('**/package*.json', { cwd: path.join(dir, '/'), ignore: ignoredFiles }, { mode })
280-
archive.glob('**/pnpm*.yaml', { cwd: path.join(dir, '/'), ignore: ignoredFiles }, { mode })
281-
archive.glob('**/yarn.lock', { cwd: path.join(dir, '/'), ignore: ignoredFiles }, { mode })
286+
const lockFileDirName = path.dirname(lockFile)
287+
archive.file(lockFile, { name: path.basename(lockFile), mode })
288+
archive.file(path.join(lockFileDirName, 'package.json'), { name: 'package.json', mode })
289+
// handle workspaces
290+
archive.glob('**/package.json', { cwd: path.join(dir, '/'), ignore: ignoredFiles }, { mode })
282291
for (const includePattern of include) {
283292
archive.glob(includePattern, { cwd: path.join(dir, '/') }, { mode })
284293
}

0 commit comments

Comments
 (0)