1

Metadata file generator helper

This commit is contained in:
2025-03-30 23:21:47 +02:00
parent 8a64bde085
commit 313d2c26ca
2 changed files with 118 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
#title: ""
#attribution: "Richard Dern"
#description: ""
#prompt: ""

View File

@@ -0,0 +1,114 @@
const fs = require('fs/promises');
const path = require('path');
const readline = require('readline');
const CONTENT_DIR = path.resolve('content');
const TEMPLATE_PATH = path.resolve('data/metadata_template.yaml');
const MEDIA_TYPES = ['images', 'sounds', 'videos'];
function askQuestion(query) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(query, answer => { rl.close(); resolve(answer.trim()); }));
}
async function findLatestBundle(dir) {
let latest = { path: null, time: 0 };
async function search(current) {
const entries = await fs.readdir(current, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(current, entry.name);
if (entry.isDirectory()) {
const hasIndex = (await fs.readdir(fullPath)).includes('index.md');
if (hasIndex) {
const stat = await fs.stat(fullPath);
if (stat.mtimeMs > latest.time) {
latest = { path: fullPath, time: stat.mtimeMs };
}
} else {
await search(fullPath);
}
}
}
}
await search(dir);
return latest.path;
}
async function loadTemplate() {
try {
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) {
console.log(`\nProcessing bundle: ${bundlePath}`);
for (const type of MEDIA_TYPES) {
const mediaDir = path.join(bundlePath, type);
const dataDir = path.join(bundlePath, 'data', type);
try {
await fs.mkdir(dataDir, { recursive: true });
} catch {}
let files;
try {
files = await fs.readdir(mediaDir);
} catch {
console.log(`Skipped: no folder "${type}" found.`);
continue;
}
for (const file of files) {
const ext = path.extname(file);
if (!ext) {
console.log(`Ignored: ${file} (no extension)`);
continue;
}
const yamlName = path.basename(file, ext) + '.yaml';
const yamlPath = path.join(dataDir, yamlName);
try {
await fs.access(yamlPath);
console.log(`Skipped: ${yamlPath} (already exists)`);
continue;
} catch {
await fs.writeFile(yamlPath, yamlTemplate, 'utf8');
console.log(`Created: ${yamlPath}`);
}
}
}
}
async function main() {
const manualPath = process.argv[2];
let bundle;
if (manualPath) {
bundle = path.resolve(manualPath);
} else {
const latest = await findLatestBundle(CONTENT_DIR);
if (!latest) {
console.error('No bundle found in content/.');
return;
}
const confirm = await askQuestion(`Use latest bundle found: ${latest}? (Y/n) `);
if (confirm.toLowerCase() === 'n') {
const inputPath = await askQuestion('Enter the relative path to your bundle: ');
bundle = path.resolve(inputPath);
} else {
bundle = latest;
}
}
const template = await loadTemplate();
await generateYamlFiles(bundle, template);
}
main().catch(err => {
console.error('Error:', err);
});