Files
CN_Tool_client/frontend/src/layouts/components/Main/check-keep-alive-contract.mjs
yexb f7d297decf docs(design): 删除磁盘监控设计文档并更新前端页面结构规范
- 删除 frontend/src/views/systemMonitor/2026-04-22-disk-monitor-design.md 设计文档
- 删除 frontend/src/views/tools/addLedger/API_DEBUG.md 调试文档
- 在 AGENTS.md 中新增前端页面结构归档章节,规范复杂工具页结构
- 明确 index.vue、components/、utils/ 职责边界和拆分原则
- 规定页面级类型和 contract 脚本管理方式
- 统一复杂页面拆分优先顺序和注意事项
2026-05-14 09:17:25 +08:00

73 lines
2.6 KiB
JavaScript

import { readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const root = resolve(dirname(fileURLToPath(import.meta.url)), '../../../..')
const read = path => readFileSync(resolve(root, path), 'utf8')
const staticRouterSource = read('src/routers/modules/staticRouter.ts')
const mainSource = read('src/layouts/components/Main/index.vue')
const tabsSource = read('src/stores/modules/tabs.ts')
const routeContracts = [
['tools', 'src/views/tools/index.vue'],
['toolWaveform', 'src/views/tools/waveform/index.vue'],
['toolMmsMapping', 'src/views/tools/mmsMapping/index.vue'],
['toolAddData', 'src/views/tools/addData/index.vue'],
['toolAddLedger', 'src/views/tools/addLedger/index.vue'],
['eventList', 'src/views/event/eventList/index.vue'],
['systemMonitor', 'src/views/systemMonitor/index.vue'],
['diskMonitor', 'src/views/systemMonitor/diskMonitor/index.vue']
]
const extractRouteBlock = routeName => {
const routeNameIndex = staticRouterSource.indexOf(`name: '${routeName}'`)
if (routeNameIndex === -1) {
throw new Error(`Route ${routeName} was not found in staticRouter.ts`)
}
const nextRouteIndex = staticRouterSource.indexOf('\n {', routeNameIndex + 1)
return staticRouterSource.slice(routeNameIndex, nextRouteIndex === -1 ? undefined : nextRouteIndex)
}
const extractComponentName = viewPath => {
const viewSource = read(viewPath)
const componentName = viewSource.match(/defineOptions\(\s*\{\s*name:\s*'([^']+)'/s)?.[1]
if (!componentName) {
throw new Error(`${viewPath} must define an explicit component name for keep-alive`)
}
return componentName
}
const errors = []
for (const [routeName, viewPath] of routeContracts) {
const routeBlock = extractRouteBlock(routeName)
const componentName = extractComponentName(viewPath)
const cacheName = routeBlock.match(/cacheName:\s*'([^']+)'/)?.[1]
if (cacheName !== componentName) {
errors.push(`${routeName} meta.cacheName should be ${componentName}, got ${cacheName || '<missing>'}`)
}
}
if (!mainSource.includes('v-if="isRouterShow"')) {
errors.push('Main router component must use isRouterShow so tab refresh can rebuild the current page')
}
if (!mainSource.includes('keepAliveName')) {
errors.push('Main keep-alive include list must come from keepAliveName')
}
if (!tabsSource.includes('cacheName')) {
errors.push('Tabs store must keep actual component cacheName values in keepAliveName')
}
if (errors.length) {
console.error(errors.join('\n'))
process.exit(1)
}