-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuild-component-dts.mjs
More file actions
45 lines (36 loc) · 1.38 KB
/
build-component-dts.mjs
File metadata and controls
45 lines (36 loc) · 1.38 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
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from 'fs'
import path from 'path'
import { v2Components } from './component-manifest.mjs'
const distDir = path.resolve(process.cwd(), 'dist')
const v2ComponentsDir = path.join(distDir, 'v2', 'components')
const publicComponentsDir = path.join(distDir, 'components')
if (!existsSync(v2ComponentsDir)) {
throw new Error(`Missing expected directory: ${v2ComponentsDir}`)
}
const manifestComponents = v2Components.map(({ sourceDir, sourcePath = sourceDir }) => ({
publicDir: sourceDir,
sourcePath
}))
const fallbackComponentDirs = readdirSync(v2ComponentsDir).filter(name => {
const fullPath = path.join(v2ComponentsDir, name)
return statSync(fullPath).isDirectory()
}).map(name => ({
publicDir: name,
sourcePath: name
}))
const componentDirs = manifestComponents.length > 0 ? manifestComponents : fallbackComponentDirs
for (const { publicDir, sourcePath } of componentDirs) {
const sourceIndex = path.join(v2ComponentsDir, sourcePath, 'index.d.ts')
if (!existsSync(sourceIndex)) {
continue
}
const outputDir = path.join(publicComponentsDir, publicDir)
mkdirSync(outputDir, { recursive: true })
const relativePath = path.relative(outputDir, sourceIndex)
.replace(/\\/g, '/')
.replace(/\.d\.ts$/, '')
writeFileSync(
path.join(outputDir, 'index.d.ts'),
`export * from '${relativePath}';\n`
)
}