Files
wwjcloud-nest-v1/wwjcloud-nest-v1/tools/java-to-nestjs-migration/utils/path-utils.js

309 lines
7.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const path = require('path');
/**
* 路径工具
* 处理文件路径和目录结构
*/
class PathUtils {
constructor() {
// NestJS项目标准目录结构
this.nestJSStructure = {
modules: 'modules/',
controllers: 'controllers/',
services: 'services/',
entities: 'entities/',
listeners: 'listeners/',
jobs: 'jobs/',
enums: 'enums/',
dtos: 'dtos/',
interfaces: 'interfaces/',
constants: 'constants/',
utils: 'utils/'
};
this.outputDir = '';
}
/**
* 设置输出目录
*/
setOutputDir(outputDir) {
this.outputDir = outputDir;
}
/**
* 获取NestJS项目根路径
*/
getNestJSRootPath() {
return this.outputDir || path.resolve(__dirname, '../../../../wwjcloud/libs/wwjcloud-core/src');
}
/**
* 获取模块目录路径
*/
getModulePath(moduleName) {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.modules);
}
/**
* 获取控制器目录路径
*/
getControllerPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.controllers);
}
/**
* 获取服务目录路径
*/
getServicePath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.services);
}
/**
* 获取实体目录路径
*/
getEntityPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.entities);
}
/**
* 获取监听器目录路径
*/
getListenerPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.listeners);
}
/**
* 获取任务目录路径
*/
getJobPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.jobs);
}
/**
* 获取枚举目录路径
*/
getEnumPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.enums);
}
/**
* 获取DTO目录路径
*/
getDtoPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.dtos);
}
/**
* 获取接口目录路径
*/
getInterfacePath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.interfaces);
}
/**
* 获取常量目录路径
*/
getConstantPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.constants);
}
/**
* 获取工具目录路径
*/
getUtilPath() {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, this.nestJSStructure.utils);
}
/**
* 生成完整的文件路径
*/
generateFilePath(componentType, fileName) {
const basePath = this.getPathByType(componentType);
return path.join(basePath, fileName);
}
/**
* 根据组件类型获取路径
*/
getPathByType(componentType) {
const pathMap = {
'controller': this.getControllerPath(),
'service': this.getServicePath(),
'entity': this.getEntityPath(),
'listener': this.getListenerPath(),
'job': this.getJobPath(),
'enum': this.getEnumPath(),
'dto': this.getDtoPath(),
'interface': this.getInterfacePath(),
'constant': this.getConstantPath(),
'util': this.getUtilPath()
};
return pathMap[componentType] || this.getNestJSRootPath();
}
/**
* 生成相对导入路径
*/
generateRelativeImportPath(fromPath, toPath) {
const relativePath = path.relative(path.dirname(fromPath), toPath);
return relativePath.replace(/\\/g, '/').replace(/\.ts$/, '');
}
/**
* 生成模块导入路径
*/
generateModuleImportPath(moduleName) {
return `./modules/${moduleName}`;
}
/**
* 生成组件导入路径
*/
generateComponentImportPath(componentType, componentName) {
const typePaths = {
'controller': 'controllers',
'service': 'services',
'entity': 'entities',
'listener': 'listeners',
'job': 'jobs',
'enum': 'enums',
'dto': 'dtos',
'interface': 'interfaces',
'constant': 'constants',
'util': 'utils'
};
const typePath = typePaths[componentType] || componentType;
return `./${typePath}/${componentName}`;
}
/**
* 确保目录存在
*/
ensureDirectoryExists(dirPath) {
const fs = require('fs');
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
/**
* 创建完整的目录结构
*/
createNestJSStructure() {
const fs = require('fs');
const rootPath = this.getNestJSRootPath();
// 只创建根目录,不预创建所有子目录
this.ensureDirectoryExists(rootPath);
console.log('✅ NestJS根目录创建完成');
}
/**
* 去除文件扩展名用于import路径
* 统一处理:'.service.ts' -> '', '.dto.ts' -> '', '.entity.ts' -> ''
*
* @param {string} fileName - 文件名(如 'xxx.service.ts'
* @returns {string} - 去除扩展名的文件名(如 'xxx'
*/
removeFileExtension(fileName) {
if (!fileName) return '';
// 移除 .ts, .js, .service.ts, .dto.ts, .entity.ts 等扩展名
return fileName.replace(/\.(service|dto|entity|controller|enum|listener|job|module)?\.(ts|js)$/, '');
}
/**
* 获取Java项目路径
*/
getJavaProjectPath() {
return path.resolve(__dirname, '../../../../../../niucloud-java/niucloud-core/src/main/java');
}
/**
* 验证路径是否存在
*/
pathExists(filePath) {
const fs = require('fs');
return fs.existsSync(filePath);
}
/**
* 获取文件扩展名
*/
getFileExtension(filePath) {
return path.extname(filePath);
}
/**
* 获取文件名(不含扩展名)
*/
getFileNameWithoutExtension(filePath) {
return path.basename(filePath, path.extname(filePath));
}
/**
* 获取目录名
*/
getDirectoryName(filePath) {
return path.dirname(filePath);
}
/**
* 标准化路径分隔符
*/
normalizePath(filePath) {
return filePath.replace(/\\/g, '/');
}
/**
* 生成包路径(用于导入)
*/
generatePackagePath(javaPackage) {
if (!javaPackage) return '';
// 将Java包名转换为路径
return javaPackage.replace(/\./g, '/');
}
/**
* 生成NestJS模块路径
*/
generateNestJSModulePath(moduleName) {
const rootPath = this.getNestJSRootPath();
return path.join(rootPath, 'modules', moduleName + '.module.ts');
}
/**
* 生成NestJS组件路径
*/
generateNestJSComponentPath(componentType, componentName) {
const rootPath = this.getNestJSRootPath();
const typePaths = {
'controller': 'controllers',
'service': 'services',
'entity': 'entities',
'listener': 'listeners',
'job': 'jobs',
'enum': 'enums',
'dto': 'dtos'
};
const typePath = typePaths[componentType] || componentType;
return path.join(rootPath, typePath, componentName);
}
}
module.exports = PathUtils;