Skip to content

Commit

Permalink
refactor: switch to vercel blob for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iCrawl committed Nov 13, 2023
1 parent ffc3ea5 commit 01c63d2
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 69 deletions.
10 changes: 6 additions & 4 deletions apps/website/src/app/docAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ export const fetchModelJSON = async (packageName: string, version: string) => {

if (process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
try {
const { rows } = await sql`select data from documentation where name = ${packageName} and version = ${'main'}`;
const { rows } = await sql`select url from documentation where name = ${packageName} and version = ${'main'}`;
const res = await fetch(rows[0]?.url ?? '');

return rows[0]?.data ?? null;
return await res.json();
} catch {
return null;
}
}

try {
const { rows } = await sql`select data from documentation where name = ${packageName} and version = ${version}`;
const { rows } = await sql`select url from documentation where name = ${packageName} and version = ${version}`;
const res = await fetch(rows[0]?.url ?? '');

return rows[0]?.data ?? null;
return await res.json();
} catch {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions apps/website/src/components/PackageSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export default function PackageSelect() {

const packageMenu = useMenuState({ gutter: 8, sameWidth: true, fitViewport: true });

// TODO: Version number is currently hard coded
const packageMenuItems = useMemo(
() =>
PACKAGES.map((pkg, idx) => (
<Link href={`/docs/packages/${pkg}/${pkg === 'discord.js' ? '14.14.1' : 'main'}`} key={`${pkg}-${idx}`}>
<Link href={`/docs/packages/${pkg}/main`} key={`${pkg}-${idx}`}>
<MenuItem
className="my-0.5 rounded bg-white p-3 text-sm outline-none active:bg-light-800 dark:bg-dark-600 hover:bg-light-700 focus:ring focus:ring-width-2 focus:ring-blurple dark:active:bg-dark-400 dark:hover:bg-dark-500"
id={pkg}
Expand Down
1 change: 1 addition & 0 deletions packages/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@actions/core": "^1.10.1",
"@actions/glob": "^0.4.0",
"@discordjs/scripts": "workspace:^",
"@vercel/blob": "^0.15.0",
"@vercel/postgres": "^0.5.1",
"meilisearch": "^0.35.0",
"tslib": "^2.6.2",
Expand Down
11 changes: 10 additions & 1 deletion packages/actions/src/uploadDocumentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises';
import process from 'node:process';
import { getInput, setFailed } from '@actions/core';
import { create } from '@actions/glob';
import { put } from '@vercel/blob';
import { createPool } from '@vercel/postgres';

if (!process.env.DATABASE_URL) {
Expand All @@ -20,7 +21,15 @@ for await (const file of globber.globGenerator()) {
const data = await readFile(file, 'utf8');
try {
console.log(`Uploading ${file} with ${version}...`);
await pool.sql`insert into documentation (version, data) values (${version}, ${data}) on conflict (name, version) do update set data = EXCLUDED.data`;
const { name } = JSON.parse(data);
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, url) do update set url = EXCLUDED.url`;
} catch (error) {
const err = error as Error;
console.log(err.message);
Expand Down
6 changes: 4 additions & 2 deletions packages/actions/src/uploadSearchIndices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { setFailed } from '@actions/core';
import { generateAllIndices } from '@discordjs/scripts';
import { createPool } from '@vercel/postgres';
import { MeiliSearch } from 'meilisearch';
import { fetch } from 'undici';

if (!process.env.DATABASE_URL) {
setFailed('DATABASE_URL is not set');
Expand Down Expand Up @@ -36,9 +37,10 @@ try {
},
fetchPackageVersionDocs: async (pkg, version) => {
console.log(`Fetching data for ${pkg} ${version}...`);
const { rows } = await pool.sql`select data from documentation where name = ${pkg} and version = ${version}`;
const { rows } = await pool.sql`select url from documentation where name = ${pkg} and version = ${version}`;
const res = await fetch(rows[0]?.url ?? '');

return rows[0]?.data ?? null;
return res.json();
},
writeToFile: false,
});
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@discordjs/api-extractor-utils": "workspace:^",
"@microsoft/tsdoc": "0.14.2",
"@microsoft/tsdoc-config": "0.16.2",
"@vercel/blob": "^0.15.0",
"@vercel/postgres": "^0.5.1",
"tslib": "^2.6.2",
"undici": "5.27.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function fetchVersions(pkg: string) {

export async function fetchVersionDocs(pkg: string, version: string) {
const response = await request(`https://docs.discordjs.dev/docs/${pkg}/${version}.api.json`);
return response.body.json() as Promise<Record<any, any>>;
return response.body.json();
}

export async function generateAllIndices({
Expand Down
20 changes: 18 additions & 2 deletions packages/scripts/src/populateDevDatabaseBranch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFile } from 'node:fs/promises';
import process, { cwd } from 'node:process';
import { create } from '@actions/glob';
import { put } from '@vercel/blob';
import { createPool } from '@vercel/postgres';

const pool = createPool({
Expand All @@ -16,14 +17,29 @@ for await (const file of globber.globGenerator()) {
if (parsed?.groups) {
console.log(parsed.groups.semver, file);
try {
await pool.sql`insert into documentation (version, data) values (${parsed.groups.semver}, ${data}) on conflict (name, version) do update set data = EXCLUDED.data`;
const { name } = JSON.parse(data);
const { url } = await put(`${name.replace('@discordjs/', '')}/${parsed.groups.semver}.json`, data, {
access: 'public',
addRandomSuffix: false,
});
await pool.sql`insert into documentation (name, version, url) values (${name.replace('@discordjs/', '')}, ${
parsed.groups.semver
}, ${url}) on conflict (name, version) do update set url = EXCLUDED.url`;
} catch (error) {
console.error(error);
}
} else {
console.log('main', file);
try {
await pool.sql`insert into documentation (version, data) values (${'main'}, ${data}) on conflict (name, version) do update set data = EXCLUDED.data`;
const { name } = JSON.parse(data);
const { url } = await put(`${name.replace('@discordjs/', '')}/main.json`, data, {
access: 'public',
addRandomSuffix: false,
});
await pool.sql`insert into documentation (name, version, url) values (${name.replace(
'@discordjs/',
'',
)}, ${'main'}, ${url}) on conflict (name, version) do update set url = EXCLUDED.url`;
} catch (error) {
console.error(error);
}
Expand Down
Loading

1 comment on commit 01c63d2

@vercel
Copy link

@vercel vercel bot commented on 01c63d2 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.