unused-files-cleaner
Version:
A CLI tool to clean unused files in projects
55 lines (54 loc) • 1.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanEmptyFiles = scanEmptyFiles;
const glob_1 = require("glob");
const promises_1 = require("fs/promises");
const chalk_1 = __importDefault(require("chalk"));
async function scanEmptyFiles(directory, options = {}) {
const { ignore = [], verbose = false } = options;
// 默认忽略的文件和目录
const defaultIgnore = [
'**/node_modules/**',
'**/.git/**',
'**/dist/**',
'**/build/**',
'**/.DS_Store'
];
const allIgnorePatterns = [...defaultIgnore, ...ignore];
if (verbose) {
console.log(chalk_1.default.blue('Scanning directory:', directory));
console.log(chalk_1.default.blue('Ignore patterns:', allIgnorePatterns.join(', ')));
}
// 获取所有文件
const files = await (0, glob_1.glob)('**/*', {
cwd: directory,
ignore: allIgnorePatterns,
nodir: true,
absolute: true
});
if (verbose) {
console.log(chalk_1.default.blue(`Found ${files.length} files to check`));
}
const emptyFiles = [];
// 检查每个文件
for (const file of files) {
try {
const content = await (0, promises_1.readFile)(file, 'utf-8');
if (!content.trim()) {
emptyFiles.push(file);
if (verbose) {
console.log(chalk_1.default.yellow(`Empty file found: ${file}`));
}
}
}
catch (error) {
if (verbose) {
console.error(chalk_1.default.red(`Error reading file ${file}:`, error instanceof Error ? error.message : 'Unknown error'));
}
}
}
return emptyFiles;
}