1

refactor(tools): lazy-create media metadata dirs and remove try/catch

Create data/<type> only when writing first YAML; replace exception-based flow with existence checks and simplify template loading and main flow.
This commit is contained in:
2025-09-06 11:44:28 +02:00
parent e83867594d
commit cc7141ec15

View File

@@ -1,4 +1,5 @@
const fs = require('fs/promises'); const fs = require('fs/promises');
const fsSync = require('fs');
const path = require('path'); const path = require('path');
const readline = require('readline'); const readline = require('readline');
@@ -8,6 +9,7 @@ const MEDIA_TYPES = ['images', 'sounds', 'videos'];
function askQuestion(query) { function askQuestion(query) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(query, answer => { rl.close(); resolve(answer.trim()); })); return new Promise(resolve => rl.question(query, answer => { rl.close(); resolve(answer.trim()); }));
} }
@@ -16,12 +18,16 @@ async function findLatestBundle(dir) {
async function search(current) { async function search(current) {
const entries = await fs.readdir(current, { withFileTypes: true }); const entries = await fs.readdir(current, { withFileTypes: true });
for (const entry of entries) { for (const entry of entries) {
const fullPath = path.join(current, entry.name); const fullPath = path.join(current, entry.name);
if (entry.isDirectory()) { if (entry.isDirectory()) {
const hasIndex = (await fs.readdir(fullPath)).includes('index.md'); const hasIndex = (await fs.readdir(fullPath)).includes('index.md');
if (hasIndex) { if (hasIndex) {
const stat = await fs.stat(fullPath); const stat = await fs.stat(fullPath);
if (stat.mtimeMs > latest.time) { if (stat.mtimeMs > latest.time) {
latest = { path: fullPath, time: stat.mtimeMs }; latest = { path: fullPath, time: stat.mtimeMs };
} }
@@ -33,72 +39,82 @@ async function findLatestBundle(dir) {
} }
await search(dir); await search(dir);
return latest.path; return latest.path;
} }
async function loadTemplate() { async function loadTemplate() {
try { return fs.readFile(TEMPLATE_PATH, 'utf8');
const content = await fs.readFile(TEMPLATE_PATH, 'utf8');
return content;
} catch (err) {
console.error(`Error loading template from ${TEMPLATE_PATH}`);
throw err;
}
} }
async function generateYamlFiles(bundlePath, yamlTemplate) { async function generateYamlFiles(bundlePath, yamlTemplate) {
console.log(`\nProcessing bundle: ${bundlePath}`); console.log(`\nProcessing bundle: ${bundlePath}`);
for (const type of MEDIA_TYPES) { for (const type of MEDIA_TYPES) {
const mediaDir = path.join(bundlePath, type); const mediaDir = path.join(bundlePath, type);
const dataDir = path.join(bundlePath, 'data', type); const dataDir = path.join(bundlePath, 'data', type);
try {
await fs.mkdir(dataDir, { recursive: true });
} catch {}
let files; let dataDirEnsured = false; // Create lazily only if a YAML file must be written
try {
files = await fs.readdir(mediaDir); if (!fsSync.existsSync(mediaDir)) {
} catch {
console.log(`Skipped: no folder "${type}" found.`); console.log(`Skipped: no folder "${type}" found.`);
continue; continue;
} }
const files = await fs.readdir(mediaDir);
for (const file of files) { for (const file of files) {
const ext = path.extname(file); const ext = path.extname(file);
if (!ext) { if (!ext) {
console.log(`Ignored: ${file} (no extension)`); console.log(`Ignored: ${file} (no extension)`);
continue; continue;
} }
const yamlName = path.basename(file, ext) + '.yaml'; const yamlName = path.basename(file, ext) + '.yaml';
const yamlPath = path.join(dataDir, yamlName); const yamlPath = path.join(dataDir, yamlName);
try { if (fsSync.existsSync(yamlPath)) {
await fs.access(yamlPath);
console.log(`Skipped: ${yamlPath} (already exists)`); console.log(`Skipped: ${yamlPath} (already exists)`);
continue; continue;
} catch {
await fs.writeFile(yamlPath, yamlTemplate, 'utf8');
console.log(`Created: ${yamlPath}`);
} }
if (!dataDirEnsured) {
await fs.mkdir(dataDir, { recursive: true });
dataDirEnsured = true;
}
await fs.writeFile(yamlPath, yamlTemplate, 'utf8');
console.log(`Created: ${yamlPath}`);
} }
} }
} }
async function main() { async function main() {
const manualPath = process.argv[2]; const manualPath = process.argv[2];
let bundle; let bundle;
if (manualPath) { if (manualPath) {
bundle = path.resolve(manualPath); bundle = path.resolve(manualPath);
} else { } else {
const latest = await findLatestBundle(CONTENT_DIR); const latest = await findLatestBundle(CONTENT_DIR);
if (!latest) { if (!latest) {
console.error('No bundle found in content/.'); console.error('No bundle found in content/.');
return; return;
} }
const confirm = await askQuestion(`Use latest bundle found: ${latest}? (Y/n) `); const confirm = await askQuestion(`Use latest bundle found: ${latest}? (Y/n) `);
if (confirm.toLowerCase() === 'n') { if (confirm.toLowerCase() === 'n') {
const inputPath = await askQuestion('Enter the relative path to your bundle: '); const inputPath = await askQuestion('Enter the relative path to your bundle: ');
bundle = path.resolve(inputPath); bundle = path.resolve(inputPath);
} else { } else {
bundle = latest; bundle = latest;
@@ -106,9 +122,8 @@ async function main() {
} }
const template = await loadTemplate(); const template = await loadTemplate();
await generateYamlFiles(bundle, template); await generateYamlFiles(bundle, template);
} }
main().catch(err => { main();
console.error('Error:', err);
});