element-plus/build/gen-type.js

42 lines
1.6 KiB
JavaScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const path = require('path')
const { noElPrefixFile } = require('./common')
const outsideImport = /import .* from '..\/(.*?)\/src\/.*/
// 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')
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()
2020-11-27 12:03:44 +08:00
if(outsideImport.test(imp) || imp.includes('@element-plus/')) {
const newImp = imp.replace(outsideImport, (i, c) => {
2020-11-27 10:20:58 +08:00
return i.replace(`../${c}`, `../el-${c}`)
2020-11-27 11:15:20 +08:00
}).replace('@element-plus/', '../el-')
fs.writeFileSync(path.resolve(__dirname, '../lib', newCompName, 'index.d.ts'), newImp)
}
}
}
})