const fs = require('node:fs'); const path = require('node:path'); const root = path.resolve(__dirname, '..'); const write = (file, text) => { const full = path.join(root, file); fs.mkdirSync(path.dirname(full), { recursive: true }); fs.writeFileSync(full, text); }; const edit = (file, fn) => write(file, fn(fs.readFileSync(path.join(root, file), 'utf8'))); for (const name of ['common', 'aima']) { write(`src/cmvr/${name}/utils/request.js`, "export { default, download, isRelogin } from '@/cmvr/request'\n"); write(`src/cmvr/${name}/utils/auth.js`, "export { getAccessToken as getToken, removeToken } from '@/utils/auth'\nexport const setToken = () => { throw new Error('请通过招商车研三维尺寸快检平台登录') }\n"); write(`src/cmvr/${name}/router/index.js`, "export { default } from '@/router'\n"); write(`src/cmvr/${name}/store/index.js`, "export { store as default } from '@/store'\n"); // Do not let source user/session stores compete with FreeScan identity. write(`src/cmvr/${name}/store/modules/user.js`, `import { useUserStore as useTargetUserStore } from '@/store/modules/user'\nimport { getAccessToken } from '@/utils/auth'\nexport default function useUserStore() { const target = useTargetUserStore(); return { get id() { return target.user.id }, get token() { return getAccessToken() }, get roles() { return target.roles }, get permissions() { return Array.from(target.permissions) }, logOut: () => target.loginOut() } }\n`); } // Original common source has a spelling mismatch that prevents robot filtering. edit('src/cmvr/common/views/resource/device/index.vue', text => text.replaceAll('robotsLoading.value', 'robotLoading.value')); const pages = { 'intelligenceTest/testCase': ['aima/views/intelligenceTest/testCase/index.vue', 'CmvrTestCase'], 'intelligenceTest/taskManage': ['aima/views/intelligenceTest/taskManage/index.vue', 'CmvrTaskManage'], 'intelligenceTest/runningTask': ['aima/views/intelligenceTest/runningTask/index.vue', 'CmvrRunningTask'], 'intelligenceTest/car': ['aima/views/intelligenceTest/car/index.vue', 'CmvrCar'], 'intelligenceTest/mobilePhone': ['aima/views/intelligenceTest/mobilePhone/index.vue', 'CmvrMobilePhone'], 'resource/robot': ['common/views/resource/robot/index.vue', 'CmvrRobot'], 'resource/device': ['common/views/resource/device/index.vue', 'CmvrDevice'], 'resource/map': ['common/views/resource/map/index.vue', 'CmvrMap'], 'resource/terminal': ['common/views/resource/terminal/index.vue', 'CmvrTerminal'], 'resource/visionCalibration': ['common/views/resource/visionCalibration/index.vue', 'CmvrVisionCalibration'], }; for (const [page, [component, name]] of Object.entries(pages)) { write(`src/views/cmvr/${page}/index.vue`, `\n\n`); } write('src/views/cmvr/flow/index.vue', `\n\n`); edit('src/main.ts', text => text.replace("import App from './App.vue'", "import App from './App.vue'\nimport { setupCmvr } from '@/cmvr/setup'").replace('setupStore(app)', 'setupStore(app)\n\n setupCmvr(app)')); edit('build/vite/index.ts', text => text.replace("'vue-router',", "'vue-router',\n 'pinia',").replace("iconDirs: [pathResolve('src/assets/svgs')]", "iconDirs: [pathResolve('src/assets/svgs'), pathResolve('src/cmvr/common/assets/icons/svg')]")); edit('vite.config.ts', text => text.replace("alias: [", "alias: [\n { find: '@cmvr/iot-common', replacement: pathResolve('src/cmvr/bridge.js') },").replace("extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css']", "extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.scss', '.css']").replace('base: env.VITE_BASE_PATH,', 'base: env.VITE_BASE_PATH,\n define: { __PROJECT_NAME__: JSON.stringify("aima") },').replace('const normalizedFilename = normalizePath(filename)', `const normalizedFilename = normalizePath(filename)\n if (normalizedFilename.includes('/src/cmvr/')) {\n const cmvrVars = pathResolve('src/cmvr/common/style/variable.scss')\n if (normalizedFilename.endsWith('/style/variable.scss')) return source\n return \`@use "\${getRelativeScssUsePath(filename, cmvrVars)}" as *;\\n\${source}\`\n }`)); // Preserve all source teach and companion routes; expose the same paths used by original actions. write('src/cmvr/routes.ts', `import { Layout } from '@/utils/routerHelper'\nimport { createDeviceRoutes } from './common/router/deviceRoutes'\nconst originalDeviceRoutes = createDeviceRoutes(Layout).map((route: any) => ({ ...route, meta: { ...route.meta, hidden: true }, children: route.children?.map((child: any) => ({ ...child, meta: { ...child.meta, hidden: true, canTo: true } })) }))\nexport const cmvrRoutes = [\n { path: '/flow', name: 'CmvrFlow', component: () => import('@/views/cmvr/flow/index.vue'), meta: { hidden: true, canTo: true, title: '流程编排' } },\n ...originalDeviceRoutes,\n]\n`); edit('src/router/modules/remaining.ts', text => text.replace("import { Layout } from '@/utils/routerHelper'", "import { Layout } from '@/utils/routerHelper'\nimport { cmvrRoutes } from '@/cmvr/routes'").replace('const remainingRouter: AppRouteRecordRaw[] = [', 'const remainingRouter: AppRouteRecordRaw[] = [\n ...cmvrRoutes,')); // Keep the imported JavaScript business modules out of the host's strict TS project; // imported TS helpers carry @ts-nocheck while Vite validates the complete dependency graph. edit('tsconfig.json', text => text.replace('"paths": {', '"paths": {\n "@cmvr/iot-common": ["src/cmvr/bridge.js"],').replace('"exclude": ["dist", "target", "node_modules"]', '"exclude": ["dist", "target", "node_modules", "src/cmvr/common", "src/cmvr/aima"]')); console.log('CMVR source adapters, original-page wrappers, hidden routes, authentication and build hooks applied.');