#!/usr/bin/env node
|
import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
|
import { join, relative, dirname } from 'node:path';
|
|
const projectRoot = 'D:/beforeProject/yudao-ui-admin-vben/web-antd-standalone';
|
|
// 递归获取所有文件
|
function getAllFiles(dir, files = [], extensions = ['.ts', '.tsx', '.vue', '.js', '.mjs', '.cjs', '.scss', '.css']) {
|
const entries = readdirSync(dir);
|
for (const entry of entries) {
|
const fullPath = join(dir, entry);
|
if (statSync(fullPath).isDirectory()) {
|
if (!['node_modules', 'dist', '.git'].includes(entry)) {
|
getAllFiles(fullPath, files, extensions);
|
}
|
} else if (extensions.some(ext => entry.endsWith(ext))) {
|
files.push(fullPath);
|
}
|
}
|
return files;
|
}
|
|
// 处理子路径引用
|
function processSubPathImport(fromFile, packageName, subPath) {
|
const fromDir = dirname(fromFile);
|
|
const packageBasePaths = {
|
'@vben/plugins': 'src/packages/effects/plugins/src',
|
'@vben/styles': 'src/packages/styles/src',
|
'@vben/stores': 'src/packages/stores/src',
|
'@vben/types': 'src/packages/types/src',
|
'@vben/icons': 'src/packages/icons/src',
|
'@vben/locales': 'src/packages/locales/src',
|
'@vben/utils': 'src/packages/utils/src',
|
'@vben/common-ui': 'src/packages/effects/common-ui/src',
|
'@vben/hooks': 'src/packages/effects/hooks/src',
|
'@vben/layouts': 'src/packages/effects/layouts/src',
|
'@vben/request': 'src/packages/effects/request/src',
|
'@vben/access': 'src/packages/effects/access/src',
|
'@vben/constants': 'src/packages/constants/src',
|
'@vben/preferences': 'src/packages/preferences/src',
|
'@vben/tailwind-config': 'src/internal/tailwind-config/src',
|
'@vben/node-utils': 'src/internal/node-utils/src',
|
'@vben/vite-config': 'src/internal/vite-config/src',
|
'@vben-core/composables': 'src/packages/@core/composables/src',
|
'@vben-core/design': 'src/packages/@core/base/design/src',
|
'@vben-core/form-ui': 'src/packages/@core/ui-kit/form-ui/src',
|
'@vben-core/icons': 'src/packages/@core/base/icons/src',
|
'@vben-core/layout-ui': 'src/packages/@core/ui-kit/layout-ui/src',
|
'@vben-core/menu-ui': 'src/packages/@core/ui-kit/menu-ui/src',
|
'@vben-core/popup-ui': 'src/packages/@core/ui-kit/popup-ui/src',
|
'@vben-core/preferences': 'src/packages/@core/preferences/src',
|
'@vben-core/shadcn-ui': 'src/packages/@core/ui-kit/shadcn-ui/src',
|
'@vben-core/shared': 'src/packages/@core/base/shared/src',
|
'@vben-core/tabs-ui': 'src/packages/@core/ui-kit/tabs-ui/src',
|
'@vben-core/typings': 'src/packages/@core/base/typings/src',
|
};
|
|
const basePath = packageBasePaths[packageName];
|
if (!basePath) return null;
|
|
// 处理特殊子路径
|
if (packageName === '@vben/plugins') {
|
const pluginSubPaths = {
|
'echarts': 'echarts',
|
'vxe-table': 'vxe-table',
|
'markmap': 'markmap',
|
'tinyflow': 'tinyflow',
|
'motion': 'motion',
|
'code-editor': 'code-editor',
|
'tiptap': 'tiptap',
|
};
|
const subDir = pluginSubPaths[subPath];
|
if (subDir) {
|
const targetPath = join(projectRoot, basePath, subDir);
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
}
|
|
if (packageName === '@vben/styles') {
|
const stylesSubPaths = {
|
'global': 'global',
|
'antd': 'antd',
|
};
|
const subDir = stylesSubPaths[subPath];
|
if (subDir) {
|
const targetPath = join(projectRoot, basePath, subDir);
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
}
|
|
if (packageName === '@vben/common-ui') {
|
// 处理 es 路径
|
if (subPath.startsWith('es/')) {
|
const esPath = subPath.replace('es/', '');
|
const targetPath = join(projectRoot, basePath, esPath);
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
}
|
|
if (packageName === '@vben/types') {
|
if (subPath === 'global') {
|
const targetPath = join(projectRoot, basePath, 'global.d.ts');
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
}
|
|
if (packageName === '@vben/tailwind-config') {
|
if (subPath === 'theme') {
|
const targetPath = join(projectRoot, basePath, 'theme.ts');
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
}
|
|
// 默认:直接拼接子路径
|
const targetPath = join(projectRoot, basePath, subPath);
|
const relPath = relative(fromDir, targetPath);
|
return relPath.startsWith('.') ? relPath : './' + relPath;
|
}
|
|
// 处理单个文件
|
function processFile(filePath) {
|
let content = readFileSync(filePath, 'utf-8');
|
let modified = false;
|
|
// 处理 @vben/plugins/echarts 等子路径引用
|
const subPathRegex = /from\s+['"](@vben(?:-core)?\/[^/]+\/[^'"]+)['"]/g;
|
const subPathRegex2 = /import\s*\(\s*['"](@vben(?:-core)?\/[^/]+\/[^'"]+)['"]\s*\)/g;
|
const scssImportRegex = /@use\s+['"](@vben\/[^'"]+)['"]/g;
|
const scssImportRegex2 = /@import\s+['"](@vben\/[^'"]+)['"]/g;
|
|
content = content.replace(subPathRegex, (match, fullImport) => {
|
const parts = fullImport.split('/');
|
const packageName = parts.slice(0, 2).join('/');
|
const subPath = parts.slice(2).join('/');
|
|
const relativePath = processSubPathImport(filePath, packageName, subPath);
|
if (relativePath) {
|
modified = true;
|
return `from '${relativePath}'`;
|
}
|
return match;
|
});
|
|
content = content.replace(subPathRegex2, (match, fullImport) => {
|
const parts = fullImport.split('/');
|
const packageName = parts.slice(0, 2).join('/');
|
const subPath = parts.slice(2).join('/');
|
|
const relativePath = processSubPathImport(filePath, packageName, subPath);
|
if (relativePath) {
|
modified = true;
|
return `import('${relativePath}')`;
|
}
|
return match;
|
});
|
|
// 处理 SCSS @use 和 @import
|
if (filePath.endsWith('.scss') || filePath.endsWith('.vue')) {
|
content = content.replace(scssImportRegex, (match, fullImport) => {
|
const parts = fullImport.split('/');
|
const packageName = parts.slice(0, 2).join('/');
|
const subPath = parts.slice(2).join('/');
|
|
const relativePath = processSubPathImport(filePath, packageName, subPath);
|
if (relativePath) {
|
modified = true;
|
return `@use '${relativePath}'`;
|
}
|
return match;
|
});
|
|
content = content.replace(scssImportRegex2, (match, fullImport) => {
|
const parts = fullImport.split('/');
|
const packageName = parts.slice(0, 2).join('/');
|
const subPath = parts.slice(2).join('/');
|
|
const relativePath = processSubPathImport(filePath, packageName, subPath);
|
if (relativePath) {
|
modified = true;
|
return `@import '${relativePath}'`;
|
}
|
return match;
|
});
|
}
|
|
if (modified) {
|
writeFileSync(filePath, content);
|
console.log(`Updated: ${filePath}`);
|
}
|
|
return modified;
|
}
|
|
// 主函数
|
const srcDir = join(projectRoot, 'src');
|
const files = getAllFiles(srcDir);
|
|
let updatedCount = 0;
|
for (const file of files) {
|
if (processFile(file)) {
|
updatedCount++;
|
}
|
}
|
|
console.log(`\nTotal files updated: ${updatedCount}`);
|