forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-type.js
68 lines (62 loc) · 2.64 KB
/
gen-type.js
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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const path = require('path')
const { noElPrefixFile } = require('./common')
const outsideImport = /import .* from '..\/(.*?)\/src\/.*/
// global.d.ts
fs.copyFileSync(
path.resolve(__dirname, '../typings/vue-shim.d.ts'),
path.resolve(__dirname, '../lib/element-plus.d.ts'),
)
// index.d.ts
const newIndexPath = path.resolve(__dirname, '../lib/index.d.ts')
fs.copyFileSync(path.resolve(__dirname, '../lib/element-plus/index.d.ts'), newIndexPath)
const index = fs.readFileSync(newIndexPath)
const newIndex = index.toString().replace(/@element-plus\//g, './el-').replace('el-utils', 'utils').replace('el-locale', 'locale')
fs.writeFileSync(newIndexPath, newIndex)
// remove ep
fs.rmdirSync(path.resolve(__dirname, '../lib/element-plus'), { recursive: true })
// remove test-utils
fs.rmdirSync(path.resolve(__dirname, '../lib/test-utils'), { recursive: true })
// component
const libDirPath = path.resolve(__dirname, '../lib')
fs.readdirSync(libDirPath).forEach(comp => {
if (!noElPrefixFile.test(comp)) {
if (fs.lstatSync(path.resolve(libDirPath, comp)).isDirectory()) {
// rename
const newCompName = `el-${comp}`
fs.renameSync(path.resolve(libDirPath, comp),
path.resolve(libDirPath, newCompName))
// re-import
const imp = fs.readFileSync(path.resolve(__dirname, '../lib', newCompName, 'index.d.ts')).toString()
if (outsideImport.test(imp) || imp.includes('@element-plus/')) {
const newImp = imp.replace(outsideImport, (i, c) => {
return i.replace(`../${c}`, `../el-${c}`)
}).replace(/@element-plus\//g, '../el-').replace('el-utils', 'utils').replace('el-locale', 'locale')
fs.writeFileSync(path.resolve(__dirname, '../lib', newCompName, 'index.d.ts'), newImp)
}
}
}
})
// after components dir renamed
fs.readdirSync(libDirPath).forEach(comp => {
// check src/*.d.ts exist
const srcPath = path.resolve(libDirPath, comp, './src')
if (fs.existsSync(srcPath)) {
if (fs.lstatSync(srcPath).isDirectory()) {
fs.readdir(srcPath, 'utf-8', (err, data) => {
if (err) return
// replace all @element-plus in src/*.d.ts
data.forEach(f => {
if (!fs.lstatSync(path.resolve(srcPath, f)).isDirectory()) {
const imp = fs.readFileSync(path.resolve(srcPath, f)).toString()
if (imp.includes('@element-plus/')) {
const newImp = imp.replace(/@element-plus\//g, '../../el-').replace('el-utils', 'utils').replace('el-locale', 'locale')
fs.writeFileSync(path.resolve(srcPath, f), newImp)
}
}
})
})
}
}
})