/**
* scripts/slim-kb-workspace-view.mjs
* 将 KbWorkspaceView.vue 的 script 替换为 useKbWorkspace 薄壳(kb 单对象绑定)。
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const composablePath = path.resolve(__dirname, '../src/features/kb/composables/useKbWorkspace.ts');
const vuePath = path.resolve(__dirname, '../src/features/kb/views/KbWorkspaceView.vue');
const composable = fs.readFileSync(composablePath, 'utf8');
const returnStart = composable.lastIndexOf('\n return {');
const returnBody = composable.slice(returnStart + '\n return {'.length, composable.lastIndexOf('\n };'));
const keys = returnBody
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.map((line) => {
const alias = line.match(/^(\w+):\s*(\w+),?$/);
if (alias) return alias[1];
return line.replace(/,$/, '');
});
const script = ``;
let vue = fs.readFileSync(vuePath, 'utf8');
let template = vue.match(/[\s\S]*/)?.[0];
if (!template) throw new Error('template not found');
// 按 key 长度降序,避免短名误替换长名前缀
const sortedKeys = [...keys].sort((a, b) => b.length - a.length);
for (const key of sortedKeys) {
const re = new RegExp(`(?