math5/tools/reflectTypes.mjs

41 lines
1.2 KiB
JavaScript
Raw Normal View History

import { readFileSync, writeFileSync, readdirSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join, relative } from 'node:path'
import ts2json from './ts2json.mjs'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const buildDir = join(__dirname, '..', 'build')
const files = (await readdirSync(buildDir, { recursive: true }))
.filter(file => file.endsWith('.js'))
.map(file => join(buildDir, file))
for (const file of files) {
reflectType5(file, { debug: true })
}
function reflectType5(srcFile, options = { debug: false }) {
log(`=========Reflecting file "${relative(__dirname, srcFile)}"`)
const defFile = srcFile.replace(/.js$/, '.d.ts')
let src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
const parsedDefs = ts2json(defFile)
for (const id in parsedDefs) {
if (id.includes('interface')) continue
if (parsedDefs[id] === undefined) continue
log(` Tagging ${id} with type data`, parsedDefs[id])
src += `\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
}
writeFileSync(srcFile, src)
function log(...args) {
if (options.debug) {
console.log(...args)
}
}
}