Files
dvcp_v2_webapp/bin/build.js
aixianling 536f579523 feat(utils): 重构路由生成逻辑
- 移除 autoRoutes.js 文件,改为使用 apps.js 存储路由信息
- 新增 createRoutes 函数,用于动态生成应用路由
- 更新 build.js,增加路由生成步骤
- 修改 router.js,使用新的路由配置
- 更新 .gitignore,忽略新的 apps.js 文件
2024-12-17 09:34:57 +08:00

92 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require('axios')
const {fsExtra, copyFiles, findApp, chalkTag, fs} = require("./tools");
const compiler = require('vue-template-compiler')
const getBuildConfig = id => {
axios.post('http://192.168.1.87:12525/node/custom/detail', null, {params: {id}}).then(res => {
if (res?.data) {
const config = res.data.data
fsExtra.outputJson('src/config.json', config.extra)
createPages(config)
}
})
}
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 => {
// 解构每个路由的属性,用于生成路由配置
const {name, label, path, esm} = e
// 生成单个路由配置的字符串表示
return `{name:"${name}",label:"${label}",path:"${path}",component:()=>import("../${esm}")}`
}).join(',\n')}
]},
{path: '/', name: "init"},
]`)
// 扫描完毕使用chalkTag标记任务完成
chalkTag.done("扫描完毕")
})
}
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)),
]).then(() => createRoutes(config)).then(() => fsExtra.ensureFile("src/apps/actions.js"))
}
})
}
const start = () => {
const buildId = process.argv[2] || process.env.VUE_APP_OMS_ID || 'f670cc46-7cf7-4a0f-86ee-3077044c0b17'
getBuildConfig(buildId)
}
start()