chore: inject the reflected type information inside the .ship() occurrences

This commit is contained in:
Jos de Jong 2024-10-11 13:37:07 +02:00
parent 91ed5fe83a
commit 2780518d97
2 changed files with 25 additions and 28 deletions

View file

@ -11,44 +11,38 @@ const files = (await readdirSync(buildDir, { recursive: true }))
.map(file => join(buildDir, file))
for (const file of files) {
reflectType(file, { debug: true })
reflectType5(file, { debug: true })
}
function reflectType(srcFile, options = { debug: false }) {
function reflectType5(srcFile, options = { debug: false }) {
log(`Reflecting file "${relative(__dirname, srcFile)}"`)
const defFile = srcFile.replace(/.js$/, '.d.ts')
const src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
if (src.includes('.reflectedType5')) {
log(' Source file already contains reflected types and will be ignored')
const typeDefMatches = defs.matchAll(/: ({(?:(?!\n}).)+\n}) & (?:(?!ReflectedTypeInfo).)+ReflectedTypeInfo/gs)
if (!typeDefMatches) {
log('No ReflectedTypeInfo found.')
return
}
const matches = Array.from(src.matchAll(/[^\n](export +)?const +([\w$]+) += +implementations\b/g))
const typeDefs = Array.from(typeDefMatches).map(def => def[1])
log(` ${typeDefs.length} ReflectedTypeInfo found`)
if (matches.length === 0) {
log(` No implementations found`)
return
let index = 0
const srcReflected = src.replaceAll(/(\s*)\.ship\(\)/g, () => {
const def = typeDefs[index]
index++
return `.ship({ reflectedType5: \`${def}\` })`
})
log(` ReflectedTypeInfo injected in ${index} occurrences of .ship()`)
if (index !== typeDefs.length) {
log(' WARNING: not all ReflectedTypeInfo occurrences could be injected')
}
for (const match of matches) {
const name = match[2]
log(` Found an implementations definition: "${name}"`)
const def = new RegExp(`export declare const ${name}: {(?:(?!\n}).)+\n}`, 's').exec(defs)
if (def) {
log(` Injected type definitions for "${name}"`)
const srcReflected = `${src}\n${name}.reflectedType5 = \`${def}\`\n`
writeFileSync(srcFile, srcReflected)
} else {
log(` No type definitions found for "${name}"`)
}
}
writeFileSync(srcFile, srcReflected)
function log(...args) {
if (options.debug) {