0
0

删除了数据保存中的内容项

This commit is contained in:
xubing
2025-12-04 10:16:57 +08:00
parent 5aae34ea36
commit 7688c54d4a
11 changed files with 95 additions and 55 deletions

View File

@@ -1,3 +1,4 @@
// src/panels/ConfigPanel.ts
import * as vscode from 'vscode';
import * as path from 'path';
import { ProjectView } from './views/ProjectView';
@@ -358,8 +359,8 @@ export class ConfigPanel {
'openRepoSelectForUpload': (data) => this.openUploadRepoSelect(data.folderId, 'local'),
'uploadLocalModuleFolder': (data) => this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName),
'openRepoSelectForGitUpload': (data) => this.openUploadRepoSelect(data.folderId, 'git'),
// 旧的 repoSelectedForGitUpload 保留兼容的话可以转发到 handleRepoSelectedForBranches但现在不再使用
// 新增:上传时仓库 + 分支选择确认
// 上传时 仓库+分支 选择确认
'uploadRepoSelected': (data) => this.handleUploadRepoSelected(
data.folderId,
data.folderType,
@@ -544,6 +545,7 @@ export class ConfigPanel {
}
private async createConfig(name: string): Promise<void> {
// 注意:真正创建文件内容的逻辑放在 ProjectService.createConfig 里处理
const configId = await this.projectService.createConfig(name, this.currentContainerId);
vscode.window.showInformationMessage(`新建配置: ${name}`);
await this.saveCurrentProjectData();
@@ -566,7 +568,7 @@ export class ConfigPanel {
}
try {
// 删除配置文件
// 删除配置文件(磁盘)
await this.projectService.deleteConfigFileFromDisk(configId);
// 从内存中删除
@@ -606,7 +608,7 @@ export class ConfigPanel {
return;
}
// 获取选中的配置
// 获取选中的配置(仅元信息,不含 content
const selectedConfigs = configIds
.map(id => this.projectService.getConfig(id))
.filter(Boolean);
@@ -620,17 +622,23 @@ export class ConfigPanel {
const mergeFolderPath = path.join(projectPath, this.getAircraftName(), container.name, folderName);
await vscode.workspace.fs.createDirectory(vscode.Uri.file(mergeFolderPath));
// 复制文件到合并文件夹
const fs = require('fs');
// 复制文件到合并文件夹:完全基于磁盘文件
for (const config of selectedConfigs) {
if (!config) continue;
const sourcePath = this.projectService.getConfigFilePath(config.id);
const targetPath = path.join(mergeFolderPath, config.fileName);
if (sourcePath && require('fs').existsSync(sourcePath)) {
await require('fs').promises.copyFile(sourcePath, targetPath);
if (sourcePath && fs.existsSync(sourcePath)) {
await fs.promises.copyFile(sourcePath, targetPath);
} else {
await require('fs').promises.writeFile(targetPath, config.content || '');
// 找不到源文件:写一个占位注释文件
const placeholder =
`# 原配置文件 "${config.fileName}" 未在磁盘中找到\n` +
`# 仅保留占位文件,建议手动补充内容\n\n`;
await fs.promises.writeFile(targetPath, placeholder, 'utf8');
}
}
@@ -646,10 +654,10 @@ export class ConfigPanel {
this.projectService.addModuleFolder(newFolder);
// 删除原配置
// 删除原配置(包括磁盘上的配置文件)
for (const configId of configIds) {
this.projectService.deleteConfig(configId);
await this.projectService.deleteConfigFileFromDisk(configId);
this.projectService.deleteConfig(configId);
}
await this.saveCurrentProjectData();
@@ -1017,17 +1025,25 @@ export class ConfigPanel {
return;
}
const fileFullPath = path.join(fullPath, filePath);
const content = await require('fs').promises.readFile(fileFullPath, 'utf8');
const fs = require('fs');
const sourceFullPath = path.join(fullPath, filePath);
const content = await fs.promises.readFile(sourceFullPath, 'utf8');
const fileName = path.basename(filePath);
// 1. 先创建一个配置ProjectService.createConfig 内部会在磁盘上生成文件)
const configId = await this.projectService.createConfig(fileName, this.currentContainerId);
const config = this.projectService.getConfig(configId);
if (config) {
config.content = content;
// 2. 再把 Git 文件的内容写到该配置文件上,覆盖默认模板
const targetConfigPath = this.projectService.getConfigFilePath(configId);
if (!targetConfigPath) {
vscode.window.showErrorMessage('无法获取新配置文件的路径');
return;
}
const dirPath = path.dirname(targetConfigPath);
await fs.promises.mkdir(dirPath, { recursive: true });
await fs.promises.writeFile(targetConfigPath, content, 'utf8');
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`文件已导入: ${fileName}`);
@@ -1107,7 +1123,8 @@ export class ConfigPanel {
try {
progress.report({ increment: 0, message: '检查目录...' });
if (!require('fs').existsSync(fullPath)) {
const fs = require('fs');
if (!fs.existsSync(fullPath)) {
throw new Error('本地文件夹不存在');
}
@@ -1143,8 +1160,9 @@ export class ConfigPanel {
try {
const gitDir = path.join(fullPath, '.git');
if (require('fs').existsSync(gitDir)) {
await require('fs').promises.rm(gitDir, { recursive: true, force: true });
const fs = require('fs');
if (fs.existsSync(gitDir)) {
await fs.promises.rm(gitDir, { recursive: true, force: true });
}
} catch (cleanupError) {
console.error('清理 .git 文件夹失败:', cleanupError);
@@ -1224,7 +1242,15 @@ export class ConfigPanel {
vscode.window.showWarningMessage('配置文件不存在,将创建新文件');
const dirPath = path.dirname(filePath);
await fs.promises.mkdir(dirPath, { recursive: true });
await fs.promises.writeFile(filePath, config.content || '');
const now = new Date();
const header =
`# ${config.fileName} 配置文件\n` +
`# 创建时间: ${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()} ` +
`${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}\n` +
`# 您可以在此编辑配置内容\n\n`;
await fs.promises.writeFile(filePath, header, 'utf8');
}
const document = await vscode.workspace.openTextDocument(filePath);
@@ -1244,7 +1270,8 @@ export class ConfigPanel {
try {
const fullPath = this.projectService.getModuleFolderFullPath(folder);
if (!fullPath || !require('fs').existsSync(fullPath)) {
const fs = require('fs');
if (!fullPath || !fs.existsSync(fullPath)) {
vscode.window.showErrorMessage('模块文件夹目录不存在');
return;
}
@@ -1285,7 +1312,8 @@ export class ConfigPanel {
const newFullPath = path.join(path.dirname(fullPath), newName);
try {
await require('fs').promises.rename(fullPath, newFullPath);
const fs = require('fs');
await fs.promises.rename(fullPath, newFullPath);
this.projectService.renameModuleFolder(folderId, newName);
await this.saveCurrentProjectData();