refactor: docs ordering

This commit is contained in:
iCrawl
2024-03-02 20:25:51 +01:00
parent 18cce83d80
commit 71bba547b6
8 changed files with 109 additions and 74 deletions

View File

@@ -46,6 +46,7 @@
"@vercel/blob": "^0.22.1",
"@vercel/postgres": "^0.7.2",
"meilisearch": "^0.37.0",
"p-limit": "^5.0.0",
"tslib": "^2.6.2",
"undici": "6.6.2"
},

View File

@@ -4,6 +4,7 @@ import { getInput, setFailed } from '@actions/core';
import { create } from '@actions/glob';
import { put } from '@vercel/blob';
import { createPool } from '@vercel/postgres';
import pLimit from 'p-limit';
if (!process.env.DATABASE_URL) {
setFailed('DATABASE_URL is not set');
@@ -16,22 +17,36 @@ const pool = createPool({
connectionString: process.env.DATABASE_URL,
});
const limit = pLimit(10);
const promises = [];
const globber = await create(`packages/${pkg}/docs/docs.api.json`);
for await (const file of globber.globGenerator()) {
const data = await readFile(file, 'utf8');
try {
console.log(`Uploading ${file} with ${version}...`);
const json = JSON.parse(data);
const name = json.name ?? json.n;
const { url } = await put(`${name.replace('@discordjs/', '')}/${version}.json`, data, {
access: 'public',
addRandomSuffix: false,
});
await pool.sql`insert into documentation (name, version, url) values (${name.replace(
'@discordjs/',
'',
)}, ${version}, ${url}) on conflict (name, version) do update set url = EXCLUDED.url`;
promises.push(
// eslint-disable-next-line @typescript-eslint/no-loop-func
limit(async () => {
console.log(`Uploading ${file} with ${version}...`);
const json = JSON.parse(data);
const name = json.name ?? json.n;
const { url } = await put(`${name.replace('@discordjs/', '')}/${version}.json`, data, {
access: 'public',
addRandomSuffix: false,
});
await pool.sql`insert into documentation (name, version, url) values (${name.replace(
'@discordjs/',
'',
)}, ${version}, ${url}) on conflict (name, version) do update set url = EXCLUDED.url`;
}),
);
} catch (error) {
console.log(error);
}
}
try {
await Promise.all(promises);
} catch (error) {
console.log(error);
}

View File

@@ -3,6 +3,7 @@ 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) {
@@ -26,6 +27,9 @@ const client = new MeiliSearch({
apiKey: process.env.SEARCH_API_KEY!,
});
const limit = pLimit(10);
let promises: Promise<any>[] = [];
try {
console.log('Generating all indices...');
const indices = await generateAllIndices({
@@ -49,8 +53,9 @@ try {
console.log('Uploading indices...');
try {
await Promise.all(
indices.map(async (index) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promises = indices.map(async (index) =>
limit(async () => {
console.log(`Uploading ${index.index}...`);
let task;
try {
@@ -71,3 +76,9 @@ try {
const err = error as Error;
setFailed(err.message);
}
try {
await Promise.all(promises);
} catch (error) {
console.log(error);
}

View File

@@ -3,21 +3,37 @@ import { basename } from 'node:path';
import { getInput } from '@actions/core';
import { create } from '@actions/glob';
import { put } from '@vercel/blob';
import pLimit from 'p-limit';
const pkg = getInput('package') || '*';
const version = getInput('version') || 'main';
const limit = pLimit(10);
const promises = [];
const globber = await create(`packages/${pkg}/docs/${pkg}/split/*.api.json`);
for await (const file of globber.globGenerator()) {
const data = await readFile(file, 'utf8');
try {
console.log(`Uploading ${file} with ${version}...`);
const name = basename(file).replace('main.', '');
await put(`rewrite/${pkg}/${version}.${name}`, data, {
access: 'public',
addRandomSuffix: false,
});
promises.push(
// eslint-disable-next-line @typescript-eslint/no-loop-func
limit(async () => {
console.log(`Uploading ${file} with ${version}...`);
const name = basename(file).replace('main.', '');
await put(`rewrite/${pkg}/${version}.${name}`, data, {
access: 'public',
addRandomSuffix: false,
});
}),
);
} catch (error) {
console.log(error);
}
}
try {
await Promise.all(promises);
} catch (error) {
console.log(error);
}