2022-10-07 11:17:24 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import {build} from 'vite'
|
|
|
|
const PWD = process.cwd()
|
|
|
|
|
2022-10-25 10:47:49 +08:00
|
|
|
const clearDir = (path, excludes) => {
|
2022-10-07 11:17:24 +08:00
|
|
|
var files = []
|
|
|
|
|
|
|
|
if (fs.existsSync(path)) {
|
|
|
|
files = fs.readdirSync(path)
|
|
|
|
|
2022-10-25 10:47:49 +08:00
|
|
|
nextFile: for (const file of files) {
|
|
|
|
if(excludes) {
|
|
|
|
for (let exclude of excludes) {
|
|
|
|
if (file === exclude) continue nextFile
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 11:17:24 +08:00
|
|
|
var curPath = path + '/' + file
|
|
|
|
|
|
|
|
if (fs.statSync(curPath).isDirectory()) {
|
|
|
|
clearDir(curPath)
|
|
|
|
fs.rmdirSync(curPath)
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(curPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fun = async (widgetName, entryName) => {
|
|
|
|
await build({
|
|
|
|
build: {
|
|
|
|
lib: {
|
2022-10-25 10:47:49 +08:00
|
|
|
name: widgetName,
|
2022-10-07 11:17:24 +08:00
|
|
|
entry: `src/widgets/${widgetName}/${entryName}.ts`,
|
2022-10-25 10:47:49 +08:00
|
|
|
formats: ['cjs', 'es'],
|
2022-10-07 11:17:24 +08:00
|
|
|
fileName: `${widgetName}`,
|
|
|
|
},
|
|
|
|
outDir: `dist/widgets/${widgetName}/`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-10-25 10:47:49 +08:00
|
|
|
clearDir(`dist/widgets/${widgetName}`, [`${widgetName}.cjs`, `${widgetName}.js`])
|
2022-10-07 11:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const safeReadDirSync = (path) => {
|
|
|
|
let dirData = {}
|
|
|
|
try {
|
|
|
|
dirData = fs.readdirSync(path)
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex.code == 'EACCES' || ex.code == 'EPERM') {
|
|
|
|
// 无权访问该文件夹,跳过
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
throw ex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirData
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildAll = () => {
|
|
|
|
const widgetsFilePath = `${PWD}/src/widgets`
|
|
|
|
const dirData = safeReadDirSync(widgetsFilePath)
|
|
|
|
|
|
|
|
dirData?.forEach((fileName) => {
|
|
|
|
const widgetPath = `${widgetsFilePath}/${fileName}`
|
|
|
|
const stats = fs.statSync(widgetPath)
|
|
|
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
const files = safeReadDirSync(widgetPath)
|
|
|
|
if (files.includes(`${fileName}.ts`)) {
|
|
|
|
fun(fileName, fileName)
|
|
|
|
} else if (files.includes(`index.ts`)) {
|
|
|
|
fun(fileName, 'index')
|
|
|
|
} else {
|
|
|
|
throw new Error(`Entry file of ${fileName} widget dose not exist!`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
buildAll()
|