32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import * as fs from "node:fs"
|
|
|
|
const globalRegex = / (?:var|function) (\w+)/g
|
|
const globalSymbols: string[] = []
|
|
let buffer = ''
|
|
|
|
for (const basename of fs.readdirSync("node_modules/@types/node/web-globals")) {
|
|
buffer += fs.readFileSync("node_modules/@types/node/web-globals/" + basename, "utf8")
|
|
}
|
|
|
|
for (const match of buffer.matchAll(globalRegex)) {
|
|
globalSymbols.push(match[0].trim())
|
|
}
|
|
|
|
const declarationRegex = /(?:^\/\*\*[\s\S]+?\*\/\n)?^(interface|type|declare \w+) (\w+).+\n(?:(?: .+\n)*};?\n)?/gm
|
|
const importableTypes=new Set<string>()
|
|
buffer = ''
|
|
|
|
for (const match of fs.readFileSync("node_modules/@types/web/index.d.ts", "utf8").matchAll(declarationRegex)) {
|
|
if (!match[1].startsWith("declare")) {
|
|
importableTypes.add(match[2])
|
|
continue
|
|
}
|
|
if (globalSymbols.includes(match[1].replace("declare ","")+" "+match[2])) {
|
|
buffer+=match[0].replace(/^declare /gm,"").replaceAll("\n","\n\t")
|
|
}
|
|
}
|
|
|
|
buffer=`import type { \n\t${Array.from(importableTypes).join(",\n\t")}\n} from "@sugoidogo/importable-types-web"\ndeclare global {\n\t${buffer.trimEnd()}\n}`
|
|
|
|
fs.writeFileSync("index.d.ts", buffer, "utf8")
|
|
fs.copyFileSync("node_modules/@types/web/LICENSE.txt","LICENSE.txt") |