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";
|
||||
|
||||
/**
|
||||
@ -21,21 +20,31 @@ export async function createFile(
|
||||
}
|
||||
}
|
||||
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检测文件是否已存在
|
||||
if (fs.existsSync(absolutePath)) {
|
||||
try {
|
||||
await vscode.workspace.fs.stat(fileUri);
|
||||
throw new Error(`文件已存在: ${absolutePath}`);
|
||||
} catch (error: any) {
|
||||
// 如果文件不存在,继续创建
|
||||
if (error.code !== 'FileNotFound') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 确保目录存在
|
||||
const dirPath = path.dirname(absolutePath);
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, {
|
||||
recursive: true,
|
||||
});
|
||||
const dirUri = vscode.Uri.file(dirPath);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(dirUri);
|
||||
} catch {
|
||||
await vscode.workspace.fs.createDirectory(dirUri);
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
fs.writeFileSync(absolutePath, content, "utf-8");
|
||||
const contentBytes = Buffer.from(content, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, contentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -62,14 +71,17 @@ export async function createOrOverwriteFile(
|
||||
|
||||
// 确保目录存在
|
||||
const dirPath = path.dirname(absolutePath);
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, {
|
||||
recursive: true,
|
||||
});
|
||||
const dirUri = vscode.Uri.file(dirPath);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(dirUri);
|
||||
} catch {
|
||||
await vscode.workspace.fs.createDirectory(dirUri);
|
||||
}
|
||||
|
||||
// 创建或覆盖文件
|
||||
fs.writeFileSync(absolutePath, content, "utf-8");
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
const contentBytes = Buffer.from(content, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, contentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -91,18 +103,25 @@ export async function createDirectory(dirPath: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
const dirUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检测创建目录是否存在
|
||||
if (fs.existsSync(absolutePath)) {
|
||||
const state = fs.statSync(absolutePath);
|
||||
if (state.isDirectory()) {
|
||||
try {
|
||||
const stat = await vscode.workspace.fs.stat(dirUri);
|
||||
if (stat.type === vscode.FileType.Directory) {
|
||||
throw new Error(`目录已存在: ${absolutePath}`);
|
||||
} else {
|
||||
throw new Error(`路径已存在且不是目录: ${absolutePath}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
// 如果目录不存在,继续创建
|
||||
if (error.code !== 'FileNotFound') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建目录
|
||||
fs.mkdirSync(absolutePath, { recursive: true });
|
||||
await vscode.workspace.fs.createDirectory(dirUri);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -146,19 +165,20 @@ export async function deleteFile(filePath: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(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}`);
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
fs.unlinkSync(absolutePath);
|
||||
await vscode.workspace.fs.delete(fileUri);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -181,19 +201,21 @@ export async function updateFile(
|
||||
}
|
||||
}
|
||||
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(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 state = fs.statSync(absolutePath);
|
||||
if (!state.isFile()) {
|
||||
throw new Error(`路径不是文件: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 修改文件内容
|
||||
fs.writeFileSync(absolutePath, content, "utf-8");
|
||||
const contentBytes = Buffer.from(content, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, contentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -218,13 +240,21 @@ export async function appendToFile(
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 检查文件是否存在并读取原内容
|
||||
let existingContent = "";
|
||||
try {
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
existingContent = Buffer.from(contentBytes).toString("utf-8");
|
||||
} catch (error) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 追加内容
|
||||
fs.appendFileSync(absolutePath, content, "utf-8");
|
||||
const newContent = existingContent + content;
|
||||
const contentBytes = Buffer.from(newContent, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, contentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -248,13 +278,16 @@ export async function replaceFile(
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 读取文件内容
|
||||
const fileContent = fs.readFileSync(absolutePath, "utf-8");
|
||||
let fileContent: string;
|
||||
try {
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
fileContent = Buffer.from(contentBytes).toString("utf-8");
|
||||
} catch (error) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 转义特殊字符,将字符串作为字面量处理
|
||||
const escapeRegExp = (str: string) => {
|
||||
@ -276,7 +309,8 @@ export async function replaceFile(
|
||||
}
|
||||
|
||||
// 写回文件
|
||||
fs.writeFileSync(absolutePath, newContent, "utf-8");
|
||||
const contentBytes = Buffer.from(newContent, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, contentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -300,20 +334,26 @@ export async function insertAtLine(
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
const fileUri = vscode.Uri.file(absolutePath);
|
||||
|
||||
// 读取文件内容
|
||||
let fileContent: string;
|
||||
try {
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
fileContent = Buffer.from(contentBytes).toString("utf-8");
|
||||
} catch (error) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
const fileContent = fs.readFileSync(absolutePath, "utf-8");
|
||||
const lines = fileContent.split("\n");
|
||||
|
||||
// 插入内容
|
||||
lines.splice(lineNumber, 0, content);
|
||||
|
||||
// 写回文件
|
||||
fs.writeFileSync(absolutePath, lines.join("\n"), "utf-8");
|
||||
const newContent = lines.join("\n");
|
||||
const newContentBytes = Buffer.from(newContent, "utf-8");
|
||||
await vscode.workspace.fs.writeFile(fileUri, newContentBytes);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@ -345,24 +385,41 @@ export async function renameFile(
|
||||
throw new Error("没有打开的工作区,无法重命名相对路径的文件");
|
||||
}
|
||||
|
||||
const oldUri = vscode.Uri.file(absoluteOldPath);
|
||||
const newUri = vscode.Uri.file(absoluteNewPath);
|
||||
|
||||
// 检查原文件是否存在
|
||||
if (!fs.existsSync(absoluteOldPath)) {
|
||||
try {
|
||||
await vscode.workspace.fs.stat(oldUri);
|
||||
} catch (error) {
|
||||
throw new Error(`文件不存在: ${absoluteOldPath}`);
|
||||
}
|
||||
|
||||
// 检查新文件名是否已存在
|
||||
if (fs.existsSync(absoluteNewPath)) {
|
||||
try {
|
||||
await vscode.workspace.fs.stat(newUri);
|
||||
throw new Error(`目标文件已存在: ${absoluteNewPath}`);
|
||||
} catch (error: any) {
|
||||
// 如果文件不存在,继续重命名
|
||||
if (error.code !== 'FileNotFound' && !error.message.includes('目标文件已存在')) {
|
||||
throw error;
|
||||
}
|
||||
if (error.message.includes('目标文件已存在')) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 确保目标目录存在
|
||||
const newDir = path.dirname(absoluteNewPath);
|
||||
if (!fs.existsSync(newDir)) {
|
||||
fs.mkdirSync(newDir, { recursive: true });
|
||||
const newDirUri = vscode.Uri.file(newDir);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(newDirUri);
|
||||
} catch {
|
||||
await vscode.workspace.fs.createDirectory(newDirUri);
|
||||
}
|
||||
|
||||
// 重命名文件
|
||||
fs.renameSync(absoluteOldPath, absoluteNewPath);
|
||||
await vscode.workspace.fs.rename(oldUri, newUri, { overwrite: false });
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user