File size: 1,871 Bytes
f6ba329 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
const fs = require('fs');
const path = require('path');
// Path to the avatar photos directory
const avatarDir = path.join(__dirname, 'images', 'avatar-photos');
// Categories based on filename patterns
const categoryPatterns = [
{ pattern: /android|bot|robot/, category: 'robots' },
{ pattern: /sloth/, category: 'sloths' },
{ pattern: /cat|dog|fox|monkey|penguin|koala|animal/, category: 'animals' },
{ pattern: /icon|cloud|api|vision|words/, category: 'abstract' }
];
// Function to determine category based on filename
function determineCategory(filename) {
for (const { pattern, category } of categoryPatterns) {
if (pattern.test(filename.toLowerCase())) {
return category;
}
}
return 'other'; // Default category
}
// Function to generate a display name from filename
function generateDisplayName(filename) {
// Remove extension and replace hyphens with spaces
const name = filename.replace(/\.webp$/, '').replace(/-/g, ' ');
// Capitalize each word
return name.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
// Read all files in the avatar directory
fs.readdir(avatarDir, (err, files) => {
if (err) {
console.error('Error reading avatar directory:', err);
return;
}
// Filter for webp files only
const avatarFiles = files.filter(file => file.endsWith('.webp'));
// Create avatar data array
const avatarData = avatarFiles.map(file => ({
file,
category: determineCategory(file),
name: generateDisplayName(file)
}));
// Write to JSON file
fs.writeFileSync(
path.join(__dirname, 'avatar-list.json'),
JSON.stringify(avatarData, null, 2)
);
console.log(`Generated avatar list with ${avatarData.length} images`);
}); |