feat:实现对文件的创建和删除
- 还涵盖了对已存在的文件进行覆盖 - 对不存在的文件创建 - 还可以创建目录 - 可以一次创建多个文件
This commit is contained in:
196
src/utils/createFiles.ts
Normal file
196
src/utils/createFiles.ts
Normal file
@ -0,0 +1,196 @@
|
||||
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 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 {
|
||||
// 如果是相对路径,转换为绝对路径
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user