新增空白文件夹处理
This commit is contained in:
parent
e16d231e3a
commit
f7cff5e32c
46
lib/index.js
46
lib/index.js
|
|
@ -10,6 +10,11 @@ const adm_zip_1 = __importDefault(require("adm-zip"));
|
|||
const DISALLOWED_EXTENSIONS = new Set([
|
||||
".exe",
|
||||
".o",
|
||||
".obj",
|
||||
".ilk",
|
||||
".pdb",
|
||||
".idb",
|
||||
".recipe",
|
||||
".class",
|
||||
".bin",
|
||||
".dll",
|
||||
|
|
@ -22,16 +27,27 @@ const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
|
|||
const DISALLOWED_DIRECTORIES = new Set([
|
||||
".vs",
|
||||
".idea",
|
||||
".tlog",
|
||||
"__pycache__",
|
||||
"node_modules",
|
||||
"pthreads.2",
|
||||
"pthreadVC2",
|
||||
".git",
|
||||
"x64",
|
||||
"x86",
|
||||
"Debug",
|
||||
"Release",
|
||||
"build",
|
||||
"dist",
|
||||
"bin",
|
||||
"obj",
|
||||
"output",
|
||||
"__MACOSX",
|
||||
".vscode",
|
||||
]);
|
||||
let deletedFiles = []; // 存储删除的文件信息
|
||||
let deletedDirectories = []; // 存储删除的文件夹信息
|
||||
let deletedEmptyDirectories = []; // 存储删除的空文件夹
|
||||
// 清理文件名
|
||||
const sanitizeFileName = (fileName) => {
|
||||
const decodedName = fileName.toString("utf8");
|
||||
|
|
@ -163,8 +179,29 @@ const processDirectory = (inputDirectory, outputDirectory) => {
|
|||
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 = () => {
|
||||
let result = "\n[INFO] 删除的文件和文件夹:";
|
||||
|
|
@ -182,12 +219,21 @@ const printDeletedItems = () => {
|
|||
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;
|
||||
};
|
||||
// 写入日志到文件
|
||||
const writeLogToFile = (log) => {
|
||||
const logFilePath = path_1.default.join(process.cwd(), "out/result.log");
|
||||
fs_1.default.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
|
||||
// 一共删除了多少
|
||||
console.log(`[INFO] 共删除了 ${deletedFiles.length} 个文件,${deletedDirectories.length} 个文件夹,${deletedEmptyDirectories.length} 个空文件夹。`);
|
||||
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
|
||||
};
|
||||
// 主函数
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
52
src/index.ts
52
src/index.ts
|
|
@ -6,6 +6,11 @@ import AdmZip from "adm-zip";
|
|||
const DISALLOWED_EXTENSIONS = new Set([
|
||||
".exe",
|
||||
".o",
|
||||
".obj",
|
||||
".ilk",
|
||||
".pdb",
|
||||
".idb",
|
||||
".recipe",
|
||||
".class",
|
||||
".bin",
|
||||
".dll",
|
||||
|
|
@ -18,11 +23,21 @@ const DISALLOWED_KEYWORDS = new Set(["a.out", "main", "temp"]);
|
|||
const DISALLOWED_DIRECTORIES = new Set([
|
||||
".vs",
|
||||
".idea",
|
||||
".tlog",
|
||||
"__pycache__",
|
||||
"node_modules",
|
||||
"pthreads.2",
|
||||
"pthreadVC2",
|
||||
".git",
|
||||
"x64",
|
||||
"x86",
|
||||
"Debug",
|
||||
"Release",
|
||||
"build",
|
||||
"dist",
|
||||
"bin",
|
||||
"obj",
|
||||
"output",
|
||||
"__MACOSX",
|
||||
".vscode",
|
||||
]);
|
||||
|
|
@ -37,6 +52,11 @@ let deletedDirectories: {
|
|||
name: string;
|
||||
originalPath: string;
|
||||
}[] = []; // 存储删除的文件夹信息
|
||||
let deletedEmptyDirectories: {
|
||||
type: string;
|
||||
name: string;
|
||||
originalPath: 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 = () => {
|
||||
let result = "\n[INFO] 删除的文件和文件夹:";
|
||||
|
|
@ -212,6 +255,13 @@ const printDeletedItems = () => {
|
|||
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;
|
||||
};
|
||||
|
||||
|
|
@ -219,6 +269,8 @@ const printDeletedItems = () => {
|
|||
const writeLogToFile = (log: string) => {
|
||||
const logFilePath = path.join(process.cwd(), "out/result.log");
|
||||
fs.writeFileSync(logFilePath, log, { encoding: "utf8", flag: "w" });
|
||||
// 一共删除了多少
|
||||
console.log(`[INFO] 共删除了 ${deletedFiles.length } 个文件,${deletedDirectories.length} 个文件夹,${deletedEmptyDirectories.length} 个空文件夹。`);
|
||||
console.log(`[INFO] 统计信息已保存到 result.log 文件中。`);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue