import { readFile } from 'fs/promises'; import { getArg } from './lib/args.js'; import { getUser } from './lib/dl.js'; import { error, log } from './lib/log.js'; const ctx = 'buildDb.js'; const getMany = (processes, threadMax, directory) => new Promise((resolve, reject) => { let running = 0; let index = 0; const get = () => { const onFinish = (currentIndex) => { log(ctx, `Finished ${processes[currentIndex].user}/media`); --running; get(); } while (running < threadMax && index < processes.length) { ++running; let currentIndex = index++; let proc = getUser(processes[currentIndex].user, directory); proc.stdout.on('data', data => { processes[currentIndex].logs.push(data); }); proc.stderr.on('data', _ => onFinish(currentIndex)); proc.on('close', _ => onFinish(currentIndex)); proc.on('error', _ => onFinish(currentIndex)); } if (running === 0) { resolve(); } } get(); }); const buildDb = async () => { log(ctx, 'Grabbing db'); let directory = '', threadMax = 1, db; try { directory = getArg('path'); } catch (err) { error(ctx, err); return; } try { threadMax = getArg('threads'); log(ctx, `Using ${threadMax} threads`); } catch (err) { log(ctx, 'Using 1 thread'); } try { let file = await readFile(`${directory}/db.json`, { encoding: 'utf8' }); db = JSON.parse(file); } catch (err) { error(ctx, err); return; } let processes = db.map(entry => ({ ...entry, logs: [], })); log(ctx, `Building db using /media for ${processes.length} users`); await getMany(processes, threadMax, directory); log(ctx, 'Building db using /search'); } buildDb();