touhou-tagger
Version:
从 THBWiki 自动填写东方 Project CD 曲目信息.
83 lines (82 loc) • 2.78 kB
JavaScript
import { readdir } from 'fs/promises';
import { join } from 'path';
import { log } from '../core/debug.js';
import { getDefaultAlbumName } from './default-album-name.js';
import { asyncFlatMap } from './helper.js';
const readFolder = async (folder, depth) => {
const currentSubFolders = (await readdir(folder, { withFileTypes: true }))
.filter(dir => dir.isDirectory())
.map(dir => ({
name: dir.name,
path: join(folder, dir.name),
}));
if (depth <= 1) {
return currentSubFolders;
}
const allSubFolders = await asyncFlatMap(currentSubFolders, subFolder => readFolder(join(folder, subFolder.name), depth - 1));
return allSubFolders;
};
const createBatchRun = async (config) => {
const { folder, depth, oraOptions, onProcess } = config;
const albums = await readFolder(folder, depth);
const albumCount = albums.length;
const { default: ora } = await import('ora');
for (let index = 0; index < albumCount; index++) {
try {
const album = await getDefaultAlbumName(albums[index].name);
const spinner = ora(oraOptions).start();
spinner.prefixText = `[${album}] (${index + 1}/${albumCount})`;
log(`start processing album #${index + 1}`);
await onProcess({
currentAlbum: album,
workingDir: albums[index].path,
spinner,
index,
});
log(`processed album #${index + 1}`);
}
catch (error) {
log('batch error:', error.message);
continue;
}
}
};
export const runBatchTagger = async (folder, depth) => {
const { CliTagger } = await import('./tagger.js');
await createBatchRun({
folder,
depth,
oraOptions: {
text: '搜索中',
spinner: {
interval: 500,
frames: ['. ', '.. ', '...'],
},
},
onProcess: async ({ currentAlbum, workingDir, spinner }) => {
const tagger = new CliTagger(spinner);
tagger.workingDir = workingDir;
await tagger.run(currentAlbum);
},
});
};
export const runBatchDump = async (folder, depth) => {
const { CliDumper } = await import('./dumper.js');
await createBatchRun({
folder,
depth,
oraOptions: {
text: '提取中',
spinner: {
interval: 500,
frames: ['. ', '.. ', '...'],
},
},
onProcess: async ({ workingDir, spinner }) => {
const tagger = new CliDumper();
tagger.workingDir = workingDir;
await tagger.run();
spinner.stop();
},
});
};