feat: 添加Common层生成器,迁移GlobalConfig和WebAppEnvs
✅ 新增文件: - tools/.../generators/common-generator.js ✅ 修改文件: - migration-coordinator.js: 集成CommonGenerator - config.converter.js: 保留原样(不转换) 📦 Common层生成器功能: 1. 扫描Java的common目录(GlobalConfig、WebAppEnvs) 2. 生成对应的TypeScript配置类到 wwjcloud-core/src/common/ 3. GlobalConfig: 全局业务配置(表前缀、域名、语言等) 4. WebAppEnvs: 应用路径环境配置 🔄 迁移流程更新: 第1阶段: 扫描Java项目 第2阶段: 映射层级关系 第3阶段: 生成Common层配置类 ⬅️ 新增 第4阶段: 生成NestJS模块 第5阶段: 生成迁移报告 📋 架构说明: - Java的 com.niu.core.common.config/component → NestJS的 @wwjCore/common - 这些是业务配置类,不是框架配置! - 框架配置在 @wwjBoot/config(数据库、Redis等)
This commit is contained in:
@@ -152,11 +152,6 @@ class ServiceMethodConverter {
|
||||
// 【Boot层工具类】
|
||||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
// ConfigService
|
||||
if (this.config.needsConfigService(convertedBody)) {
|
||||
imports.boot.add('ConfigService');
|
||||
}
|
||||
|
||||
// StringUtils
|
||||
const stringImports = this.string.analyzeImports(convertedBody);
|
||||
stringImports.forEach(imp => imports.boot.add(imp));
|
||||
@@ -169,6 +164,12 @@ class ServiceMethodConverter {
|
||||
const jsonImports = this.json.analyzeImports(convertedBody);
|
||||
jsonImports.forEach(imp => imports.boot.add(imp));
|
||||
|
||||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
// 【Core层业务配置类】(未迁移,暂时标记TODO)
|
||||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
const configImports = this.config.analyzeImports(convertedBody);
|
||||
// 注意:WebAppEnvs/GlobalConfig未来需要从@wwjCore导入,暂时不添加
|
||||
|
||||
return {
|
||||
nestjs: Array.from(imports.nestjs),
|
||||
boot: Array.from(imports.boot),
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
/**
|
||||
* 配置访问转换器
|
||||
*
|
||||
* WebAppEnvs / GlobalConfig → ConfigService
|
||||
* WebAppEnvs / GlobalConfig → ✅ 保留原样(Core层业务配置类)
|
||||
*
|
||||
* 重要说明:
|
||||
* - WebAppEnvs:应用路径环境配置(projectRoot、webRoot等)
|
||||
* - GlobalConfig:全局业务配置(表前缀、域名、语言等)
|
||||
* - 这些是业务配置,不是框架配置!
|
||||
* - ❌ 不应该转换为 NestJS ConfigService(框架配置)
|
||||
* - ✅ 保留原样,它们会被迁移到 @wwjCore 层
|
||||
*/
|
||||
class ConfigConverter {
|
||||
/**
|
||||
@@ -10,42 +17,31 @@ class ConfigConverter {
|
||||
convert(javaCode) {
|
||||
let tsCode = javaCode;
|
||||
|
||||
// 1. WebAppEnvs.get().property.exists() → fs.existsSync(this.config.get('property'))
|
||||
tsCode = tsCode.replace(/WebAppEnvs\.get\(\)\.(\w+)\.exists\(\)/g, (match, prop) => {
|
||||
return `fs.existsSync(this.config.get('${prop}'))`;
|
||||
});
|
||||
|
||||
// 2. WebAppEnvs.get().property → this.config.get('property')
|
||||
tsCode = tsCode.replace(/WebAppEnvs\.get\(\)\.(\w+)/g, (match, prop) => {
|
||||
return `this.config.get('${prop}')`;
|
||||
});
|
||||
|
||||
// 3. WebAppEnvs.get() → this.config
|
||||
tsCode = tsCode.replace(/WebAppEnvs\.get\(\)/g, 'this.config');
|
||||
|
||||
// 4. WebAppEnvs.staticProperty → this.config.get('staticProperty')
|
||||
tsCode = tsCode.replace(/WebAppEnvs\.(\w+)/g, (match, prop) => {
|
||||
return `this.config.get('${prop}')`;
|
||||
});
|
||||
|
||||
// 5. GlobalConfig.property.exists() → fs.existsSync(this.config.get('property'))
|
||||
tsCode = tsCode.replace(/GlobalConfig\.(\w+)\.exists\(\)/g, (match, prop) => {
|
||||
return `fs.existsSync(this.config.get('${prop}'))`;
|
||||
});
|
||||
|
||||
// 6. GlobalConfig.property → this.config.get('property')
|
||||
tsCode = tsCode.replace(/GlobalConfig\.(\w+)/g, (match, prop) => {
|
||||
return `this.config.get('${prop}')`;
|
||||
});
|
||||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
// 【策略】保留原样,不做任何转换!
|
||||
// WebAppEnvs 和 GlobalConfig 会作为业务类迁移到 Core 层
|
||||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
// 不做任何转换,直接返回原代码
|
||||
return tsCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析是否需要ConfigService
|
||||
* 分析需要的imports
|
||||
*/
|
||||
needsConfigService(tsCode) {
|
||||
return tsCode.includes('this.config.');
|
||||
analyzeImports(tsCode) {
|
||||
const imports = new Set();
|
||||
|
||||
// 检查是否使用了WebAppEnvs或GlobalConfig
|
||||
// 这些将来会从 @wwjCore/common 导入
|
||||
if (tsCode.includes('WebAppEnvs')) {
|
||||
imports.add('WebAppEnvs');
|
||||
}
|
||||
if (tsCode.includes('GlobalConfig')) {
|
||||
imports.add('GlobalConfig');
|
||||
}
|
||||
|
||||
return Array.from(imports);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Common层生成器
|
||||
*
|
||||
* 负责迁移Java common层的配置类和工具类到NestJS Core层
|
||||
* 包括:GlobalConfig、WebAppEnvs等业务配置类
|
||||
*/
|
||||
class CommonGenerator {
|
||||
constructor() {
|
||||
this.javaPath = '';
|
||||
this.outputPath = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Common层文件
|
||||
*/
|
||||
async generate(scanResults, outputPath) {
|
||||
console.log('📦 开始生成Common层配置类...');
|
||||
this.outputPath = outputPath;
|
||||
|
||||
const commonClasses = scanResults.common || [];
|
||||
let generated = 0;
|
||||
|
||||
for (const commonClass of commonClasses) {
|
||||
const className = commonClass.className;
|
||||
|
||||
// 只处理GlobalConfig和WebAppEnvs
|
||||
if (className === 'GlobalConfig' || className === 'WebAppEnvs') {
|
||||
await this.generateCommonClass(commonClass);
|
||||
generated++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ 生成了 ${generated} 个Common配置类`);
|
||||
return generated;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Common配置类
|
||||
*/
|
||||
async generateCommonClass(commonClass) {
|
||||
const className = commonClass.className;
|
||||
console.log(` 📄 生成 ${className}...`);
|
||||
|
||||
let content = '';
|
||||
|
||||
if (className === 'GlobalConfig') {
|
||||
content = this.generateGlobalConfig(commonClass);
|
||||
} else if (className === 'WebAppEnvs') {
|
||||
content = this.generateWebAppEnvs(commonClass);
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
const fileName = this.toKebabCase(className) + '.ts';
|
||||
const filePath = path.join(this.outputPath, 'common', fileName);
|
||||
|
||||
// 确保目录存在
|
||||
const dir = path.dirname(filePath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
console.log(` ✅ 已生成: ${filePath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成GlobalConfig类
|
||||
*/
|
||||
generateGlobalConfig(commonClass) {
|
||||
return `import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
/**
|
||||
* 全局配置
|
||||
*
|
||||
* 从Java GlobalConfig迁移
|
||||
* 原路径: com.niu.core.common.config.GlobalConfig
|
||||
*/
|
||||
@Injectable()
|
||||
export class GlobalConfig {
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
/**
|
||||
* 表前缀配置
|
||||
*/
|
||||
static tablePrefix = 'nc_';
|
||||
|
||||
/**
|
||||
* 配置的默认语言包
|
||||
*/
|
||||
static defaultLanguage = 'zh_CN';
|
||||
|
||||
/**
|
||||
* 应用名称
|
||||
*/
|
||||
static applicationName: string;
|
||||
|
||||
/**
|
||||
* 运行环境(dev、prod)
|
||||
*/
|
||||
static runActive: string;
|
||||
|
||||
/**
|
||||
* 后端域名
|
||||
*/
|
||||
static adminDomain: string;
|
||||
|
||||
/**
|
||||
* 手机端域名
|
||||
*/
|
||||
static wapDomain: string;
|
||||
|
||||
/**
|
||||
* web端域名
|
||||
*/
|
||||
static webDomain: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
static version = '1.0.1';
|
||||
|
||||
/**
|
||||
* 应用key
|
||||
*/
|
||||
static appKey = 'niucloud-admin';
|
||||
|
||||
/**
|
||||
* 默认访问路径
|
||||
*/
|
||||
static defaultAccessPath: string;
|
||||
|
||||
/**
|
||||
* 是否是演示站点
|
||||
*/
|
||||
static isDemo = false;
|
||||
|
||||
/**
|
||||
* 获取默认语言
|
||||
*/
|
||||
static language(): string {
|
||||
return GlobalConfig.defaultLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认访问路径
|
||||
*/
|
||||
static getDefaultAccessPath(): string {
|
||||
return GlobalConfig.defaultAccessPath;
|
||||
}
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成WebAppEnvs类
|
||||
*/
|
||||
generateWebAppEnvs(commonClass) {
|
||||
return `/**
|
||||
* Web应用环境配置
|
||||
*
|
||||
* 从Java WebAppEnvs迁移
|
||||
* 原路径: com.niu.core.common.component.context.WebAppEnvs
|
||||
*/
|
||||
export class WebAppEnvs {
|
||||
envType: string;
|
||||
projectRoot: string;
|
||||
projectNiucloudAddon: string;
|
||||
webRoot: string;
|
||||
webRootDownAddon: string;
|
||||
webRootDownJar: string;
|
||||
webRootDownPublic: string;
|
||||
webRootDownResource: string;
|
||||
webRootDownRuntime: string;
|
||||
|
||||
/**
|
||||
* 获取应用环境配置
|
||||
*/
|
||||
static get(active?: string): WebAppEnvs {
|
||||
const runActive = active || process.env.NODE_ENV || 'dev';
|
||||
const appEnvs = new WebAppEnvs();
|
||||
|
||||
if (runActive === 'dev') {
|
||||
appEnvs.envType = 'dev';
|
||||
appEnvs.projectRoot = process.cwd();
|
||||
appEnvs.projectNiucloudAddon = appEnvs.projectRoot + '/niucloud-addon/';
|
||||
appEnvs.webRoot = appEnvs.projectRoot + '/webroot/';
|
||||
appEnvs.webRootDownAddon = appEnvs.projectRoot + '/webroot/addon/';
|
||||
appEnvs.webRootDownJar = appEnvs.projectRoot + '/webroot/jar/';
|
||||
appEnvs.webRootDownPublic = appEnvs.projectRoot + '/webroot/public/';
|
||||
appEnvs.webRootDownResource = appEnvs.projectRoot + '/webroot/resource/';
|
||||
appEnvs.webRootDownRuntime = appEnvs.projectRoot + '/webroot/runtime/';
|
||||
} else {
|
||||
appEnvs.envType = 'prod';
|
||||
// TODO: 生产环境路径配置需要根据实际部署调整
|
||||
appEnvs.webRoot = process.cwd() + '/webroot/';
|
||||
appEnvs.projectRoot = process.cwd();
|
||||
appEnvs.projectNiucloudAddon = appEnvs.projectRoot + '/niucloud-addon/';
|
||||
appEnvs.webRootDownAddon = appEnvs.projectRoot + '/webroot/addon/';
|
||||
appEnvs.webRootDownJar = appEnvs.projectRoot + '/webroot/jar/';
|
||||
appEnvs.webRootDownPublic = appEnvs.projectRoot + '/webroot/public/';
|
||||
appEnvs.webRootDownResource = appEnvs.projectRoot + '/webroot/resource/';
|
||||
appEnvs.webRootDownRuntime = appEnvs.projectRoot + '/webroot/runtime/';
|
||||
}
|
||||
|
||||
return appEnvs;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return \`WebAppEnvs
|
||||
{
|
||||
envType='\${this.envType}',
|
||||
projectRoot='\${this.projectRoot}',
|
||||
projectNiucloudAddon='\${this.projectNiucloudAddon}',
|
||||
webRoot='\${this.webRoot}',
|
||||
webRootDownAddon='\${this.webRootDownAddon}',
|
||||
webRootDownJar='\${this.webRootDownJar}',
|
||||
webRootDownPublic='\${this.webRootDownPublic}',
|
||||
webRootDownResource='\${this.webRootDownResource}',
|
||||
webRootDownRuntime='\${this.webRootDownRuntime}'
|
||||
}\`;
|
||||
}
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为kebab-case
|
||||
*/
|
||||
toKebabCase(str) {
|
||||
return str
|
||||
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
||||
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
|
||||
.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CommonGenerator;
|
||||
|
||||
@@ -5,6 +5,7 @@ const path = require('path');
|
||||
const JavaScanner = require('./scanners/java-scanner');
|
||||
const LayerMapper = require('./mappers/layer-mapper');
|
||||
const ModuleGenerator = require('./generators/module-generator');
|
||||
const CommonGenerator = require('./generators/common-generator');
|
||||
|
||||
/**
|
||||
* Java到NestJS迁移协调器
|
||||
@@ -17,12 +18,14 @@ class JavaToNestJSMigrationCoordinator {
|
||||
this.scanner = new JavaScanner();
|
||||
this.mapper = new LayerMapper();
|
||||
this.moduleGenerator = new ModuleGenerator();
|
||||
this.commonGenerator = new CommonGenerator();
|
||||
|
||||
this.stats = {
|
||||
startTime: null,
|
||||
endTime: null,
|
||||
filesProcessed: 0,
|
||||
modulesGenerated: 0,
|
||||
commonGenerated: 0,
|
||||
errors: []
|
||||
};
|
||||
}
|
||||
@@ -43,12 +46,16 @@ class JavaToNestJSMigrationCoordinator {
|
||||
console.log('\n🔄 第2阶段:映射层级关系...');
|
||||
const nestJSModules = this.mapLayers();
|
||||
|
||||
// 第3阶段:生成NestJS模块
|
||||
console.log('\n🔧 第3阶段:生成NestJS模块...');
|
||||
// 第3阶段:生成Common层配置类
|
||||
console.log('\n📦 第3阶段:生成Common层配置类...');
|
||||
await this.generateCommonLayer();
|
||||
|
||||
// 第4阶段:生成NestJS模块
|
||||
console.log('\n🔧 第4阶段:生成NestJS模块...');
|
||||
await this.generateModules(nestJSModules);
|
||||
|
||||
// 第4阶段:生成报告
|
||||
console.log('\n📋 第4阶段:生成迁移报告...');
|
||||
// 第5阶段:生成报告
|
||||
console.log('\n📋 第5阶段:生成迁移报告...');
|
||||
this.generateReport();
|
||||
|
||||
this.stats.endTime = new Date();
|
||||
@@ -250,6 +257,17 @@ class JavaToNestJSMigrationCoordinator {
|
||||
return nestJSModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Common层配置类
|
||||
*/
|
||||
async generateCommonLayer() {
|
||||
const scanResults = this.scanner.getScanResults();
|
||||
const generated = await this.commonGenerator.generate(scanResults, this.nestJSPath);
|
||||
|
||||
this.stats.commonGenerated = generated;
|
||||
console.log(`✅ 生成了 ${generated} 个Common配置类`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成NestJS模块
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user