chore: inject the reflected type information inside the .ship()
occurrences
This commit is contained in:
parent
ad5fd99f53
commit
b9675c6d59
@ -102,6 +102,10 @@ type Implementations<
|
||||
Specs extends Specifications<Signatures, NeedKeys, NeedList>
|
||||
> = {[K in NeedKeys]: ImpType<Signatures, K, Specs[K]>}
|
||||
|
||||
export interface ReflectedTypeInfo {
|
||||
reflectedType5: string
|
||||
}
|
||||
|
||||
// The builder interface that lets us assemble narrowly-typed Implementations:
|
||||
interface ImplementationBuilder<
|
||||
Signatures extends GenSigs,
|
||||
@ -135,7 +139,7 @@ interface ImplementationBuilder<
|
||||
Specs & {[K in NewKeys]: DepCheck<RD, Signatures>}
|
||||
>
|
||||
|
||||
ship(): Implementations<Signatures, NeedKeys, NeedList, Specs> & { reflectedType5: string }
|
||||
ship(info?: ReflectedTypeInfo): Implementations<Signatures, NeedKeys, NeedList, Specs> & ReflectedTypeInfo
|
||||
}
|
||||
|
||||
// And a function that actually provides the builder interface:
|
||||
@ -187,9 +191,8 @@ function impBuilder<
|
||||
Specs & {[K in NewKeys]: DepCheck<RD, Signatures, DepKeys>}
|
||||
>
|
||||
},
|
||||
ship() {
|
||||
return (sofar as
|
||||
Implementations<Signatures, NeedKeys, NeedList, Specs> & { reflectedType5: string })
|
||||
ship(info?: ReflectedTypeInfo) {
|
||||
return { ...sofar, ...info }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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}"`)
|
||||
}
|
||||
}
|
||||
|
||||
function log(...args) {
|
||||
if (options.debug) {
|
||||
|
Loading…
Reference in New Issue
Block a user