Skip to content
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: full ESM config support using Jiti (build #1041) #1048

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- run: npm run lint
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
name: test - ${{ matrix.os }}
Expand Down
3 changes: 1 addition & 2 deletions examples/advanced-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"license": "ISC",
"devDependencies": {
"checkly": "latest",
"ts-node": "10.9.1",
"typescript": "4.9.5"
"jiti": "^2"
}
}
3 changes: 1 addition & 2 deletions examples/boilerplate-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"license": "ISC",
"devDependencies": {
"checkly": "latest",
"ts-node": "10.9.1",
"typescript": "4.9.5"
"jiti": "^2"
}
}
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,22 @@
"config": "3.3.9",
"cross-env": "7.0.3",
"jest": "29.7.0",
"jiti": "2.4.2",
"nanoid": "3.3.4",
"oclif": "3.7.3",
"simple-git-hooks": "2.11.1",
"ts-jest": "29.2.4",
"ts-node": "10.9.1",
"typescript": "5.3.3"
},
"peerDependencies": {
"jiti": ">=2"
},
"peerDependenciesMeta": {
"jiti": {
"optional": true
}
},
"jest": {
"testTimeout": 30000,
"projects": [
Expand Down
26 changes: 15 additions & 11 deletions packages/cli/src/playwright/playwright-config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import path from 'path'
import { loadFile } from '../services/checkly-config-loader'
import fs from 'fs'
import path from 'path'
import { loadFile } from '../services/util'

export async function loadPlaywrightConfig () {
let config
const filenames = ['playwright.config.ts', 'playwright.config.js']
const filenames = [
'playwright.config.ts',
'playwright.config.mts',
'playwright.config.cts',
'playwright.config.js',
'playwright.config.mjs',
'playwright.config.cjs',
]
for (const configFile of filenames) {
if (!fs.existsSync(path.resolve(path.dirname(configFile)))) {
const configPath = path.resolve(configFile)
if (!fs.existsSync(configPath)) {
continue
}
const dir = path.resolve(path.dirname(configFile))
config = await loadFile(path.join(dir, configFile))
if (config) {
break
}
const result = await loadFile(configPath)
return result
}
return config
return undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('loadChecklyConfig()', () => {
await loadChecklyConfig(configDir)
} catch (e: any) {
expect(e.message).toContain(`Unable to locate a config at ${configDir} with ${
['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs'].join(', ')}.`)
['checkly.config.ts', 'checkly.config.mts', 'checkly.config.cts', 'checkly.config.js', 'checkly.config.mjs', 'checkly.config.cjs'].join(', ')}.`)
}
})
it('config TS file should export an object', async () => {
Expand Down
41 changes: 13 additions & 28 deletions packages/cli/src/services/checkly-config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path'
import { existsSync } from 'fs'
import { loadJsFile, loadTsFile } from './util'
import { loadFile } from './util'
import { CheckProps } from '../constructs/check'
import { Session } from '../constructs'
import { Construct } from '../constructs/construct'
Expand Down Expand Up @@ -73,35 +73,19 @@ export type ChecklyConfig = {
}
}

// eslint-disable-next-line no-restricted-syntax
enum Extension {
JS = '.js',
MJS = '.mjs',
TS = '.ts',
}

export function loadFile (file: string) {
if (!existsSync(file)) {
return Promise.resolve(null)
}
switch (path.extname(file)) {
case Extension.JS:
return loadJsFile(file)
case Extension.MJS:
return loadJsFile(file)
case Extension.TS:
return loadTsFile(file)
default:
throw new Error(`Unsupported file extension ${file} for the config file`)
}
}

function isString (obj: any) {
return (Object.prototype.toString.call(obj) === '[object String]')
}

export function getChecklyConfigFile (): {checklyConfig: string, fileName: string} | undefined {
const filenames: string[] = ['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs']
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))
Expand All @@ -120,13 +104,14 @@ export function getChecklyConfigFile (): {checklyConfig: string, fileName: strin
return config
}

export async function loadChecklyConfig (dir: string, filenames = ['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs']): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
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']): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
let config
Session.loadingChecklyConfigFile = true
Session.checklyConfigFileConstructs = []
for (const filename of filenames) {
config = await loadFile(path.join(dir, filename))
if (config) {
const filePath = path.join(dir, filename)
if (existsSync(filePath)) {
config = await loadFile(filePath)
break
}
}
Expand Down
12 changes: 4 additions & 8 deletions packages/cli/src/services/project-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { glob } from 'glob'
import * as path from 'path'
import { loadJsFile, loadTsFile, pathToPosix } from './util'
import { loadFile, pathToPosix } from './util'
import {
Check, BrowserCheck, CheckGroup, Project, Session,
PrivateLocation, PrivateLocationCheckAssignment, PrivateLocationGroupAssignment, MultiStepCheck,
Expand Down Expand Up @@ -85,15 +85,11 @@ async function loadAllCheckFiles (
// setting the checkFilePath is used for filtering by file name on the command line
Session.checkFileAbsolutePath = checkFile
Session.checkFilePath = pathToPosix(path.relative(directory, checkFile))
if (checkFile.endsWith('.js')) {
await loadJsFile(checkFile)
} else if (checkFile.endsWith('.mjs')) {
await loadJsFile(checkFile)
} else if (checkFile.endsWith('.ts')) {
await loadTsFile(checkFile)
if (/\.[mc]?(js|ts)$/.test(checkFile)) {
await loadFile(checkFile)
} else {
throw new Error('Unable to load check configuration file with unsupported extension. ' +
`Please use a .js, .msj or .ts file instead.\n${checkFile}`)
`Please use a .js, .mjs, .cjs, .ts, .mts or .cts file instead.\n${checkFile}`)
}
Session.checkFilePath = undefined
Session.checkFileAbsolutePath = undefined
Expand Down
Loading
Loading