am-freescan/am-web/scripts/port-cmvr.cjs
2026-10-22 16:18:37 +08:00

62 lines
3.4 KiB
JavaScript

// One-time, read-only source import. Runtime files never reference source folders.
const fs = require('node:fs');
const path = require('node:path');
const crypto = require('node:crypto');
const target = path.resolve(__dirname, '..');
const roots = {
common: 'E:/workspace/frontend/cmvr-iot-frontend/cmvr-iot-common',
aima: 'E:/workspace/frontend/cmvr-iot-frontend/CMVR-AIMA'
};
const inventory = [];
const globalTags = ['Pagination', 'DictTag', 'FileUpload', 'ImageUpload', 'ImagePreview', 'RightToolbar', 'Editor', 'SvgIcon'];
function visit(dir, fn) {
for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
const file = path.join(dir, item.name);
if (item.isDirectory()) visit(file, fn); else fn(file);
}
}
for (const [name, root] of Object.entries(roots)) {
const source = path.join(root, 'src');
visit(source, file => {
const relative = path.relative(source, file).replaceAll('\\', '/');
const out = path.join(target, 'src/cmvr', name, relative);
const original = fs.readFileSync(file);
inventory.push({ source: `${name}/src/${relative}`, target: `src/cmvr/${name}/${relative}`, sha256: crypto.createHash('sha256').update(original).digest('hex'), bytes: original.length });
fs.mkdirSync(path.dirname(out), { recursive: true });
if (/\.(vue|js|ts|scss|css|json)$/.test(file)) {
let text = original.toString('utf8').replaceAll('@/', `@/cmvr/${name}/`);
// RuoYi component contracts differ from Yudao's components with the same names.
if (file.endsWith('.vue')) for (const tag of globalTags) {
const kebab = tag.replace(/[A-Z]/g, (c, i) => (i ? '-' : '') + c.toLowerCase());
text = text.replace(new RegExp(`<(\\/?)(${tag}|${kebab})(?=[\\s>\\/])`, 'g'), `<$1Cmvr${tag}`);
}
text = text.replaceAll('import.meta.env.VITE_APP_BASE_API', '(import.meta.env.VITE_BASE_URL + "/admin-api/cmvr")');
text = text.replaceAll("'/public/models/'", "'/cmvr/models/'");
// Source uses auto-imported Pinia APIs and VueUse helpers.
if (/\.ts$/.test(file)) text = '// @ts-nocheck -- imported CMVR module; validated by Vite and runtime integration.\n' + text;
fs.writeFileSync(out, text);
} else fs.writeFileSync(out, original);
});
}
const publicRoot = path.join(roots.aima, 'public');
visit(publicRoot, file => {
const relative = path.relative(publicRoot, file).replaceAll('\\', '/');
const out = path.join(target, 'public/cmvr', relative);
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.copyFileSync(file, out);
const data = fs.readFileSync(file);
inventory.push({ source: `aima/public/${relative}`, target: `public/cmvr/${relative}`, sha256: crypto.createHash('sha256').update(data).digest('hex'), bytes: data.length });
});
fs.mkdirSync(path.join(target, 'docs'), { recursive: true });
fs.writeFileSync(path.join(target, 'docs/cmvr-source-inventory.json'), JSON.stringify(inventory, null, 2) + '\n');
const packageJson = path.join(target, 'package.json');
const pkg = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
for (const root of Object.values(roots)) {
const src = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
for (const [name, version] of Object.entries(src.dependencies)) {
if (name !== '@cmvr/iot-common' && !pkg.dependencies[name]) pkg.dependencies[name] = version;
}
}
fs.writeFileSync(packageJson, JSON.stringify(pkg, null, 2) + '\n');
console.log(`Copied ${inventory.length} source and public files; sources were read only.`);