chore: Remove string-based type injection via ship()

This commit is contained in:
Glen Whitney 2024-10-21 10:12:05 -07:00
parent 59c325ff6c
commit 20aa17df25

View File

@ -20,39 +20,17 @@ function reflectType5(srcFile, options = { debug: false }) {
log(`=========Reflecting file "${relative(__dirname, srcFile)}"`)
const defFile = srcFile.replace(/.js$/, '.d.ts')
const src = String(readFileSync(srcFile))
let src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
const parsedDefs = ts2json(defFile)
const typeDefMatches = defs.matchAll(/: ({(?:(?!\n}).)+\n}) & (?:(?!ReflectedTypeInfo).)+ReflectedTypeInfo/gs)
if (!typeDefMatches) {
log('No ReflectedTypeInfo found.')
return
}
const typeDefs = Array.from(typeDefMatches).map(def => def[1])
log(` ${typeDefs.length} ReflectedTypeInfo found`)
let index = 0
let 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 id in parsedDefs) {
if (id.includes('interface')) continue
if (parsedDefs[id] === undefined) continue
log(` Tagging ${id} with type data`, parsedDefs[id])
srcReflected +=
`\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
src += `\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
}
writeFileSync(srcFile, srcReflected)
writeFileSync(srcFile, src)
function log(...args) {
if (options.debug) {