生产管理系前端 - 删除不需要的文件夹,比如apis和utils
This commit is contained in:
@@ -46,6 +46,113 @@ function logInfo(message) {
|
||||
logInfo(`当前环境: ${process.env.NODE_ENV || 'development'}`);
|
||||
logInfo(`API 服务器: ${process.env.API_BASE_URL || 'http://localhost:8080'}`);
|
||||
|
||||
/**
|
||||
* 检查自定义文件是否存在
|
||||
* 如果某些文件已经被自定义,我们不希望覆盖它们
|
||||
*/
|
||||
function checkCustomFiles() {
|
||||
const customFiles = [
|
||||
'client.gen.ts' // 客户端文件通常是自定义的
|
||||
];
|
||||
|
||||
const outputDir = path.join(process.cwd(), 'src', 'lib', 'api');
|
||||
const existingCustomFiles = [];
|
||||
|
||||
for (const file of customFiles) {
|
||||
const filePath = path.join(outputDir, file);
|
||||
if (fs.existsSync(filePath)) {
|
||||
// 检查文件是否包含自定义内容的标识
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
// 检查是否有自定义配置的标识
|
||||
const customIndicators = [
|
||||
'getBaseUrl',
|
||||
'createDynamicClient',
|
||||
'getCurrentClientConfig',
|
||||
// 可以添加更多自定义标识
|
||||
];
|
||||
|
||||
const hasCustomContent = customIndicators.some(indicator =>
|
||||
content.includes(indicator)
|
||||
);
|
||||
|
||||
if (hasCustomContent) {
|
||||
existingCustomFiles.push(file);
|
||||
logWarning(`检测到自定义文件: ${file}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return existingCustomFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 备份自定义文件
|
||||
*/
|
||||
function backupCustomFiles(customFiles) {
|
||||
if (customFiles.length === 0) return;
|
||||
|
||||
logInfo('备份自定义文件...');
|
||||
const outputDir = path.join(process.cwd(), 'src', 'lib', 'api');
|
||||
const backupDir = path.join(process.cwd(), 'src', 'lib', 'api', 'backup');
|
||||
|
||||
// 创建备份目录
|
||||
if (!fs.existsSync(backupDir)) {
|
||||
fs.mkdirSync(backupDir, { recursive: true });
|
||||
}
|
||||
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const timestampedBackupDir = path.join(backupDir, `backup-${timestamp}`);
|
||||
fs.mkdirSync(timestampedBackupDir);
|
||||
|
||||
for (const file of customFiles) {
|
||||
const srcPath = path.join(outputDir, file);
|
||||
const destPath = path.join(timestampedBackupDir, file);
|
||||
fs.copyFileSync(srcPath, destPath);
|
||||
logInfo(` 备份: ${file} -> backup/${timestamp}/${file}`);
|
||||
}
|
||||
|
||||
logSuccess(`自定义文件已备份到: backup/${timestamp}/`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复自定义文件
|
||||
*/
|
||||
function restoreCustomFiles(customFiles) {
|
||||
if (customFiles.length === 0) return;
|
||||
|
||||
logInfo('恢复自定义文件...');
|
||||
const outputDir = path.join(process.cwd(), 'src', 'lib', 'api');
|
||||
const backupDir = path.join(process.cwd(), 'src', 'lib', 'api', 'backup');
|
||||
|
||||
// 找到最新的备份目录
|
||||
const backupDirs = fs.existsSync(backupDir)
|
||||
? fs.readdirSync(backupDir)
|
||||
.filter(name => name.startsWith('backup-'))
|
||||
.sort()
|
||||
.reverse()
|
||||
: [];
|
||||
|
||||
if (backupDirs.length === 0) {
|
||||
logWarning('没有找到备份文件,跳过恢复');
|
||||
return;
|
||||
}
|
||||
|
||||
const latestBackupDir = path.join(backupDir, backupDirs[0]);
|
||||
|
||||
for (const file of customFiles) {
|
||||
const srcPath = path.join(latestBackupDir, file);
|
||||
const destPath = path.join(outputDir, file);
|
||||
|
||||
if (fs.existsSync(srcPath)) {
|
||||
fs.copyFileSync(srcPath, destPath);
|
||||
logInfo(` 恢复: ${file}`);
|
||||
}
|
||||
}
|
||||
|
||||
logSuccess('自定义文件已恢复');
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 openapi-ts 命令生成客户端代码
|
||||
*/
|
||||
@@ -144,9 +251,22 @@ async function main() {
|
||||
log('='.repeat(60), 'cyan');
|
||||
|
||||
try {
|
||||
// 检查是否有自定义文件
|
||||
const customFiles = checkCustomFiles();
|
||||
|
||||
if (customFiles.length > 0) {
|
||||
logInfo('检测到自定义文件,将在生成前进行备份');
|
||||
backupCustomFiles(customFiles);
|
||||
}
|
||||
|
||||
// 使用 openapi-ts 生成代码
|
||||
await generateWithOpenApiTS();
|
||||
|
||||
// 恢复自定义文件
|
||||
if (customFiles.length > 0) {
|
||||
restoreCustomFiles(customFiles);
|
||||
}
|
||||
|
||||
// 验证生成的文件
|
||||
if (!validateGeneratedFiles()) {
|
||||
logWarning('文件验证发现问题,但生成过程已完成');
|
||||
@@ -156,6 +276,10 @@ async function main() {
|
||||
logSuccess(`API 客户端代码生成完成!总耗时: ${totalTime}ms`);
|
||||
logSuccess('生成的文件位于 src/lib/api/ 目录');
|
||||
|
||||
if (customFiles.length > 0) {
|
||||
logInfo('自定义文件已保持不变,避免覆盖手动配置');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
const totalTime = Date.now() - startTime;
|
||||
logError(`脚本执行失败 (${totalTime}ms): ${error.message}`);
|
||||
|
||||
Reference in New Issue
Block a user