-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_defs.ts
36 lines (30 loc) · 1.16 KB
/
generate_defs.ts
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
import fs from 'fs/promises'
import path from 'path'
async function readVendor(vendor: string) {
const root = `./defs/${vendor}`
const defs = await fs.readdir(root)
const lines = defs.map((def, index) => {
const uuid = 'a' + Math.random().toString(32).substring(2)
return {
importLine: `import ${uuid} from './${vendor}/${def}';`,
exportLine: `"${path.parse(def).name}": ${uuid}`
}
})
return lines
}
async function main() {
const vendors = await fs.readdir('./defs').then((paths) => paths.filter(p => p !== 'index.ts'));
const allImports: string[] = []
const allExports: string[] = []
allExports.push(`export default {`)
for (const vendor of vendors) {
allExports.push(`\t"${vendor}": {`)
const vendorLines = await readVendor(vendor)
vendorLines.forEach(({ importLine }) => allImports.push(importLine))
vendorLines.forEach(({ exportLine }) => allExports.push(`\t\t${exportLine},`))
allExports.push(`\t},`)
}
allExports.push(`}`)
await fs.writeFile('./defs/index.ts', allImports.join('\n') + '\n' + allExports.join('\n'), 'utf-8')
}
main()