Skip to content

Commit 970ea29

Browse files
committed
move: jdks/sdks page generation to separate preprocessing script
1 parent f3fe148 commit 970ea29

File tree

7 files changed

+118
-32
lines changed

7 files changed

+118
-32
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
2121

22+
# Hide preprocessing out
23+
/preprocessing/out
24+
2225
# Hide auto generated doc pages
2326
/docs/jdks.mdx
27+
/docs/sdks.mdx

docusaurus.config.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ const config: Config = {
4949
],
5050
],
5151

52-
plugins: [
53-
'docusaurus-plugin-sass',
54-
'./src/plugins/google-fonts.ts',
55-
'./src/plugins/jdks.ts',
56-
],
52+
plugins: ['docusaurus-plugin-sass', './src/plugins/google-fonts.ts'],
5753

5854
themeConfig: {
5955
// Replace with your project's social card

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",
7-
"start": "docusaurus start",
8-
"build": "docusaurus build",
7+
"start": "npm run preprocessing:start && npm run docusaurus start",
8+
"build": "npm run preprocessing:start && npm run docusaurus build",
99
"swizzle": "docusaurus swizzle",
1010
"deploy": "docusaurus deploy",
11-
"clear": "docusaurus clear",
11+
"clear": "npm run preprocessing:clear && npm run docusaurus clear",
1212
"serve": "docusaurus serve",
1313
"write-translations": "docusaurus write-translations",
1414
"write-heading-ids": "docusaurus write-heading-ids",
15-
"typecheck": "tsc"
15+
"typecheck": "tsc",
16+
"preprocessing:start": "tsc --outDir ./preprocessing/out ./preprocessing/src/*.ts && node ./preprocessing/out/index.js",
17+
"preprocessing:clear": "rm -rf ./preprocessing/out ./docs/jdks.mdx ./docs/sdks.mdx"
1618
},
1719
"dependencies": {
1820
"@docusaurus/core": "^3.5.1",

preprocessing/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import generateJDKs from './jdks';
2+
import generateSDKs from './sdks';
3+
4+
async function run() {
5+
try {
6+
await Promise.all([generateJDKs(), generateSDKs()]);
7+
} catch (err) {
8+
console.error(err);
9+
}
10+
}
11+
12+
run();

src/plugins/jdks.ts preprocessing/src/jdks/index.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
import fs from 'node:fs/promises';
2-
import path from 'node:path';
3-
4-
import { vendors } from '../data/jdks';
5-
6-
export default function jdksPlugin() {
7-
return {
8-
name: 'docusaurus-jdks-plugin',
9-
async loadContent() {
10-
const jdkList = getJDKsMarkup(vendors);
11-
const template = getMarkup(jdkList);
12-
13-
try {
14-
await fs.writeFile(
15-
path.resolve(__dirname, '../../docs/jdks.mdx'),
16-
template,
17-
);
18-
} catch (err) {
19-
console.error(err);
20-
}
21-
},
22-
};
1+
import { writeFile } from 'node:fs/promises';
2+
import { resolve } from 'node:path';
3+
4+
import vendors from './vendors';
5+
6+
export default async function generateJDKs() {
7+
const jdkList = getJDKsMarkup(vendors);
8+
const template = getMarkup(jdkList);
9+
10+
try {
11+
await writeFile(resolve(process.cwd(), './docs/jdks.mdx'), template);
12+
} catch (err) {
13+
console.error(err);
14+
}
2315
}
2416

2517
function getMarkup(content: string) {

src/data/jdks.ts preprocessing/src/jdks/vendors.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const vendors = [
1+
const vendors = [
22
{
33
id: 'amzn',
44
vendor: 'Amazon',
@@ -201,3 +201,5 @@ export const vendors = [
201201
solution-oriented engineering assistance.`,
202202
},
203203
];
204+
205+
export default vendors;

preprocessing/src/sdks/index.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { writeFile } from 'node:fs/promises';
2+
import { resolve } from 'node:path';
3+
4+
export default async function generateSDKs() {
5+
const sdksData = await fetchSdks();
6+
const sdks = parseSDKs(sdksData);
7+
const sdkList = getSDKsMarkup(sdks);
8+
const template = getMarkup(sdkList);
9+
10+
try {
11+
await writeFile(resolve(process.cwd(), './docs/sdks.mdx'), template);
12+
} catch (err) {
13+
console.error(err);
14+
}
15+
}
16+
17+
function getMarkup(content: string) {
18+
return `---
19+
title: SDK Installation Candidates
20+
---
21+
22+
{/* ATTENTION! Do not edit, the file is generated automatically by the plugin */}
23+
24+
${content}`;
25+
}
26+
27+
function getSDKsMarkup(sdks: any[]) {
28+
return sdks.reduce((acc, val) => {
29+
const item = `
30+
## ${val.title} \\{#${val.id}\\}
31+
32+
[${val.url}](${val.url})
33+
34+
${val.description}
35+
36+
\`\`\`shell
37+
sdk install ${val.id}
38+
\`\`\`
39+
`;
40+
41+
acc += `${item}`;
42+
43+
return acc;
44+
}, '');
45+
}
46+
47+
async function fetchSdks() {
48+
try {
49+
const res = await fetch('https://api.sdkman.io/2/candidates/list');
50+
51+
return await res.text();
52+
} catch (err) {
53+
console.error(err);
54+
}
55+
}
56+
57+
function parseSDKs(sdksData: string) {
58+
const baseParts = sdksData
59+
.split(
60+
'--------------------------------------------------------------------------------',
61+
)
62+
.slice(1, -1);
63+
64+
return baseParts.map((item) => {
65+
const lines = item.slice(1, -1).split('\n');
66+
const id = lines.slice(-1)[0].split(' ').slice(-1)[0].trim();
67+
const title = lines[0].split(' ')[0].trim();
68+
const url = lines[0].split(' ').slice(-1)[0].trim();
69+
const description = lines.slice(2, -2).join('\n').trim();
70+
71+
return {
72+
id,
73+
title,
74+
url,
75+
description,
76+
};
77+
});
78+
}

0 commit comments

Comments
 (0)