chore: Remove string-based type injection via ship()
This commit is contained in:
parent
59c325ff6c
commit
20aa17df25
@ -20,39 +20,17 @@ function reflectType5(srcFile, options = { debug: false }) {
|
|||||||
log(`=========Reflecting file "${relative(__dirname, srcFile)}"`)
|
log(`=========Reflecting file "${relative(__dirname, srcFile)}"`)
|
||||||
|
|
||||||
const defFile = srcFile.replace(/.js$/, '.d.ts')
|
const defFile = srcFile.replace(/.js$/, '.d.ts')
|
||||||
const src = String(readFileSync(srcFile))
|
let src = String(readFileSync(srcFile))
|
||||||
const defs = String(readFileSync(defFile))
|
const defs = String(readFileSync(defFile))
|
||||||
const parsedDefs = ts2json(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) {
|
for (const id in parsedDefs) {
|
||||||
if (id.includes('interface')) continue
|
if (id.includes('interface')) continue
|
||||||
if (parsedDefs[id] === undefined) continue
|
if (parsedDefs[id] === undefined) continue
|
||||||
log(` Tagging ${id} with type data`, parsedDefs[id])
|
log(` Tagging ${id} with type data`, parsedDefs[id])
|
||||||
srcReflected +=
|
src += `\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
|
||||||
`\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
|
|
||||||
}
|
}
|
||||||
writeFileSync(srcFile, srcReflected)
|
writeFileSync(srcFile, src)
|
||||||
|
|
||||||
function log(...args) {
|
function log(...args) {
|
||||||
if (options.debug) {
|
if (options.debug) {
|
||||||
|
Loading…
Reference in New Issue
Block a user