feat:实现读取本地文件的功能

This commit is contained in:
Roe-xin
2025-12-11 14:29:56 +08:00
parent 565f4afe46
commit 49b3e34101
5 changed files with 315 additions and 7 deletions

164
src/utils/readFiles.ts Normal file
View File

@ -0,0 +1,164 @@
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
/**
* 读取文件内容
*/
export async function readFileContent(filePath: string): Promise<string> {
try {
// 如果是相对路径,转换为绝对路径
let absolutePath = filePath;
if (!path.isAbsolute(filePath)) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
}
}
// 检查文件是否存在
if (!fs.existsSync(absolutePath)) {
throw new Error(`文件不存在: ${absolutePath}`);
}
// 检查是否是文件
const stats = fs.statSync(absolutePath);
if (!stats.isFile()) {
throw new Error(`路径不是文件: ${absolutePath}`);
}
// 读取文件内容
const content = fs.readFileSync(absolutePath, "utf-8");
return content;
} catch (error) {
throw error;
}
}
/**
* 读取多个文件内容
*/
export async function readMultipleFiles(
filePaths: string[]
): Promise<{ path: string; content: string; error?: string }[]> {
const results = [];
for (const filePath of filePaths) {
try {
const content = await readFileContent(filePath);
results.push({ path: filePath, content });
} catch (error) {
results.push({
path: filePath,
content: "",
error: error instanceof Error ? error.message : "未知错误",
});
}
}
return results;
}
/**
* 读取目录下所有文件
*/
export async function readDirectory(
dirPath: string,
extensions?: string[]
): Promise<{ path: string; content: string }[]> {
try {
// 如果是相对路径,转换为绝对路径
let absolutePath = dirPath;
if (!path.isAbsolute(dirPath)) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
}
}
// 检查目录是否存在
if (!fs.existsSync(absolutePath)) {
throw new Error(`目录不存在: ${absolutePath}`);
}
const stats = fs.statSync(absolutePath);
if (!stats.isDirectory()) {
throw new Error(`路径不是目录: ${absolutePath}`);
}
// 读取目录内容
const files = fs.readdirSync(absolutePath);
const results = [];
for (const file of files) {
const filePath = path.join(absolutePath, file);
const fileStats = fs.statSync(filePath);
if (fileStats.isFile()) {
// 如果指定了扩展名过滤
if (extensions && extensions.length > 0) {
const ext = path.extname(file);
if (!extensions.includes(ext)) {
continue;
}
}
try {
const content = fs.readFileSync(filePath, "utf-8");
results.push({ path: file, content });
} catch (error) {
// 跳过无法读取的文件
continue;
}
}
}
return results;
} catch (error) {
throw error;
}
}
/**
* 获取文件信息
*/
export function getFileInfo(filePath: string): {
exists: boolean;
isFile: boolean;
isDirectory: boolean;
size?: number;
extension?: string;
} {
try {
let absolutePath = filePath;
if (!path.isAbsolute(filePath)) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
}
}
if (!fs.existsSync(absolutePath)) {
return {
exists: false,
isFile: false,
isDirectory: false,
};
}
const stats = fs.statSync(absolutePath);
return {
exists: true,
isFile: stats.isFile(),
isDirectory: stats.isDirectory(),
size: stats.size,
extension: path.extname(absolutePath),
};
} catch (error) {
return {
exists: false,
isFile: false,
isDirectory: false,
};
}
}