ObsiViewer/scripts/enrich-all-notes.mjs

110 lines
3.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Script to enrich all existing markdown files with standardized front-matter
* Usage: node scripts/enrich-all-notes.mjs [--dry-run]
*/
import { enrichFrontmatterOnOpen } from '../server/ensureFrontmatter.mjs';
import { VAULT_PATH as CFG_VAULT_PATH } from '../server/config.mjs';
import fg from 'fast-glob';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isDryRun = process.argv.includes('--dry-run');
const VAULT_PATH = path.isAbsolute(CFG_VAULT_PATH)
? CFG_VAULT_PATH
: path.resolve(__dirname, '..', CFG_VAULT_PATH);
async function enrichAllNotes() {
console.log('\n🔧 Front-matter Enrichment Script');
console.log('=====================================\n');
if (isDryRun) {
console.log('⚠️ DRY RUN MODE - No files will be modified\n');
}
console.log(`📁 Vault path: ${VAULT_PATH}\n`);
// Find all markdown files
console.log('🔍 Scanning for markdown files...');
const files = await fg(['**/*.md'], {
cwd: VAULT_PATH,
absolute: true,
ignore: ['**/node_modules/**', '**/.git/**', '**/dist/**']
});
console.log(`✓ Found ${files.length} markdown files\n`);
if (files.length === 0) {
console.log('No markdown files found. Exiting.\n');
return;
}
// Process files
let enriched = 0;
let skipped = 0;
let errors = 0;
console.log('📝 Processing files...\n');
for (let i = 0; i < files.length; i++) {
const file = files[i];
const relativePath = path.relative(VAULT_PATH, file);
const progress = `[${i + 1}/${files.length}]`;
try {
if (isDryRun) {
// In dry-run mode, just log what would happen
console.log(`${progress} Would process: ${relativePath}`);
skipped++;
} else {
const result = await enrichFrontmatterOnOpen(file);
if (result.modified) {
console.log(`${progress} ✓ Enriched: ${relativePath}`);
enriched++;
} else {
console.log(`${progress} ○ Already compliant: ${relativePath}`);
skipped++;
}
}
} catch (error) {
console.error(`${progress} ✗ Error processing ${relativePath}:`, error.message);
errors++;
}
}
// Summary
console.log('\n=====================================');
console.log('📊 Summary\n');
console.log(`Total files: ${files.length}`);
console.log(`Enriched: ${enriched}`);
console.log(`Already OK: ${skipped}`);
console.log(`Errors: ${errors}`);
console.log('=====================================\n');
if (isDryRun) {
console.log('💡 Run without --dry-run to apply changes\n');
} else if (enriched > 0) {
console.log('✨ Enrichment complete!');
console.log('💡 Remember to reindex Meilisearch: npm run meili:reindex\n');
} else {
console.log('✨ All files are already compliant!\n');
}
if (errors > 0) {
process.exit(1);
}
}
// Execute
enrichAllNotes().catch(err => {
console.error('\n❌ Fatal error:', err);
process.exit(1);
});