import { readFileSync, writeFileSync, copyFileSync } from 'fs'; import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; import glob from 'glob'; const __dirname = dirname(fileURLToPath(import.meta.url)); const genAuthors = (a) => { let authors = []; if (typeof a === 'string') { authors = a.split(', '); } else if (Array.isArray(a)) { authors = a; } return authors .map((x, i) => { if (typeof x === 'object') { // User object const pfp = ``; const name = `${x.n}`; //#${result.discriminator}`; return i > 1 ? pfp : pfp + name; } let idMatch = x.match(/(.*) \(([0-9]{17,18})\)/); // " ()" if (idMatch === null) return `${x}`; // "" return `${idMatch[1]}`; }) .join(', '); }; const makeCard = (m) => { const authors = genAuthors(m.authors); return `
${authors}
${m.name}
${ m.description }
${m.github.stars}
${ m.version === '0' || m.version.toLowerCase().includes('auto') ? '' : `v${m.version}` }
`; }; const makeRepo = (r) => { return `
${r.name}
${r.description}
${r.themes} themes
${r.plugins} plugins
${r.developers} developers
`; }; export default () => { let template = readFileSync(join(__dirname, 'template.html'), 'utf8'); const repos = glob.sync(join(global.distDir, '*.json')); let cards = []; let metas = []; let name = 'GooseMod Store'; let description = ''; for (const repo of repos) { console.log(repo); const json = JSON.parse(readFileSync(repo, 'utf8')); if (repos.length === 1) { // 1 repo likely means a custom repo, so use that name and description name = json.meta.name; description = `${json.meta.description} `; } cards = cards.concat(json.modules); metas.push({ ...json.meta, themes: json.modules.filter((x) => x.tags.includes('theme')).length, plugins: json.modules.filter((x) => !x.tags.includes('theme')).length, developers: Object.keys( json.modules.reduce((acc, x) => { (!Array.isArray(x.authors) ? [x.authors] : x.authors).forEach( (a) => (acc[typeof a === 'object' ? a.i : a] = true), ); return acc; }, {}), ).length, filename: repo.split('/').pop(), }); } description += `Browse ${cards.filter((x) => x.tags.includes('theme')).length} themes and ${ cards.filter((x) => !x.tags.includes('theme')).length } plugins from ${ Object.keys( cards.reduce((acc, x) => { (!Array.isArray(x.authors) ? [x.authors] : x.authors).forEach( (a) => (acc[typeof a === 'object' ? a.i : a] = true), ); return acc; }, {}), ).length } developers.`; cards = cards.sort((a, b) => a.name.localeCompare(b.name)).map((x) => makeCard(x)); template = template .replace('ALL_CARDS', cards.join('\n')) .replace('REPOS', metas.map((x) => makeRepo(x)).join('\n')) .replace(/NAME/g, name) .replace(/DESCRIPTION/g, description); writeFileSync(join(global.distDir, 'index.html'), template); copyFileSync(join(__dirname, 'NotoSans-Medium.ttf'), join(global.distDir, 'NotoSans-Medium.ttf')); };