0
0

修复了拉取出现的bug,完善了改名功能

This commit is contained in:
xubing
2025-12-06 18:05:35 +08:00
parent 46c184b920
commit 11c405f79a
10 changed files with 650 additions and 98 deletions

145
src/panels/services/ProjectService.ts Normal file → Executable file
View File

@@ -95,6 +95,10 @@ export class ProjectService {
getAircraftsByProject(projectId: string): Aircraft[] {
return this.aircrafts.filter(a => a.projectId === projectId);
}
getAircraft(aircraftId: string): Aircraft | undefined {
return this.aircrafts.find(a => a.id === aircraftId);
}
async createAircraft(name: string, projectId: string): Promise<string> {
const newId = this.generateUniqueId('a', this.aircrafts);
@@ -191,6 +195,10 @@ export class ProjectService {
getContainersByAircraft(aircraftId: string): Container[] {
return this.containers.filter(c => c.aircraftId === aircraftId);
}
getContainer(containerId: string): Container | undefined {
return this.containers.find(c => c.id === containerId);
}
async createContainer(name: string, aircraftId: string): Promise<string> {
const newId = this.generateUniqueId('c', this.containers);
@@ -232,10 +240,10 @@ export class ProjectService {
* 从已存在的磁盘目录导入容器
*
* ✅ 你的最新需求:
* - 不创建“默认两个配置”
* - 自动扫描容器目录下的『子文件夹』
* - 每个子文件夹创建一个 ModuleFoldertype: 'local'
* - 排除 `.git` 子文件夹
* - 不创建“默认两个配置”
* - 自动扫描容器目录下的『子文件夹』
* - 每个子文件夹创建一个 ModuleFoldertype: 'local'
* - 排除 `.git` 子文件夹
*/
async importContainerFromExistingFolder(aircraftId: string, containerName: string): Promise<string> {
if (containerName === '.git') {
@@ -388,7 +396,132 @@ export class ProjectService {
return true;
}
// =============== 文件系统操作 ===============
// =============== 文件系统操作 (新增/修改) ===============
/**
* 获取项目目录重命名所需的旧路径和新路径(新增)
*/
getProjectOldAndNewPaths(projectId: string, newName: string): { oldPath: string, newPath: string } | null {
const project = this.projects.find(p => p.id === projectId);
const oldPath = this.projectPaths.get(projectId);
if (!project || !oldPath) return null;
const parentDir = path.dirname(oldPath);
// 新路径使用新的项目名称作为文件夹名
const newPath = path.join(parentDir, newName);
return { oldPath, newPath };
}
/**
* 获取飞行器目录的完整路径
*/
getAircraftDirectoryPath(aircraftId: string): string | null {
const aircraft = this.getAircraft(aircraftId);
if (!aircraft) return null;
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) return null;
return path.join(projectPath, aircraft.name);
}
/**
* 获取飞行器目录重命名所需的旧路径和新路径(新增)
* 注意oldPath使用内存中当前的名称newPath使用将要更新的新名称
*/
getAircraftOldAndNewPaths(aircraftId: string, newName: string): { oldPath: string, newPath: string } | null {
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (!aircraft) return null;
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) return null;
const oldPath = path.join(projectPath, aircraft.name);
const newPath = path.join(projectPath, newName);
return { oldPath, newPath };
}
/**
* 获取容器目录的完整路径
*/
getContainerDirectoryPath(containerId: string): string | null {
const container = this.getContainer(containerId);
if (!container) return null;
const aircraft = this.getAircraft(container.aircraftId);
if (!aircraft) return null;
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) return null;
return path.join(projectPath, aircraft.name, container.name);
}
/**
* 获取容器目录重命名所需的旧路径和新路径(新增)
* 注意oldPath使用内存中当前的名称newPath使用将要更新的新名称
*/
getContainerOldAndNewPaths(containerId: string, newName: string): { oldPath: string, newPath: string } | null {
const container = this.containers.find(c => c.id === containerId);
if (!container) return null;
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) return null;
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) return null;
const aircraftDir = path.join(projectPath, aircraft.name);
const oldPath = path.join(aircraftDir, container.name);
const newPath = path.join(aircraftDir, newName);
return { oldPath, newPath };
}
/**
* 递归删除目录
*/
async deleteDirectoryFromDisk(directoryPath: string): Promise<boolean> {
if (!directoryPath) return false;
try {
if (fs.existsSync(directoryPath)) {
await fs.promises.rm(directoryPath, { recursive: true, force: true });
console.log(`✅ 已删除目录: ${directoryPath}`);
return true;
}
return false;
} catch (error) {
console.error(`删除目录失败: ${error}`);
throw error; // Re-throw to be handled by ConfigPanel
}
}
/**
* 重命名磁盘上的目录(新增)
*/
async renameDirectoryOnDisk(oldPath: string, newPath: string): Promise<boolean> {
if (!oldPath || !newPath) return false;
try {
if (fs.existsSync(oldPath)) {
await fs.promises.rename(oldPath, newPath);
console.log(`✅ 已重命名目录: ${oldPath} -> ${newPath}`);
return true;
}
console.warn(`目录不存在,跳过重命名: ${oldPath}`);
return false;
} catch (error) {
console.error(`重命名目录失败: ${error}`);
throw error; // Re-throw to be handled by ConfigPanel
}
}
private async createAircraftDirectory(aircraft: Aircraft): Promise<void> {
try {
@@ -610,4 +743,4 @@ export class ProjectService {
return false;
}
}
}
}