Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #!/usr/bin/env tsx
/**
* Generate missing metadata files for SVG icons
* Creates basic metadata JSON files with empty synonyms for any SVG that lacks metadata
*/
import { readdir, writeFile, access } from 'fs/promises';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, '..');
const iconTypes = ['action', 'custom', 'doctype', 'standard', 'utility'] as const;
interface Stats {
category: string;
created: number;
skipped: number;
total: number;
}
async function fileExists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
async function collectSvgBasenames(category: string): Promise<Set<string>> {
const basenames = new Set<string>();
const svgDir = join(rootDir, 'svg', category);
const subdirs = ['common', 'ltr', 'rtl'];
for (const subdir of subdirs) {
const dir = join(svgDir, subdir);
try {
const files = await readdir(dir);
files.filter((f) => f.endsWith('.svg')).forEach((file) => basenames.add(file.replace('.svg', '')));
} catch {
// Directory doesn't exist, skip
}
}
return basenames;
}
async function getExistingMetadata(category: string): Promise<Set<string>> {
const metadataDir = join(rootDir, 'model', 'metadata', category);
try {
const files = await readdir(metadataDir);
return new Set(files.filter((f) => f.endsWith('.json')).map((f) => f.replace('.json', '')));
} catch {
return new Set();
}
}
async function generateMetadataForCategory(category: string): Promise<Stats> {
const svgBasenames = await collectSvgBasenames(category);
const existingMetadata = await getExistingMetadata(category);
const stats: Stats = {
category,
created: 0,
skipped: 0,
total: svgBasenames.size,
};
const metadataDir = join(rootDir, 'model', 'metadata', category);
for (const basename of svgBasenames) {
if (existingMetadata.has(basename)) {
stats.skipped++;
continue;
}
// Generate default metadata with empty synonyms
const metadata = {
synonyms: [],
};
const metadataPath = join(metadataDir, `${basename}.json`);
await writeFile(metadataPath, JSON.stringify(metadata, null, 2) + '\n', 'utf-8');
stats.created++;
console.log(`ā
Created: ${category}/${basename}.json`);
}
return stats;
}
async function main() {
console.log('\nš¦ Generating missing metadata files\n');
const allStats: Stats[] = [];
for (const category of iconTypes) {
console.log(`\nš Processing ${category} icons...`);
const stats = await generateMetadataForCategory(category);
allStats.push(stats);
}
// Print summary
console.log('\n' + '='.repeat(60));
console.log('š Summary\n');
let totalCreated = 0;
let totalSkipped = 0;
let totalIcons = 0;
for (const stats of allStats) {
console.log(
`${stats.category.padEnd(10)} - Created: ${stats.created}, Skipped: ${stats.skipped}, Total: ${stats.total}`,
);
totalCreated += stats.created;
totalSkipped += stats.skipped;
totalIcons += stats.total;
}
console.log('\n' + '='.repeat(60));
console.log(`Total - Created: ${totalCreated}, Skipped: ${totalSkipped}, Total: ${totalIcons}`);
console.log('\n⨠Done!\n');
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
|