mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 18:15:55 +08:00
chore: 全面代码审查修复 + 开源标配完善
## 审查修复 (18 项)
- TitleBar 版本号改为动态 import package.json
- CLI profile_apply 加 verify_and_save 原子性保护
- CLI 新增 profile rename 子命令
- cmd_clean 默认清理 system+user 两个 hive
- Rust import_csv 加 BOM/header 处理
- exportToJson/exportToCsv 保留 enabled 状态
- CLI version 使用 env!("CARGO_PKG_VERSION")
- export_paths 返回 Result, 未知格式报错
- importFromContent 未知扩展名 throw Error
- profile 文件名加路径遍历/Win保留字校验
- 数据路径统一到 ~/.patheditor/
## clippy (18 处修复)
- backup/scanner/system/profiles: empty_line_after_doc_comments
- profiles: needless_borrow ×5, unnecessary_map_or
- scanner: collapsible_if
- cli: nonminimal_bool ×6, implicit_saturating_sub, to_string_in_format_args
- 零警告通过
## 测试 (33 条新增)
- Rust: backup(3) + disabled(1) + fs(13) + scanner(4) + profiles(1) = 25 条
- 前端: merge-preview(2) + analyze-dialog(1) + import-parity(5) = 8 条
- Rust 10→35, 前端 72→80
## Scanner 并行化
- std::thread::scope 多线程并行扫描目录,N 倍性能提升
## expand_env_vars UTF-16 修复
- 非法码点编码为 \u{XXXX} 而非静默丢弃
## 开源标配
- CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SECURITY.md (漏洞报告流程)
- .github/PULL_REQUEST_TEMPLATE.md
- CONTRIBUTING.md (贡献指南)
- CHANGELOG.md (v4.0~v5.0)
## E2E 测试 (4 条新增)
- keyboard / analyze / profiles / import-export
- IPC mock 扩展 scan/profiles 命令
## CI
- Rust job 目录调整为 workspace 根
## 其他
- rustdoc: 8 个 pub fn 补文档注释
- 帮助文本 v4.0→v5.0
- 前后端导入逻辑加交叉引用注释
- .gitignore 添加 target/
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+38
-21
@@ -1,7 +1,10 @@
|
||||
/**
|
||||
* 导入导出模块 — 对应 C 版 import_export.c
|
||||
* 支持 JSON、CSV、TXT 三种格式
|
||||
* 导入导出模块 — 支持 JSON、CSV、TXT 三种格式
|
||||
*
|
||||
* 注意:Rust 端 core/src/fs.rs 有对应的导入导出实现,
|
||||
* 前端使用此模块(需 ImportDialog 交互),CLI 使用 Rust 版,修改时需同步两端。
|
||||
*/
|
||||
import { version } from '../../package.json';
|
||||
import type { PathEntry } from './path-entry';
|
||||
|
||||
export type ExportFormat = 'json' | 'csv' | 'txt';
|
||||
@@ -23,11 +26,10 @@ export function detectExportFormat(filepath: string): ExportFormat {
|
||||
|
||||
export function exportToJson(data: ExportData): string {
|
||||
const obj = {
|
||||
version: '1.0',
|
||||
type: 'PathEditor',
|
||||
exported: new Date().toISOString(),
|
||||
system: data.system.map(e => e.path),
|
||||
user: data.user.map(e => e.path),
|
||||
version,
|
||||
timestamp: new Date().toISOString(),
|
||||
system: data.system.map(e => ({ path: e.path, enabled: e.enabled })),
|
||||
user: data.user.map(e => ({ path: e.path, enabled: e.enabled })),
|
||||
};
|
||||
return JSON.stringify(obj, null, 2);
|
||||
}
|
||||
@@ -37,13 +39,13 @@ export function exportToJson(data: ExportData): string {
|
||||
export function exportToCsv(data: ExportData): string {
|
||||
const lines: string[] = [];
|
||||
// UTF-8 BOM
|
||||
lines.push('type,path');
|
||||
lines.push('type,path,enabled');
|
||||
|
||||
for (const entry of data.system) {
|
||||
lines.push(`system,${escapeCsvField(entry.path)}`);
|
||||
lines.push(`system,${escapeCsvField(entry.path)},${entry.enabled}`);
|
||||
}
|
||||
for (const entry of data.user) {
|
||||
lines.push(`user,${escapeCsvField(entry.path)}`);
|
||||
lines.push(`user,${escapeCsvField(entry.path)},${entry.enabled}`);
|
||||
}
|
||||
|
||||
return lines.join('\n') + '\n';
|
||||
@@ -92,10 +94,13 @@ export function importFromCsv(content: string): ImportResult {
|
||||
|
||||
if (path.length === 0) continue;
|
||||
|
||||
// 第三列 enabled(可选,默认 true)
|
||||
const enabled = fields.length >= 3 ? fields[2].trim().toLowerCase() !== 'false' : true;
|
||||
|
||||
if (type === 'system') {
|
||||
result.system.push({ path, enabled: true });
|
||||
result.system.push({ path, enabled });
|
||||
} else if (type === 'user') {
|
||||
result.user.push({ path, enabled: true });
|
||||
result.user.push({ path, enabled });
|
||||
}
|
||||
// 未知类型忽略
|
||||
}
|
||||
@@ -153,20 +158,31 @@ export function importFromJson(content: string): ImportResult {
|
||||
try {
|
||||
obj = JSON.parse(content);
|
||||
} catch {
|
||||
return result; // 无效 JSON 返回空结果,由调用方显示错误
|
||||
return result;
|
||||
}
|
||||
|
||||
if (typeof obj !== 'object' || obj === null) return result;
|
||||
|
||||
const parseEntry = (item: unknown): { path: string; enabled: boolean } | null => {
|
||||
if (typeof item === 'string') {
|
||||
const trimmed = item.trim();
|
||||
return trimmed.length > 0 ? { path: trimmed, enabled: true } : null;
|
||||
}
|
||||
if (typeof item === 'object' && item !== null) {
|
||||
const rec = item as Record<string, unknown>;
|
||||
const path = typeof rec.path === 'string' ? rec.path.trim() : '';
|
||||
if (path.length === 0) return null;
|
||||
const enabled = typeof rec.enabled === 'boolean' ? rec.enabled : true;
|
||||
return { path, enabled };
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
if (Array.isArray(obj.system)) {
|
||||
result.system = obj.system
|
||||
.filter((p: unknown) => typeof p === 'string' && p.trim().length > 0)
|
||||
.map((p: string) => ({ path: p.trim(), enabled: true }));
|
||||
result.system = obj.system.map(parseEntry).filter((e): e is { path: string; enabled: boolean } => e !== null);
|
||||
}
|
||||
if (Array.isArray(obj.user)) {
|
||||
result.user = obj.user
|
||||
.filter((p: unknown) => typeof p === 'string' && p.trim().length > 0)
|
||||
.map((p: string) => ({ path: p.trim(), enabled: true }));
|
||||
result.user = obj.user.map(parseEntry).filter((e): e is { path: string; enabled: boolean } => e !== null);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -203,9 +219,10 @@ export function importFromContent(
|
||||
return importFromCsv(content);
|
||||
} else if (lower.endsWith('.json')) {
|
||||
return importFromJson(content);
|
||||
} else {
|
||||
// TXT 文件:所有路径放入 system(用户后续可选择目标)
|
||||
} else if (lower.endsWith('.txt')) {
|
||||
return { system: importFromTxt(content), user: [] };
|
||||
} else {
|
||||
throw new Error(`不支持的导入格式: ${filepath}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user