78 lines
1.7 KiB
JavaScript
78 lines
1.7 KiB
JavaScript
import fs from 'fs'
|
|
import {build} from 'vite'
|
|
const PWD = process.cwd()
|
|
|
|
const clearDir = (path, exclude) => {
|
|
var files = []
|
|
|
|
if (fs.existsSync(path)) {
|
|
files = fs.readdirSync(path)
|
|
|
|
for (const file of files) {
|
|
if (file === exclude) continue
|
|
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: {
|
|
entry: `src/widgets/${widgetName}/${entryName}.ts`,
|
|
formats: ['es'],
|
|
fileName: `${widgetName}`,
|
|
},
|
|
outDir: `dist/widgets/${widgetName}/`,
|
|
},
|
|
})
|
|
|
|
clearDir(`dist/widgets/${widgetName}`, `${widgetName}.js`)
|
|
}
|
|
|
|
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()
|