import { BaseCommand } from './baseCommand.js' import * as recast from 'recast' import { getChecklyConfigFile } from '../services/checkly-config-loader.js' import { loadPlaywrightConfig } from '../playwright/playwright-config-loader.js' import path from 'path' import { ux } from '@oclif/core' import PlaywrightConfigTemplate from '../playwright/playwright-config-template.js' import { addOrReplaceItem, findPropertyByName, reWriteChecklyConfigFile } from '../helpers/write-config-helpers.js' import * as acornParser from '../helpers/recast-acorn-parser.js' export default class SyncPlaywright extends BaseCommand { static hidden = false static idempotent = true static description = 'Copy Playwright config into the Checkly config file.' async run (): Promise { // oclif renders the spinner on stderr by default, keeping stdout clean. ux.action.start('Syncing Playwright config to the Checkly config file') const configFile = await getChecklyConfigFile() if (!configFile) { return this.handleError('Could not find a checkly config file') } const checklyAst = recast.parse(configFile.checklyConfig, { parser: acornParser }) const config = await loadPlaywrightConfig() if (!config) { return this.handleError('Could not find any playwright.config file.') } const checksAst = findPropertyByName(checklyAst, 'checks') if (!checksAst) { return this.handleError('Unable to automatically sync your config file. This can happen if your Checkly config is ' + 'built using helper functions or other JS/TS features. You can still manually set Playwright config values in ' + 'your Checkly config: https://www.checklyhq.com/docs/cli/constructs-reference/#project') } const pwtConfig = new PlaywrightConfigTemplate(config).getConfigTemplate() const pwtConfigAst = findPropertyByName(recast.parse(pwtConfig, { parser: acornParser }), 'playwrightConfig') addOrReplaceItem(checksAst.value, pwtConfigAst, 'playwrightConfig') const checklyConfigData = recast.print(checklyAst, { tabWidth: 2 }).code const dir = path.resolve(path.dirname(configFile.fileName)) await reWriteChecklyConfigFile(checklyConfigData, configFile.fileName, dir) ux.action.stop('✅ ') this.log('Successfully updated Checkly config file') this.exit(0) } private handleError (message: string) { ux.action.stop('❌') this.logToStderr(message) this.exit(1) } }