fix(cli): adopted audio is labelled voice or sfx by its folder or file name, not always bgm

This commit is contained in:
Miguel Angel Simon Sierra
2026-10-30 00:54:22 -04:00
parent 21eefcd006
commit f8444dbb94
2 changed files with 32 additions and 10 deletions
+12 -10
View File
@@ -9,18 +9,20 @@ const AUDIO_EXT = new Set([".mp3", ".wav", ".ogg", ".m4a", ".aac"]);
const IMAGE_EXT = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg", ".ico"]);
const VIDEO_EXT = new Set([".mp4", ".webm", ".mov"]);
// A folder or a file name says what an audio file is (voice/intro.mp3, voice-intro.mp3); only a file that says
// nothing is background music.
function audioKind(filePath) {
const words = filePath.toLowerCase().split(/[^a-z0-9]+/);
const has = (test) => words.some(test);
if (has((w) => w === "bgm" || w === "music" || w === "soundtrack")) return "bgm";
if (has((w) => w === "sfx" || w === "fx" || w === "sound" || w === "sounds")) return "sfx";
if (has((w) => w.startsWith("voice") || w.startsWith("narrat"))) return "voice";
return "bgm";
}
function inferType(filePath) {
const ext = extname(filePath).toLowerCase();
if (AUDIO_EXT.has(ext)) {
const lower = filePath.toLowerCase();
if (lower.includes("/bgm/") || lower.includes("/music/") || lower.startsWith("bgm/"))
return "bgm";
if (lower.includes("/sfx/") || lower.includes("/sound") || lower.startsWith("sfx/"))
return "sfx";
if (lower.includes("/voice/") || lower.includes("/narrat") || lower.startsWith("voice/"))
return "voice";
return "bgm";
}
if (AUDIO_EXT.has(ext)) return audioKind(filePath);
if (IMAGE_EXT.has(ext)) {
if (ext === ".svg" || ext === ".ico") return "icon";
return "image";
@@ -74,6 +74,26 @@ function runTests() {
cleanup();
});
test("labels an audio file by its folder or its file name, not always bgm", () => {
setup();
drop("voice-intro.mp3");
drop("sfx-click.mp3");
drop("narration.mp3");
drop("soundtrack/main.mp3");
drop("track.mp3");
const types = Object.fromEntries(
adoptExistingAssets(tmp).map((record) => [record.path, record.type]),
);
assert.deepEqual(types, {
"assets/voice-intro.mp3": "voice",
"assets/sfx-click.mp3": "sfx",
"assets/narration.mp3": "voice",
"assets/soundtrack/main.mp3": "bgm",
"assets/track.mp3": "bgm",
});
cleanup();
});
let passed = 0;
let failed = 0;
for (const { name, fn } of tests) {