Files
CN_Tool_client/frontend/src/layouts/components/Main/check-keep-alive-contract.mjs

73 lines
2.6 KiB
JavaScript
Raw Normal View History

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)
}