feat:对本地文件进行修改
- 对某一行进行修改 - 将文件中的某些词进行替换 - 将文件重命名
This commit is contained in:
@ -132,37 +132,6 @@ export async function createMultipleFiles(
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加文件内容
|
||||
*/
|
||||
export async function appendToFile(
|
||||
filePath: string,
|
||||
content: string
|
||||
): Promise<void> {
|
||||
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);
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法追加相对路径的文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 追加内容
|
||||
fs.appendFileSync(absolutePath, content, "utf-8");
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
export async function deleteFile(filePath: string): Promise<void> {
|
||||
try {
|
||||
@ -194,3 +163,207 @@ export async function deleteFile(filePath: string): Promise<void> {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 修改文件内容(完全替换)
|
||||
export async function updateFile(
|
||||
filePath: string,
|
||||
content: string
|
||||
): Promise<void> {
|
||||
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);
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 检查是否是文件内容
|
||||
const state = fs.statSync(absolutePath);
|
||||
if (!state.isFile()) {
|
||||
throw new Error(`路径不是文件: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 修改文件内容
|
||||
fs.writeFileSync(absolutePath, content, "utf-8");
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加文件内容
|
||||
*/
|
||||
export async function appendToFile(
|
||||
filePath: string,
|
||||
content: string
|
||||
): Promise<void> {
|
||||
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);
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法追加相对路径的文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 追加内容
|
||||
fs.appendFileSync(absolutePath, content, "utf-8");
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 部分修改文件内容(查找替换)
|
||||
export async function replaceFile(
|
||||
filePath: string,
|
||||
searchValue: string | RegExp,
|
||||
replaceValue: string
|
||||
): Promise<void> {
|
||||
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);
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
const fileContent = fs.readFileSync(absolutePath, "utf-8");
|
||||
|
||||
// 转义特殊字符,将字符串作为字面量处理
|
||||
const escapeRegExp = (str: string) => {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
};
|
||||
|
||||
// 替换内容 - 如果是字符串,先转义特殊字符
|
||||
let newContent: string;
|
||||
if (typeof searchValue === 'string') {
|
||||
const escapedSearch = escapeRegExp(searchValue);
|
||||
newContent = fileContent.replace(new RegExp(escapedSearch, "g"), replaceValue);
|
||||
} else {
|
||||
newContent = fileContent.replace(searchValue, replaceValue);
|
||||
}
|
||||
|
||||
// 检查是否有内容被替换
|
||||
if (fileContent === newContent) {
|
||||
throw new Error(`未找到要替换的内容: ${searchValue}`);
|
||||
}
|
||||
|
||||
// 写回文件
|
||||
fs.writeFileSync(absolutePath, newContent, "utf-8");
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 在指定位置插入内容
|
||||
export async function insertAtLine(
|
||||
filePath: string,
|
||||
lineNumber: number,
|
||||
content: string
|
||||
): Promise<void> {
|
||||
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);
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
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");
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名文件
|
||||
*/
|
||||
export async function renameFile(
|
||||
oldPath: string,
|
||||
newPath: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
// 如果是相对路径,转换为绝对路径
|
||||
let absoluteOldPath = oldPath;
|
||||
let absoluteNewPath = newPath;
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||
const workspaceRoot = workspaceFolders[0].uri.fsPath;
|
||||
|
||||
if (!path.isAbsolute(oldPath)) {
|
||||
absoluteOldPath = path.join(workspaceRoot, oldPath);
|
||||
}
|
||||
|
||||
if (!path.isAbsolute(newPath)) {
|
||||
absoluteNewPath = path.join(workspaceRoot, newPath);
|
||||
}
|
||||
} else {
|
||||
throw new Error("没有打开的工作区,无法重命名相对路径的文件");
|
||||
}
|
||||
|
||||
// 检查原文件是否存在
|
||||
if (!fs.existsSync(absoluteOldPath)) {
|
||||
throw new Error(`文件不存在: ${absoluteOldPath}`);
|
||||
}
|
||||
|
||||
// 检查新文件名是否已存在
|
||||
if (fs.existsSync(absoluteNewPath)) {
|
||||
throw new Error(`目标文件已存在: ${absoluteNewPath}`);
|
||||
}
|
||||
|
||||
// 确保目标目录存在
|
||||
const newDir = path.dirname(absoluteNewPath);
|
||||
if (!fs.existsSync(newDir)) {
|
||||
fs.mkdirSync(newDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 重命名文件
|
||||
fs.renameSync(absoluteOldPath, absoluteNewPath);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user