新增空白文件夹处理

This commit is contained in:
pqcqaq 2025-01-16 19:56:21 +08:00
parent e16d231e3a
commit f7cff5e32c
3 changed files with 99 additions and 1 deletions

View File

@ -10,6 +10,11 @@ const adm_zip_1 = __importDefault(require("adm-zip"));
const DISALLOWED_EXTENSIONS = new Set([ const DISALLOWED_EXTENSIONS = new Set([
".exe", ".exe",
".o", ".o",
".obj",
".ilk",
".pdb",
".idb",
".recipe",
".class", ".class",
".bin", ".bin",
".dll", ".dll",
@ -22,16 +27,27 @@ const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
const DISALLOWED_DIRECTORIES = new Set([ const DISALLOWED_DIRECTORIES = new Set([
".vs", ".vs",
".idea", ".idea",
".tlog",
"__pycache__", "__pycache__",
"node_modules", "node_modules",
"pthreads.2",
"pthreadVC2",
".git", ".git",
"x64", "x64",
"x86", "x86",
"Debug",
"Release",
"build",
"dist",
"bin",
"obj",
"output",
"__MACOSX", "__MACOSX",
".vscode", ".vscode",
]); ]);
let deletedFiles = []; // 存储删除的文件信息 let deletedFiles = []; // 存储删除的文件信息
let deletedDirectories = []; // 存储删除的文件夹信息 let deletedDirectories = []; // 存储删除的文件夹信息
let deletedEmptyDirectories = []; // 存储删除的空文件夹
// 清理文件名 // 清理文件名
const sanitizeFileName = (fileName) => { const sanitizeFileName = (fileName) => {
const decodedName = fileName.toString("utf8"); const decodedName = fileName.toString("utf8");
@ -163,8 +179,29 @@ const processDirectory = (inputDirectory, outputDirectory) => {
console.log(`[KEEP] 保留文件: ${itemPath} 并复制到 ${outputFilePath}`); console.log(`[KEEP] 保留文件: ${itemPath} 并复制到 ${outputFilePath}`);
} }
} }
// 删除空目录
deleteEmptyDirectories(inputDirectory);
}); });
}; };
// 删除空目录
const deleteEmptyDirectories = (dir) => {
// 如果文件夹不存在直接跳过
if (!fs_1.default.existsSync(dir)) {
return;
}
const files = fs_1.default.readdirSync(dir);
if (files.length === 0) {
fs_1.default.rmSync(dir, { recursive: true, force: true });
console.log(`[DELETE] 删除空目录: ${dir}`);
deletedEmptyDirectories.push({
type: "文件夹",
name: path_1.default.basename(dir),
originalPath: dir,
});
const parentDir = path_1.default.dirname(dir);
deleteEmptyDirectories(parentDir); // 递归检查父目录
}
};
// 输出删除信息到控制台 // 输出删除信息到控制台
const printDeletedItems = () => { const printDeletedItems = () => {
let result = "\n[INFO] 删除的文件和文件夹:"; let result = "\n[INFO] 删除的文件和文件夹:";
@ -182,12 +219,21 @@ const printDeletedItems = () => {
result += `\n${item.type} ${item.name} ${item.originalPath}`; result += `\n${item.type} ${item.name} ${item.originalPath}`;
}); });
} }
// 输出空文件夹
if (deletedEmptyDirectories.length > 0) {
result += "\n[INFO] 被删除的空文件夹:";
deletedEmptyDirectories.forEach((item) => {
result += `\n${item.type} ${item.name} ${item.originalPath}`;
});
}
return result; return result;
}; };
// 写入日志到文件 // 写入日志到文件
const writeLogToFile = (log) => { const writeLogToFile = (log) => {
const logFilePath = path_1.default.join(process.cwd(), "out/result.log"); const logFilePath = path_1.default.join(process.cwd(), "out/result.log");
fs_1.default.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" }); fs_1.default.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
// 一共删除了多少
console.log(`[INFO] 共删除了 ${deletedFiles.length} 个文件,${deletedDirectories.length} 个文件夹,${deletedEmptyDirectories.length} 个空文件夹。`);
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`); console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
}; };
// 主函数 // 主函数

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,11 @@ import AdmZip from "adm-zip";
const DISALLOWED_EXTENSIONS = new Set([ const DISALLOWED_EXTENSIONS = new Set([
".exe", ".exe",
".o", ".o",
".obj",
".ilk",
".pdb",
".idb",
".recipe",
".class", ".class",
".bin", ".bin",
".dll", ".dll",
@ -18,11 +23,21 @@ const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
const DISALLOWED_DIRECTORIES = new Set([ const DISALLOWED_DIRECTORIES = new Set([
".vs", ".vs",
".idea", ".idea",
".tlog",
"__pycache__", "__pycache__",
"node_modules", "node_modules",
"pthreads.2",
"pthreadVC2",
".git", ".git",
"x64", "x64",
"x86", "x86",
"Debug",
"Release",
"build",
"dist",
"bin",
"obj",
"output",
"__MACOSX", "__MACOSX",
".vscode", ".vscode",
]); ]);
@ -37,6 +52,11 @@ let deletedDirectories: {
name: string; name: string;
originalPath: string; originalPath: string;
}[] = []; // 存储删除的文件夹信息 }[] = []; // 存储删除的文件夹信息
let deletedEmptyDirectories: {
type: string;
name: string;
originalPath: string;
}[] = []; // 存储删除的空文件夹
// 清理文件名 // 清理文件名
const sanitizeFileName = (fileName: Buffer): string => { const sanitizeFileName = (fileName: Buffer): string => {
@ -192,9 +212,32 @@ const processDirectory = (
); );
} }
} }
// 删除空目录
deleteEmptyDirectories(inputDirectory);
}); });
}; };
// 删除空目录
const deleteEmptyDirectories = (dir: string) => {
// 如果文件夹不存在直接跳过
if (!fs.existsSync(dir)) {
return;
}
const files = fs.readdirSync(dir);
if (files.length === 0) {
fs.rmSync(dir, { recursive: true, force: true });
console.log(`[DELETE] 删除空目录: ${dir}`);
deletedEmptyDirectories.push({
type: "文件夹",
name: path.basename(dir),
originalPath: dir,
});
const parentDir = path.dirname(dir);
deleteEmptyDirectories(parentDir); // 递归检查父目录
}
};
// 输出删除信息到控制台 // 输出删除信息到控制台
const printDeletedItems = () => { const printDeletedItems = () => {
let result = "\n[INFO] 删除的文件和文件夹:"; let result = "\n[INFO] 删除的文件和文件夹:";
@ -212,6 +255,13 @@ const printDeletedItems = () => {
result += `\n${item.type} ${item.name} ${item.originalPath}`; result += `\n${item.type} ${item.name} ${item.originalPath}`;
}); });
} }
// 输出空文件夹
if (deletedEmptyDirectories.length > 0) {
result += "\n[INFO] 被删除的空文件夹:";
deletedEmptyDirectories.forEach((item) => {
result += `\n${item.type} ${item.name} ${item.originalPath}`;
});
}
return result; return result;
}; };
@ -219,6 +269,8 @@ const printDeletedItems = () => {
const writeLogToFile = (log: string) => { const writeLogToFile = (log: string) => {
const logFilePath = path.join(process.cwd(), "out/result.log"); const logFilePath = path.join(process.cwd(), "out/result.log");
fs.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" }); fs.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
// 一共删除了多少
console.log(`[INFO] 共删除了 ${deletedFiles.length } 个文件,${deletedDirectories.length} 个文件夹,${deletedEmptyDirectories.length} 个空文件夹。`);
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`); console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
}; };