-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ability to copy playwright config into checkly config #919
Changes from 4 commits
862a91f
84bc436
455f376
07d511b
692235f
466c6b0
dc7a3fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||
import { BaseCommand } from './baseCommand' | ||||||
import * as recast from 'recast' | ||||||
import playwrightConfigTemplate from '../playwright/playwright-template' | ||||||
import { getChecklyConfigFile } from '../services/checkly-config-loader' | ||||||
import { loadPlaywrightConfig } from '../playwright/playwright-config-loader' | ||||||
import { parse } from '../services/handlebars-helpers' | ||||||
import * as Handlebars from 'handlebars' | ||||||
import fs from 'fs' | ||||||
import path from 'path' | ||||||
import { ux } from '@oclif/core' | ||||||
|
||||||
export default class SyncPlaywright extends BaseCommand { | ||||||
static hidden = true | ||||||
static description = 'Sync playwright config' | ||||||
|
||||||
async run (): Promise<void> { | ||||||
ux.action.start('Sync playwright config with checkly config', undefined, { stdout: true }) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @clample Syncing or Synching? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think Syncing is good. Apparently some British folks use Synching though. https://english.stackexchange.com/questions/681/synced-or-synched |
||||||
|
||||||
const config = await loadPlaywrightConfig() | ||||||
if (!config) { | ||||||
return this.handleError('Could not find any playwright.config file.') | ||||||
} | ||||||
|
||||||
Handlebars.registerHelper('parse', parse) | ||||||
const pwtConfig = Handlebars.compile(playwrightConfigTemplate)(config) | ||||||
const configFile = getChecklyConfigFile() | ||||||
if (!configFile) { | ||||||
return this.handleError('Could not find a checkly config file') | ||||||
} | ||||||
|
||||||
const checklyAst = recast.parse(configFile.checklyConfig) | ||||||
const checksAst = this.findPropertyByName(checklyAst, 'checks') | ||||||
if (!checksAst) { | ||||||
return this.handleError('Could not parse your checkly config file') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that users will assume that either their config is invalid or the I think that we could make it more clear that this can happen in some cases and there's no need to worry. For example, maybe they're importing the config from another file or using a helper function. Maybe something like this?
Suggested change
|
||||||
} | ||||||
|
||||||
const browserCheckAst = this.findPropertyByName(checksAst.value, 'browserChecks') | ||||||
if (!browserCheckAst) { | ||||||
return this.handleError('Could not parse your checkly config file') | ||||||
} | ||||||
const pwtConfigAst = this.findPropertyByName(recast.parse(pwtConfig), 'playwrightConfig') | ||||||
this.addOrReplacePlaywrightConfig(browserCheckAst.value, pwtConfigAst) | ||||||
|
||||||
const checklyConfigData = recast.print(checklyAst, { tabWidth: 2 }).code | ||||||
const dir = path.resolve(path.dirname(configFile.fileName)) | ||||||
this.reWriteChecklyConfigFile(checklyConfigData, configFile.fileName, dir) | ||||||
|
||||||
ux.action.stop('✅ ') | ||||||
this.log('Successfully sync') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @clample "...updated the Checkly config file" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks 😅 |
||||||
this.exit(0) | ||||||
} | ||||||
|
||||||
private handleError (message: string) { | ||||||
ux.action.stop('❌') | ||||||
this.log(message) | ||||||
this.exit(1) | ||||||
} | ||||||
|
||||||
private 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 | ||||||
} | ||||||
|
||||||
private addOrReplacePlaywrightConfig (ast: any, node: any) { | ||||||
const playWrightConfig = this.findPropertyByName(ast, 'playwrightConfig') | ||||||
if (playWrightConfig) { | ||||||
playWrightConfig.value = node.value | ||||||
} else { | ||||||
ast.properties.push(node) | ||||||
} | ||||||
} | ||||||
|
||||||
private reWriteChecklyConfigFile (data: string, fileName: string, dir: string) { | ||||||
fs.writeFileSync(path.join(dir, fileName), data) | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import path from 'path' | ||
import { loadFile } from '../services/checkly-config-loader' | ||
|
||
export async function loadPlaywrightConfig () { | ||
let config | ||
const filenames = ['playwright.config.ts', 'playwright.config.js'] | ||
for (const configFile of filenames) { | ||
const dir = path.resolve(path.dirname(configFile)) | ||
config = await loadFile(path.join(dir, configFile)) | ||
if (config) { | ||
break | ||
} | ||
} | ||
return config | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const playwrightConfigTemplate = ` | ||
const playwrightConfig = { | ||
playwrightConfig: { | ||
{{#timeout}} | ||
timeout: {{.}}, | ||
{{/timeout}} | ||
{{#use}} | ||
use: { | ||
{{#baseURL}} | ||
baseURL: '{{.}}', | ||
{{/baseURL}} | ||
{{#colorScheme}} | ||
colorScheme: '{{.}}', | ||
{{/colorScheme}} | ||
{{#geolocation}} | ||
geolocation: { | ||
{{#longitude}} | ||
longitude: {{.}}, | ||
{{/longitude}} | ||
{{#latitude}} | ||
latitude: {{.}}, | ||
{{/latitude}} | ||
{{#accuracy}} | ||
accuracy: {{.}}, | ||
{{/accuracy}} | ||
}, | ||
{{/geolocation}} | ||
{{#locale}} | ||
locale: '{{.}}', | ||
{{/locale}} | ||
{{#if permissions}} | ||
permissions: [{{#permissions}}'{{.}}',{{/permissions}}], | ||
{{/if}} | ||
{{#timezoneId}} | ||
timezoneId: '{{.}}', | ||
{{/timezoneId}} | ||
{{#viewport}} | ||
viewport: { | ||
{{#width}} | ||
width: {{.}}, | ||
{{/width}} | ||
{{#height}} | ||
height: {{.}}, | ||
{{/height}} | ||
}, | ||
{{/viewport}} | ||
{{#deviceScaleFactor}} | ||
deviceScaleFactor: {{.}}, | ||
{{/deviceScaleFactor}} | ||
{{#hasTouch}} | ||
hasTouch: {{.}}, | ||
{{/hasTouch}} | ||
{{#isMobile}} | ||
isMobile: {{.}}, | ||
{{/isMobile}} | ||
{{#javaScriptEnabled}} | ||
javaScriptEnabled: {{.}}, | ||
{{/javaScriptEnabled}} | ||
{{#acceptDownloads}} | ||
acceptDownloads: {{.}}, | ||
{{/acceptDownloads}} | ||
{{#extraHTTPHeaders}} | ||
extraHTTPHeaders: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/extraHTTPHeaders}} | ||
{{#httpCredentials}} | ||
httpCredentials: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/httpCredentials}} | ||
{{#ignoreHTTPSErrors}} | ||
ignoreHTTPSErrors: {{.}}, | ||
{{/ignoreHTTPSErrors}} | ||
{{#offline}} | ||
offline: {{.}}, | ||
{{/offline}} | ||
{{#actionTimeout}} | ||
actionTimeout: {{.}}, | ||
{{/actionTimeout}} | ||
{{#navigationTimeout}} | ||
navigationTimeout: {{.}}, | ||
{{/navigationTimeout}} | ||
{{#testIdAttribute}} | ||
testIdAttribute: '{{.}}', | ||
{{/testIdAttribute}} | ||
{{#launchOptions}} | ||
launchOptions: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/launchOptions}} | ||
{{#contextOptions}} | ||
contextOptions: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/contextOptions}} | ||
{{#bypassCSP}} | ||
bypassCSP: '{{.}}', | ||
{{/bypassCSP}} | ||
}, | ||
{{/use}} | ||
{{#expect}} | ||
expect: { | ||
{{#timeout}} | ||
timeout: {{.}}, | ||
{{/timeout}} | ||
{{#toHaveScreenshot}} | ||
toHaveScreenshot: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/toHaveScreenshot}} | ||
{{#toMatchSnapshot}} | ||
toMatchSnapshot: { | ||
{{#each .}} | ||
{{@key}}: {{{parse this}}}, | ||
{{/each}} | ||
}, | ||
{{/toMatchSnapshot}} | ||
}, | ||
{{/expect}} | ||
} | ||
} | ||
` | ||
export default playwrightConfigTemplate |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Handlebars do not handle properly nested objects/arrays | ||
export function parse (input: any): any { | ||
if (typeof input === 'string') { | ||
return `'${input}'` | ||
} | ||
if (typeof input === 'number' || typeof input === 'boolean') { | ||
return input | ||
} | ||
if (Array.isArray(input)) { | ||
let arr | ||
for (const o of input) { | ||
if (!arr) { | ||
arr = parse(o) | ||
continue | ||
} | ||
arr = `${arr}, ${parse(o)}` | ||
} | ||
return `[${arr}]` | ||
} | ||
if (typeof input === 'object') { | ||
const keys = Object.keys(input) | ||
let returnObj = '' | ||
for (const key of keys) { | ||
const curr = `${key}: ${parse(input[key])}` | ||
returnObj = `${returnObj} ${curr},` | ||
} | ||
return `{${returnObj}}` | ||
} | ||
return input | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
Playwright
andCheckly
should be capitalized in the user facing messages.Maybe we can make the help description more specific too in case users aren't familiar with the command. Maybe something like this?