Files
discord.js/packages/actions/src/uploadSearchIndices/index.ts
Almeida e2e71b4d09 build: bump dependencies (#10457)
* build: bump `@vladfrangu/async_event_emitter`

* chore: bump again + fixes

* build: bump types/node and some dev deps

* build: bump discord-api-types again

* style: remove unused eslint-ignore comment

* build: sync dependencies and update templates

* build: bump turbo

* build: vercel + vitest

* build: bump undici

---------

Co-authored-by: Vlad Frangu <me@vladfrangu.dev>
2024-08-22 17:33:35 +02:00

87 lines
2.1 KiB
TypeScript

import process from 'node:process';
import { setFailed } from '@actions/core';
import { generateAllIndices } from '@discordjs/scripts';
import { createPool } from '@vercel/postgres';
import { MeiliSearch } from 'meilisearch';
import pLimit from 'p-limit';
import { fetch } from 'undici';
if (!process.env.DATABASE_URL) {
setFailed('DATABASE_URL is not set');
}
if (!process.env.SEARCH_API_URL) {
setFailed('SEARCH_API_URL is not set');
}
if (!process.env.SEARCH_API_KEY) {
setFailed('SEARCH_API_KEY is not set');
}
const pool = createPool({
connectionString: process.env.DATABASE_URL,
});
const client = new MeiliSearch({
host: process.env.SEARCH_API_URL!,
apiKey: process.env.SEARCH_API_KEY!,
});
const limit = pLimit(10);
let promises: Promise<any>[] = [];
try {
console.log('Generating all indices...');
const indices = await generateAllIndices({
fetchPackageVersions: async (pkg) => {
console.log(`Fetching versions for ${pkg}...`);
const { rows } = await pool.sql`select version from documentation where name = ${pkg}`;
return rows.map((row) => row.version);
},
fetchPackageVersionDocs: async (pkg, version) => {
console.log(`Fetching data for ${pkg} ${version}...`);
const { rows } = await pool.sql`select url from documentation where name = ${pkg} and version = ${version}`;
const res = await fetch(rows[0]?.url ?? '');
return res.json();
},
writeToFile: false,
});
console.log('Generated all indices.');
console.log('Uploading indices...');
try {
promises = indices.map(async (index) =>
limit(async () => {
console.log(`Uploading ${index.index}...`);
let task;
try {
task = await client.createIndex(index.index);
} catch {}
if (task) {
await client.waitForTask(task.taskUid);
}
const searchIndex = client.index(index.index);
await searchIndex.updateSettings({ sortableAttributes: ['type'] });
await searchIndex.addDocuments(index.data);
}),
);
} catch {}
console.log('Uploaded all indices.');
} catch (error) {
const err = error as Error;
setFailed(err.message);
}
try {
await Promise.all(promises);
} catch (error) {
console.log(error);
}