370 lines
10 KiB
TypeScript
370 lines
10 KiB
TypeScript
import * as vscode from "vscode";
|
|
import * as fs from "fs";
|
|
import * as path from "path";
|
|
|
|
/**
|
|
* 创建文件(本来不存在的情况)
|
|
*/
|
|
export async function createFile(
|
|
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 dirPath = path.dirname(absolutePath);
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, {
|
|
recursive: true,
|
|
});
|
|
}
|
|
|
|
// 创建文件
|
|
fs.writeFileSync(absolutePath, content, "utf-8");
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建文件(如果存在则覆盖)
|
|
*/
|
|
export async function createOrOverwriteFile(
|
|
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("没有打开的工作区,无法创建相对路径的文件");
|
|
}
|
|
}
|
|
|
|
// 确保目录存在
|
|
const dirPath = path.dirname(absolutePath);
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, {
|
|
recursive: true,
|
|
});
|
|
}
|
|
|
|
// 创建或覆盖文件
|
|
fs.writeFileSync(absolutePath, content, "utf-8");
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建目录
|
|
*/
|
|
export async function createDirectory(dirPath: string): Promise<void> {
|
|
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);
|
|
} else {
|
|
throw new Error("没有打开的工作区,无法创建相对路径的目录");
|
|
}
|
|
}
|
|
|
|
// 检测创建目录是否存在
|
|
if (fs.existsSync(absolutePath)) {
|
|
const state = fs.statSync(absolutePath);
|
|
if (state.isDirectory()) {
|
|
throw new Error(`目录已存在: ${absolutePath}`);
|
|
} else {
|
|
throw new Error(`路径已存在且不是目录: ${absolutePath}`);
|
|
}
|
|
}
|
|
|
|
// 创建目录
|
|
fs.mkdirSync(absolutePath, { recursive: true });
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量创建文件
|
|
*/
|
|
export async function createMultipleFiles(
|
|
files: { path: string; content: string }[]
|
|
): Promise<{ path: string; success: boolean; error?: string }[]> {
|
|
const results = [];
|
|
|
|
for (const file of files) {
|
|
try {
|
|
await createFile(file.path, file.content);
|
|
results.push({ path: file.path, success: true });
|
|
} catch (error) {
|
|
results.push({
|
|
path: file.path,
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "未知错误",
|
|
});
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
// 删除文件
|
|
export async function deleteFile(filePath: 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 stats = fs.statSync(absolutePath);
|
|
if (!stats.isFile()) {
|
|
throw new Error(`路径不是文件: ${absolutePath}`);
|
|
}
|
|
|
|
// 删除文件
|
|
fs.unlinkSync(absolutePath);
|
|
} catch (error) {
|
|
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;
|
|
}
|
|
}
|