feat:将调用node.js的fs模块改为vscode官方的API
- 这样可以避免用户本地没有node环境导致插件无法运行的原因
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import * as vscode from "vscode";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
/**
|
||||
@ -16,19 +15,21 @@ export async function readFileContent(filePath: string): Promise<string> {
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检查文件是否存在并获取文件类型
|
||||
try {
|
||||
const stat = await vscode.workspace.fs.stat(fileUri);
|
||||
if (stat.type !== vscode.FileType.File) {
|
||||
throw new Error(`路径不是文件: ${absolutePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 检查是否是文件
|
||||
const stats = fs.statSync(absolutePath);
|
||||
if (!stats.isFile()) {
|
||||
throw new Error(`路径不是文件: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
const content = fs.readFileSync(absolutePath, "utf-8");
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
const content = Buffer.from(contentBytes).toString("utf-8");
|
||||
return content;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@ -76,36 +77,38 @@ export async function readDirectory(
|
||||
}
|
||||
}
|
||||
|
||||
const dirUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检查目录是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
try {
|
||||
const stat = await vscode.workspace.fs.stat(dirUri);
|
||||
if (stat.type !== vscode.FileType.Directory) {
|
||||
throw new Error(`路径不是目录: ${absolutePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`目录不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
const stats = fs.statSync(absolutePath);
|
||||
if (!stats.isDirectory()) {
|
||||
throw new Error(`路径不是目录: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取目录内容
|
||||
const files = fs.readdirSync(absolutePath);
|
||||
const entries = await vscode.workspace.fs.readDirectory(dirUri);
|
||||
const results = [];
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(absolutePath, file);
|
||||
const fileStats = fs.statSync(filePath);
|
||||
|
||||
if (fileStats.isFile()) {
|
||||
for (const [fileName, fileType] of entries) {
|
||||
if (fileType === vscode.FileType.File) {
|
||||
// 如果指定了扩展名过滤
|
||||
if (extensions && extensions.length > 0) {
|
||||
const ext = path.extname(file);
|
||||
const ext = path.extname(fileName);
|
||||
if (!extensions.includes(ext)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
results.push({ path: file, content });
|
||||
const filePath = path.join(absolutePath, fileName);
|
||||
const fileUri = vscode.Uri.file(filePath);
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
const content = Buffer.from(contentBytes).toString("utf-8");
|
||||
results.push({ path: fileName, content });
|
||||
} catch (error) {
|
||||
// 跳过无法读取的文件
|
||||
continue;
|
||||
@ -122,13 +125,13 @@ export async function readDirectory(
|
||||
/**
|
||||
* 获取文件信息
|
||||
*/
|
||||
export function getFileInfo(filePath: string): {
|
||||
export async function getFileInfo(filePath: string): Promise<{
|
||||
exists: boolean;
|
||||
isFile: boolean;
|
||||
isDirectory: boolean;
|
||||
size?: number;
|
||||
extension?: string;
|
||||
} {
|
||||
}> {
|
||||
try {
|
||||
let absolutePath = filePath;
|
||||
if (!path.isAbsolute(filePath)) {
|
||||
@ -138,22 +141,24 @@ export function getFileInfo(filePath: string): {
|
||||
}
|
||||
}
|
||||
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
try {
|
||||
const stat = await vscode.workspace.fs.stat(fileUri);
|
||||
return {
|
||||
exists: true,
|
||||
isFile: stat.type === vscode.FileType.File,
|
||||
isDirectory: stat.type === vscode.FileType.Directory,
|
||||
size: stat.size,
|
||||
extension: path.extname(absolutePath),
|
||||
};
|
||||
} catch (error) {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user