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' let {signPage: sign, homePage: home = "console"} = config.extra || {} if (config.extra?.signPage) { signPage = `../apps/custom/${sign}/${sign}` } let homePage = `../views/console` if (config.extra?.homePage) { homePage = `../apps/custom/${home}/${home}` } // 查找并处理所有应用,将它们的信息添加到路由中 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: [ {path:'/',name:'mainEntry', component:()=>import('../views/mainEntry'),children:[ {name: "${home}", path: "${home}", component: () => import('${homePage}')}, ${routes.filter(e => ![sign, home].includes(e.name)).map(e => { // 解构每个路由的属性,用于生成路由配置 const {name, label, esm} = e // 生成单个路由配置的字符串表示 return `{name:"${name}",label:"${label}",path:"${name}",component:()=>import("../${esm}")}` }).join(',\n')}, {path: '*',name: '404',component: ()=>import('../views/building')}, ]} ]}, {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()