Add generator plugin

This commit is contained in:
venashial
2022-05-20 00:10:15 -07:00
parent 06c210f5c8
commit 0f6263d82e
11 changed files with 1446 additions and 147 deletions

View File

@@ -0,0 +1,44 @@
import { fetch } from 'undici';
import { promises as fs } from 'fs';
import sharp from 'sharp';
import FastAverageColor from 'fast-average-color';
import cliProgress from 'cli-progress';
export async function landingPage(API_URL: string) {
const progressBar = new cliProgress.SingleBar({
format: 'Generating landing page | {bar} | {percentage}% || {value}/{total} mods',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
});
progressBar.start(100, 0);
// Fetch top 100 mods
const response = (await (
await fetch(API_URL + 'search?limit=100&facets=[["project_type:mod"]]')
).json()) as Record<string, any>;
// Simplified array with the format: ['id', 'slug', 'icon_extension']
const compressed = response.hits
.filter((project) => project.icon_url)
.map((project) => {
progressBar.increment();
return [
project.project_id,
project.slug || '',
project.icon_url.match(/\.[0-9a-z]+$/i)[0].substring(1),
];
});
// Write JSON file
await fs.writeFile(
'./generated/landingPage.json',
JSON.stringify({
mods: compressed,
random: Math.random(),
})
);
progressBar.stop();
}