115 lines
2.9 KiB
JavaScript
Executable File
115 lines
2.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
|
||
/**
|
||
* 自动扫描风景照片脚本
|
||
*/
|
||
|
||
import fs from 'fs';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = path.dirname(__filename);
|
||
|
||
const MEDIA_DIR = path.join(__dirname, '../public/media');
|
||
const OUTPUT_FILE = path.join(__dirname, '../src/data/scenery-photos.js');
|
||
|
||
// 风景照片的命名规则:5_、6_、7_、8_ 开头
|
||
const SCENERY_PATTERN = /^[5-8]_\d+_\d+\.(jpg|jpeg|png|webp)$/i;
|
||
|
||
// 分类映射
|
||
const CATEGORIES = {
|
||
'5': { name: '纯自然风景', files: [] },
|
||
'6': { name: '非自然风景', files: [] },
|
||
'7': { name: '植物', files: [] },
|
||
'8': { name: '动物', files: [] }
|
||
};
|
||
|
||
function scanPhotos() {
|
||
console.log('🔍 正在扫描风景照片...');
|
||
|
||
const files = fs.readdirSync(MEDIA_DIR);
|
||
const sceneryFiles = files.filter(file => SCENERY_PATTERN.test(file));
|
||
|
||
console.log(`✅ 找到 ${sceneryFiles.length} 张风景照片`);
|
||
|
||
sceneryFiles.forEach(file => {
|
||
const category = file[0];
|
||
if (CATEGORIES[category]) {
|
||
CATEGORIES[category].files.push(file);
|
||
}
|
||
});
|
||
|
||
console.log('\n📊 照片分类统计:');
|
||
Object.entries(CATEGORIES).forEach(([key, { name, files }]) => {
|
||
console.log(` ${key}_ ${name}: ${files.length} 张`);
|
||
});
|
||
|
||
return sceneryFiles.sort();
|
||
}
|
||
|
||
function generateConfigFile() {
|
||
const timestamp = new Date().toISOString();
|
||
|
||
const lines = [];
|
||
Object.entries(CATEGORIES).forEach(([key, { name, files }]) => {
|
||
if (files.length > 0) {
|
||
lines.push(` // ${name} (${key}_)`);
|
||
files.forEach(file => lines.push(` '${file}',`));
|
||
lines.push('');
|
||
}
|
||
});
|
||
|
||
if (lines.length > 0 && lines[lines.length - 1] === '') {
|
||
lines.pop();
|
||
lines[lines.length - 1] = lines[lines.length - 1].replace(/,$/, '');
|
||
}
|
||
|
||
return `/**
|
||
* 风景照片配置文件
|
||
*
|
||
* ⚠️ 此文件由脚本自动生成,请勿手动编辑
|
||
* 生成时间: ${timestamp}
|
||
*
|
||
* 命名规则:
|
||
* - 5_Y_Z.JPG - 纯自然风景
|
||
* - 6_Y_Z.JPG - 非自然风景(建筑、城市等)
|
||
* - 7_Y_Z.JPG - 植物
|
||
* - 8_Y_Z.JPG - 动物
|
||
*/
|
||
|
||
export const sceneryPhotos = [
|
||
${lines.join('\n')}
|
||
];
|
||
`;
|
||
}
|
||
|
||
function main() {
|
||
console.log('🎨 Gallery 风景照片扫描工具\n');
|
||
|
||
if (!fs.existsSync(MEDIA_DIR)) {
|
||
console.error(`❌ 错误: 找不到 media 目录: ${MEDIA_DIR}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
const photos = scanPhotos();
|
||
|
||
if (photos.length === 0) {
|
||
console.log('\n⚠️ 警告: 未找到符合命名规则的风景照片');
|
||
return;
|
||
}
|
||
|
||
const content = generateConfigFile();
|
||
|
||
const dir = path.dirname(OUTPUT_FILE);
|
||
if (!fs.existsSync(dir)) {
|
||
fs.mkdirSync(dir, { recursive: true });
|
||
}
|
||
|
||
fs.writeFileSync(OUTPUT_FILE, content, 'utf8');
|
||
console.log(`\n✅ 配置文件已更新: ${path.relative(process.cwd(), OUTPUT_FILE)}`);
|
||
console.log('\n🎉 完成!\n');
|
||
}
|
||
|
||
main();
|