Skip to content

Commit 2270cd1

Browse files
Copilotna-trium-144
andcommitted
Generate languages.yml at build time, remove hardcoded LANGUAGE_IDS from getPagesList
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent dd34b4b commit 2270cd1

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# generated docs section file lists (regenerated by npm run generateSections)
77
/public/docs/**/sections.yml
88

9+
# generated languages list (regenerated by npm run generateLanguages)
10+
/public/docs/languages.yml
11+
912
# dependencies
1013
/node_modules
1114
/.pnp

app/lib/getPagesList.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCloudflareContext } from "@opennextjs/cloudflare";
2-
import { readFile } from "node:fs/promises";
2+
import { readFile, readdir } from "node:fs/promises";
33
import { join } from "node:path";
44
import yaml from "js-yaml";
55
import { isCloudflare } from "./detectCloudflare";
@@ -23,35 +23,39 @@ interface IndexYml {
2323
pages: PageEntry[];
2424
}
2525

26-
const LANGUAGE_IDS = [
27-
"python",
28-
"ruby",
29-
"javascript",
30-
"typescript",
31-
"cpp",
32-
"rust",
33-
] as const;
34-
35-
async function readIndexYml(langId: string): Promise<string> {
26+
async function readPublicFile(path: string): Promise<string> {
3627
if (isCloudflare()) {
3728
const cfAssets = getCloudflareContext().env.ASSETS;
38-
const res = await cfAssets!.fetch(
39-
`https://assets.local/docs/${langId}/index.yml`
40-
);
41-
if (!res.ok) throw new Error(`Failed to fetch index.yml for ${langId}`);
29+
const res = await cfAssets!.fetch(`https://assets.local/${path}`);
30+
if (!res.ok) throw new Error(`Failed to fetch ${path}`);
4231
return await res.text();
4332
} else {
4433
return await readFile(
45-
join(process.cwd(), "public", "docs", langId, "index.yml"),
34+
join(process.cwd(), "public", path),
4635
"utf-8"
4736
);
4837
}
4938
}
5039

40+
async function getLanguageIds(): Promise<string[]> {
41+
if (isCloudflare()) {
42+
const raw = await readPublicFile("docs/languages.yml");
43+
return yaml.load(raw) as string[];
44+
} else {
45+
const docsDir = join(process.cwd(), "public", "docs");
46+
const entries = await readdir(docsDir, { withFileTypes: true });
47+
return entries
48+
.filter((e) => e.isDirectory())
49+
.map((e) => e.name)
50+
.sort();
51+
}
52+
}
53+
5154
export async function getPagesList(): Promise<LanguageEntry[]> {
55+
const langIds = await getLanguageIds();
5256
return await Promise.all(
53-
LANGUAGE_IDS.map(async (langId) => {
54-
const raw = await readIndexYml(langId);
57+
langIds.map(async (langId) => {
58+
const raw = await readPublicFile(`docs/${langId}/index.yml`);
5559
const data = yaml.load(raw) as IndexYml;
5660
return {
5761
id: langId,

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"dev": "npm run cf-typegen && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next dev",
8-
"build": "npm run cf-typegen && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next build",
7+
"dev": "npm run cf-typegen && npm run generateLanguages && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next dev",
8+
"build": "npm run cf-typegen && npm run generateLanguages && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next build",
99
"start": "next start",
1010
"lint": "npm run cf-typegen && next lint",
1111
"tsc": "npm run cf-typegen && tsc",
1212
"format": "prettier --write app/",
13+
"generateLanguages": "tsx ./scripts/generateLanguagesList.ts",
1314
"generateSections": "tsx ./scripts/generateSectionsList.ts",
1415
"copyAllDTSFiles": "tsx ./scripts/copyAllDTSFiles.ts",
1516
"removeHinting": "tsx ./scripts/removeHinting.ts",

scripts/generateLanguagesList.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Generates public/docs/languages.yml listing all language directories.
2+
3+
import { readdir, writeFile, stat } from "node:fs/promises";
4+
import { join } from "node:path";
5+
import yaml from "js-yaml";
6+
7+
const docsDir = join(process.cwd(), "public", "docs");
8+
9+
const entries = await readdir(docsDir);
10+
const langIds: string[] = [];
11+
for (const entry of entries) {
12+
const entryPath = join(docsDir, entry);
13+
if ((await stat(entryPath)).isDirectory()) {
14+
langIds.push(entry);
15+
}
16+
}
17+
langIds.sort();
18+
19+
const yamlContent = yaml.dump(langIds);
20+
await writeFile(join(docsDir, "languages.yml"), yamlContent, "utf-8");
21+
console.log(`Generated languages.yml (${langIds.length} languages: ${langIds.join(", ")})`);

0 commit comments

Comments
 (0)