mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
feat: local and preview detection
This commit is contained in:
4
apps/website/src/util/env.ts
Normal file
4
apps/website/src/util/env.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const ENV = {
|
||||
IS_LOCAL_DEV: process.env.VERCEL_ENV === 'development' || process.env.NEXT_PUBLIC_LOCAL_DEV === 'true',
|
||||
IS_PREVIEW: process.env.VERCEL_ENV === 'preview',
|
||||
};
|
||||
30
apps/website/src/util/fetchDependencies.ts
Normal file
30
apps/website/src/util/fetchDependencies.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { ENV } from './env';
|
||||
|
||||
export async function fetchDependencies({
|
||||
packageName,
|
||||
version,
|
||||
}: {
|
||||
readonly packageName: string;
|
||||
readonly version: string;
|
||||
}) {
|
||||
if (ENV.IS_LOCAL_DEV) {
|
||||
const fileContent = await readFile(
|
||||
join(process.cwd(), `../../packages/${packageName}/docs/${packageName}/split/${version}.dependencies.api.json`),
|
||||
'utf8',
|
||||
);
|
||||
|
||||
return JSON.parse(fileContent);
|
||||
}
|
||||
|
||||
const isMainVersion = version === 'main';
|
||||
const fileContent = await fetch(
|
||||
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.dependencies.api.json`,
|
||||
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
|
||||
);
|
||||
const parsedDependencies = await fileContent.json();
|
||||
return Object.entries<string>(parsedDependencies)
|
||||
.filter(([key]) => key.startsWith('@discordjs/') && !key.includes('api-extractor'))
|
||||
.map(([key, value]) => `${key.replace('@discordjs/', '').replaceAll('.', '-')}-${value.replaceAll('.', '-')}`);
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { ENV } from './env';
|
||||
|
||||
export async function fetchLatestVersion(packageName: string): Promise<string> {
|
||||
if (ENV.IS_LOCAL_DEV) {
|
||||
return 'main';
|
||||
}
|
||||
|
||||
const { rows } = await sql`select version from documentation where name = ${packageName} order by version desc`;
|
||||
|
||||
return rows.map((row) => row.version).at(1) ?? 'main';
|
||||
|
||||
30
apps/website/src/util/fetchNode.ts
Normal file
30
apps/website/src/util/fetchNode.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { ENV } from './env';
|
||||
|
||||
export async function fetchNode({
|
||||
item,
|
||||
packageName,
|
||||
version,
|
||||
}: {
|
||||
readonly item: any;
|
||||
readonly packageName: string;
|
||||
readonly version: string;
|
||||
}) {
|
||||
const normalizeItem = item.split(encodeURIComponent(':')).join('.').toLowerCase();
|
||||
|
||||
if (ENV.IS_LOCAL_DEV) {
|
||||
const fileContent = await readFile(
|
||||
join(process.cwd(), `../../packages/${packageName}/docs/split/${version}.${normalizeItem}.api.json`),
|
||||
'utf8',
|
||||
);
|
||||
return JSON.parse(fileContent);
|
||||
}
|
||||
|
||||
const isMainVersion = version === 'main';
|
||||
const fileContent = await fetch(
|
||||
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.${normalizeItem}.api.json`,
|
||||
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
|
||||
);
|
||||
return fileContent.json();
|
||||
}
|
||||
26
apps/website/src/util/fetchSitemap.ts
Normal file
26
apps/website/src/util/fetchSitemap.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { ENV } from './env';
|
||||
|
||||
export async function fetchSitemap({
|
||||
packageName,
|
||||
version,
|
||||
}: {
|
||||
readonly packageName: string;
|
||||
readonly version: string;
|
||||
}) {
|
||||
if (ENV.IS_LOCAL_DEV) {
|
||||
const fileContent = await readFile(
|
||||
join(process.cwd(), `../../packages/${packageName}/docs/split/${version}.sitemap.api.json`),
|
||||
'utf8',
|
||||
);
|
||||
return JSON.parse(fileContent);
|
||||
}
|
||||
|
||||
const isMainVersion = version === 'main';
|
||||
const fileContent = await fetch(
|
||||
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.sitemap.api.json`,
|
||||
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
|
||||
);
|
||||
return fileContent.json();
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { ENV } from './env';
|
||||
|
||||
export async function fetchVersions(packageName: string) {
|
||||
if (ENV.IS_LOCAL_DEV) {
|
||||
return [{ version: 'main' }];
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows } = await sql<{
|
||||
version: string;
|
||||
|
||||
Reference in New Issue
Block a user