chore: properly working generated types within tests (#6288)

This commit is contained in:
Alessio Gravili
2024-05-09 17:12:51 -04:00
committed by GitHub
parent cfeac79b99
commit f7a2cf96b9
93 changed files with 7012 additions and 3097 deletions

View File

@@ -1,4 +1,5 @@
import fs from 'fs'
import { spawn } from 'node:child_process'
import path from 'path'
import { generateTypes } from 'payload/node'
@@ -14,36 +15,59 @@ import { load } from './loader/load.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
let testDir
if (testConfigDir) {
testDir = path.resolve(dirname, testConfigDir)
let testDir: string
console.log('Generating types for config:', path.resolve(testDir, 'config.ts'))
async function run() {
if (testConfigDir) {
testDir = path.resolve(dirname, testConfigDir)
const config: SanitizedConfig = (await load(
path.resolve(testDir, 'config.ts'),
)) as unknown as SanitizedConfig
const pathWithConfig = path.resolve(testDir, 'config.ts')
console.log('Generating types for config:', pathWithConfig)
setTestEnvPaths(testDir)
generateTypes(config)
} else {
// Generate types for entire directory
testDir = dirname
const config: SanitizedConfig = (await load(pathWithConfig)) as unknown as SanitizedConfig
console.log(
'No testConfigDir passed. Generating types for config:',
path.resolve(testDir, 'config.ts'),
)
setTestEnvPaths(testDir)
await generateTypes(config)
} else {
// Search through every folder in dirname, and if it has a config.ts file, generate types for it
const foundDirs: string[] = []
const config: SanitizedConfig = (await load(
path.resolve(testDir, 'config.ts'),
)) as unknown as SanitizedConfig
fs.readdirSync(dirname, { withFileTypes: true })
.filter((f) => f.isDirectory())
.forEach((dir) => {
const suiteDir = path.resolve(dirname, dir.name)
const configFound = fs.existsSync(path.resolve(suiteDir, 'config.ts'))
if (configFound) {
foundDirs.push(dir.name)
}
})
fs.readdirSync(dirname, { withFileTypes: true })
.filter((f) => f.isDirectory())
.forEach((dir) => {
const suiteDir = path.resolve(testDir, dir.name)
const configFound = setTestEnvPaths(suiteDir)
if (configFound) generateTypes(config)
})
let i = 0
for (const suiteDir of foundDirs) {
i++
const pathWithConfig = path.resolve(suiteDir, 'config.ts')
console.log(`Generating types for config ${i} / ${foundDirs.length}:`, pathWithConfig)
// start a new node process which runs test/generateTypes with pathWithConfig as argument. Can't run it in this process, as there could otherwise be
// breakage between tests, as node can cache things in between runs.
// Make sure to wait until the process is done before starting the next one.
const child = spawn('tsx', ['test/generateTypes.ts', suiteDir])
child.stdout.setEncoding('utf8')
child.stdout.on('data', function (data) {
console.log(suiteDir + ' stdout: ' + data)
})
child.stderr.setEncoding('utf8')
child.stderr.on('data', function (data) {
console.log(suiteDir + ' stderr: ' + data)
})
child.on('close', function (code) {
console.log(suiteDir + ' closing code: ' + code)
})
}
}
}
void run()