-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathlink-typedefinitions.ts
More file actions
57 lines (49 loc) · 1.87 KB
/
Copy pathlink-typedefinitions.ts
File metadata and controls
57 lines (49 loc) · 1.87 KB
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
import { writeFileSync } from 'fs'
import { resolve } from 'path'
import glob from 'tiny-glob/sync.js'
type FromWheretoImport = string
type OutputPath = string
type FilterFunction = (file: string) => boolean
const mappings: [FromWheretoImport, OutputPath?, FilterFunction?][] = [
['adapter-angular', 'angular'],
['adapter-react', 'react'],
['adapter-solid', 'solid'],
['adapter-svelte', 'svelte'],
['adapter-vue', 'vue'],
['config', 'config', (file) => file === 'index.d.mts'],
['detectors'],
['exporter', 'exporter', (file) => file === 'index.d.mts'],
['formatters'],
['importer', 'importer', (file) => file === 'index.d.mts'],
['parser', 'parser', (file) => file === 'index.d.mts'],
['runtime', 'types'],
['runtime', 'runtime/cjs/runtime/src'],
['runtime', 'runtime/esm/runtime/src'],
['utils'],
]
const goToRoot = (outputPath: string, file: string) => {
const goUpXTimes = outputPath.startsWith('runtime') ? 3 : 0
return new Array(file.split('/').length + goUpXTimes).fill('../').join('')
}
mappings.forEach(([fromWheretoImport, outputPath = fromWheretoImport, filterFunction]) => {
const files = glob(`types/${fromWheretoImport}/src/**/*.d.{ts,mts}`).map((file) =>
resolve(file)
.substring(resolve(__dirname, `../types/${fromWheretoImport}/src/`).length + 1)
.replace(/\\/g, '/'),
)
// link generated files to types
const filteredFiles = (filterFunction && files.filter(filterFunction)) || files
filteredFiles.forEach((file) => {
const esm = file.endsWith('.d.mts')
const fileName = file.replace(esm ? '.d.mts' : '.d.ts', '')
writeFileSync(
resolve(__dirname, `../${outputPath}/${file.replace('.d.mts', '.d.ts')}`),
`export * from '${goToRoot(outputPath, file)}types/${fromWheretoImport}/src/${fileName}.${
esm ? 'mjs' : 'js'
}'`,
{ encoding: 'utf8' },
)
})
// eslint-disable-next-line no-console
console.info(`types linked for '${outputPath}'`)
})