2024-10-12 16:17:37 +08:00
|
|
|
|
const axios = require('axios')
|
2024-12-17 09:34:57 +08:00
|
|
|
|
const {fsExtra, copyFiles, findApp, chalkTag, fs} = require("./tools");
|
|
|
|
|
|
const compiler = require('vue-template-compiler')
|
2024-10-12 16:17:37 +08:00
|
|
|
|
const getBuildConfig = id => {
|
2024-10-17 17:19:49 +08:00
|
|
|
|
axios.post('http://192.168.1.87:12525/node/custom/detail', null, {params: {id}}).then(res => {
|
2024-10-12 16:17:37 +08:00
|
|
|
|
if (res?.data) {
|
|
|
|
|
|
const config = res.data.data
|
|
|
|
|
|
fsExtra.outputJson('src/config.json', config.extra)
|
|
|
|
|
|
createPages(config)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-12-17 09:34:57 +08:00
|
|
|
|
const getAppInfo = (file, apps) => {
|
|
|
|
|
|
if (/[\\\/](App[A-Z][^\\\/]+)\.vue$/g.test(file)) {
|
|
|
|
|
|
const name = file.replace(/.+[\\\/](App[^\\\/]+)\.vue$/, '$1'),
|
|
|
|
|
|
source = fs.readFileSync(file).toString(),
|
|
|
|
|
|
parsed = compiler.parseComponent(source),
|
|
|
|
|
|
script = parsed.script?.content || "",
|
|
|
|
|
|
label = script.match(/label:[^,]+/)?.[0]?.replace(/.+["']([^"']+).+/, '$1')
|
|
|
|
|
|
const paths = file.split(/[\\\/]/)
|
|
|
|
|
|
apps.push({
|
|
|
|
|
|
id: file.replace(/\.vue$/, '').replace(/[\\\/]/g, '_'),
|
|
|
|
|
|
label: label || name,
|
|
|
|
|
|
path: `/${file.replace(/\.vue$/, '').replace(/[\\\/]/g, '/')}`,
|
|
|
|
|
|
workspace: paths.at(0),
|
|
|
|
|
|
esm: file.replace(/[\\\/]/g, '/').substring(4),
|
|
|
|
|
|
name
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据配置生成应用路由
|
|
|
|
|
|
* @param {Object} config - 配置对象,用于定制化路由生成过程
|
|
|
|
|
|
* @returns {Promise} - 返回一个Promise对象,表示路由生成完成
|
|
|
|
|
|
*/
|
|
|
|
|
|
const createRoutes = (config = {}) => {
|
|
|
|
|
|
// 初始化路由数组
|
|
|
|
|
|
const routes = []
|
|
|
|
|
|
// 获取签到页面的路径,如果未指定,则使用默认路径
|
|
|
|
|
|
let signPage = '../views/sign'
|
|
|
|
|
|
if (config.extra?.signPage) {
|
|
|
|
|
|
const sign = config.extra.signPage
|
|
|
|
|
|
signPage = `../apps/custom/${sign}/${sign}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找并处理所有应用,将它们的信息添加到路由中
|
|
|
|
|
|
return findApp("src/apps", app => getAppInfo(app, routes)).then(() => {
|
|
|
|
|
|
// 生成并输出apps.js文件,定义所有应用的路由
|
|
|
|
|
|
fsExtra.outputFile('src/utils/apps.js', `export default [
|
|
|
|
|
|
{path: "/login", name: "登录", component: () => import('${signPage}')},
|
|
|
|
|
|
{path: '/dv', name: '数据大屏入口', component: () => import('../views/dvIndex')},
|
|
|
|
|
|
{path: '/v', name: 'Home', component: () => import('../views/home'), children: [
|
|
|
|
|
|
${routes.map(e => {
|
|
|
|
|
|
// 解构每个路由的属性,用于生成路由配置
|
2024-12-23 10:20:19 +08:00
|
|
|
|
const {name, label, esm} = e
|
2024-12-17 09:34:57 +08:00
|
|
|
|
// 生成单个路由配置的字符串表示
|
2024-12-23 10:20:19 +08:00
|
|
|
|
return `{name:"${name}",label:"${label}",path:"${name}",component:()=>import("../${esm}")}`
|
2024-12-17 09:34:57 +08:00
|
|
|
|
}).join(',\n')}
|
|
|
|
|
|
]},
|
|
|
|
|
|
{path: '/', name: "init"},
|
|
|
|
|
|
]`)
|
|
|
|
|
|
// 扫描完毕,使用chalkTag标记任务完成
|
|
|
|
|
|
chalkTag.done("扫描完毕")
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-10-12 16:17:37 +08:00
|
|
|
|
|
|
|
|
|
|
const createPages = (config = {}) => {
|
|
|
|
|
|
fsExtra.emptyDir("src/apps", err => {
|
|
|
|
|
|
if (!err) {
|
|
|
|
|
|
const {customPath, appList} = config
|
|
|
|
|
|
const stdApps = {}
|
|
|
|
|
|
appList.filter(e => !/project/.test(e.id))?.forEach(e => {
|
|
|
|
|
|
const paths = e.libPath.split('/').filter(Boolean) || []
|
|
|
|
|
|
paths.pop()
|
|
|
|
|
|
stdApps[paths.join("/")] = 1
|
|
|
|
|
|
})
|
|
|
|
|
|
Promise.all([
|
|
|
|
|
|
copyFiles("src/apps/core", "packages/core"),
|
|
|
|
|
|
copyFiles("src/apps/custom", `project/${customPath}`),
|
|
|
|
|
|
...Object.keys(stdApps).map(e => copyFiles(`src/apps/${e.replace(/^packages[\\\/]/, '')}`, e)),
|
2024-12-17 09:34:57 +08:00
|
|
|
|
]).then(() => createRoutes(config)).then(() => fsExtra.ensureFile("src/apps/actions.js"))
|
2024-10-12 16:17:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const start = () => {
|
2024-12-17 09:34:57 +08:00
|
|
|
|
const buildId = process.argv[2] || process.env.VUE_APP_OMS_ID || 'f670cc46-7cf7-4a0f-86ee-3077044c0b17'
|
2024-10-12 16:17:37 +08:00
|
|
|
|
getBuildConfig(buildId)
|
|
|
|
|
|
}
|
|
|
|
|
|
start()
|