-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfixUniqueSymbolExports.mts
77 lines (58 loc) · 2.14 KB
/
fixUniqueSymbolExports.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node --import=tsx
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
const entryPointDirectories = ['', 'react', 'query', 'query/react']
const typeDefinitionEntryFiles = entryPointDirectories.flatMap(
(filePath) =>
[
path.join(import.meta.dirname, '..', 'dist', filePath, 'index.d.ts'),
path.join(import.meta.dirname, '..', 'dist', filePath, 'index.d.mts'),
] as const,
)
const filePathsToContentMap = new Map<string, string>(
await Promise.all(
typeDefinitionEntryFiles.map(
async (filePath) =>
[filePath, await fs.readFile(filePath, { encoding: 'utf-8' })] as const,
),
),
)
const main = async () => {
filePathsToContentMap.forEach(async (content, filePath) => {
const exportedUniqueSymbols = new Set<string>()
console.log(`Fixing \`unique symbol\` exports in ${filePath}`)
const lines = content.split('\n')
const allUniqueSymbols = lines
.filter((line) => /declare const (\w+): unique symbol;/.test(line))
.map((line) => line.match(/declare const (\w+): unique symbol;/)?.[1])
if (allUniqueSymbols.length === 0) {
console.log(`${filePath} does not have any unique symbols.`)
return
}
const allNamedExports = lines
.at(-2)
?.match(/^export \{ (.*) \};$/)?.[1]
.split(', ')
allNamedExports?.forEach((namedExport) => {
if (allUniqueSymbols.includes(namedExport)) {
exportedUniqueSymbols.add(namedExport)
}
})
if (exportedUniqueSymbols.size === 0) {
console.log(
`${filePath} has unique symbols but none of them are exported.`,
)
return
}
let newContent = `${lines.slice(0, -2).join('\n')}\nexport { ${allNamedExports?.filter((namedExport) => !exportedUniqueSymbols.has(namedExport)).join(', ')} };\n`
exportedUniqueSymbols.forEach((uniqueSymbol) => {
console.log(`Exporting \`${uniqueSymbol}\` from ${filePath}`)
newContent = newContent.replace(
`declare const ${uniqueSymbol}`,
`export declare const ${uniqueSymbol}`,
)
})
await fs.writeFile(filePath, newContent, { encoding: 'utf-8' })
})
}
void main()