72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
const fs = require('fs/promises');
|
|
const fsSync = require('fs');
|
|
const path = require('path');
|
|
const { promptForBundlePath } = require('./lib/bundles');
|
|
|
|
const CONTENT_DIR = path.resolve('content');
|
|
const TEMPLATE_PATH = path.resolve('data/metadata_template.yaml');
|
|
const MEDIA_TYPES = ['images', 'sounds', 'videos'];
|
|
|
|
async function loadTemplate() {
|
|
return fs.readFile(TEMPLATE_PATH, 'utf8');
|
|
}
|
|
|
|
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);
|
|
|
|
let dataDirEnsured = false; // Create lazily only if a YAML file must be written
|
|
|
|
if (!fsSync.existsSync(mediaDir)) {
|
|
console.log(`Skipped: no folder "${type}" found.`);
|
|
|
|
continue;
|
|
}
|
|
|
|
const files = await fs.readdir(mediaDir);
|
|
|
|
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);
|
|
|
|
if (fsSync.existsSync(yamlPath)) {
|
|
console.log(`Skipped: ${yamlPath} (already exists)`);
|
|
|
|
continue;
|
|
}
|
|
|
|
if (!dataDirEnsured) {
|
|
await fs.mkdir(dataDir, { recursive: true });
|
|
|
|
dataDirEnsured = true;
|
|
}
|
|
|
|
await fs.writeFile(yamlPath, yamlTemplate, 'utf8');
|
|
|
|
console.log(`Created: ${yamlPath}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const manualPath = process.argv[2];
|
|
const bundle = await promptForBundlePath(manualPath, { contentDir: CONTENT_DIR });
|
|
|
|
const template = await loadTemplate();
|
|
|
|
await generateYamlFiles(bundle, template);
|
|
}
|
|
|
|
main();
|