Skip to content

Commit 43f5194

Browse files
committed
Versions path fix
1 parent 1711d67 commit 43f5194

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

website/build.mjs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDefaultVersion } from './scripts/helpers.mjs';
1+
import { expandVersionsConfig, getDefaultVersion } from './scripts/helpers.mjs';
22
import fm from 'front-matter';
33
import { JSDOM } from 'jsdom';
44
import { Marked } from 'marked';
@@ -20,14 +20,6 @@ let docsMenu = '';
2020
let isBlog = false;
2121
let isDocs = false;
2222

23-
async function getDefaultVersionTarget(versionFile, defaultTarget = null) {
24-
if (defaultTarget) return defaultTarget;
25-
26-
const versions = await readJson(versionFile);
27-
const defaultVersion = versions.find(v => v.default);
28-
return defaultVersion?.branch ?? defaultVersion?.tag;
29-
}
30-
3123
async function getAllMdFiles(dir) {
3224
const entries = await readdir(dir, { withFileTypes: true });
3325
const files = [];
@@ -89,12 +81,20 @@ async function buildDocsMenuForVersion(version) {
8981
return menu;
9082
}
9183

84+
function getVersionPath(version) {
85+
return version.path ?? version.label;
86+
}
87+
88+
function getBundlesPath(version) {
89+
return path.join(config.bundlesPath, version.branch ?? version.tag);
90+
}
91+
9292
async function buildVersionsMenuList(versions, currentVersion, section) {
9393
let versionsMenuHtml = '<div class="dropdown-block">';
9494
for (const v of versions) {
9595
const activityClass = v.label === currentVersion && !v.default ? ' class="active"' : '';
9696
const defaultBadge = v.default ? ' (default)' : '';
97-
const versionPath = v.default ? '' : `${ v.path ?? v.label }/`;
97+
const versionPath = v.default ? '' : `${ getVersionPath(v) }/`;
9898
versionsMenuHtml += `<a href="./${ versionPath }${ section }"${ activityClass }>${ v.label }${ defaultBadge }</a>`;
9999
}
100100
versionsMenuHtml += '</div>';
@@ -227,9 +227,10 @@ async function buildPlaygrounds(template, versions) {
227227
}
228228

229229
async function buildPlayground(template, version, versions) {
230-
const versionPath = version.branch ?? version.tag;
231-
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ versionPath }/${ config.bundleName }"></script>`;
232-
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ versionPath }/${ config.bundleNameESModules }"></script>`;
230+
const versionPath = getVersionPath(version);
231+
const bundlesPath = getBundlesPath(version);
232+
const bundleScript = `<script nomodule src="${ bundlesPath }/${ config.bundleName }"></script>`;
233+
const bundleESModulesScript = `<script type="module" src="${ bundlesPath }/${ config.bundleNameESModules }"></script>`;
233234
const babelScript = '<script src="./babel.min.js"></script>';
234235
const playgroundContent = await readFileContent(`${ config.srcDir }playground.html`);
235236
const versionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
@@ -256,11 +257,11 @@ async function buildPlayground(template, version, versions) {
256257
}
257258

258259
async function createDocsIndexes(versions) {
259-
if (BRANCH) versions = [{ label: '' }];
260+
if (BRANCH) versions = [{ path: '' }];
260261

261262
for (const version of versions) {
262263
if (version.default) continue;
263-
const versionPath = version.path ?? version.label;
264+
const versionPath = getVersionPath(version);
264265
const menuItems = await getDocsMenuItems(versionPath);
265266
const firstDocPath = path.join(config.resultDir,
266267
`${ menuItems[0].url }.html`.replace(`{docs-version}${ BRANCH ? '/' : '' }`, versionPath));
@@ -273,15 +274,17 @@ async function createDocsIndexes(versions) {
273274
async function getVersions() {
274275
if (BRANCH) {
275276
return [{
276-
label: BRANCH,
277-
default: true,
278277
branch: BRANCH,
278+
default: true,
279+
label: BRANCH,
280+
path: BRANCH,
279281
}];
280282
}
283+
281284
const versions = await readJson(config.versionsFile);
282285
echo(chalk.green('Got versions from file'));
283286

284-
return versions;
287+
return expandVersionsConfig(versions);
285288
}
286289

287290
function getTitle(content) {
@@ -294,7 +297,8 @@ async function build() {
294297
await buildBlogMenu();
295298
const mdFiles = await getAllMdFiles(config.docsDir);
296299
const versions = await getVersions();
297-
const bundlesPath = `${ config.bundlesPath }/${ await getDefaultVersionTarget(config.versionsFile, BRANCH) }`;
300+
const defaultVersion = versions.find(v => v.default);
301+
const bundlesPath = getBundlesPath(defaultVersion);
298302
const bundleScript = `<script nomodule src="${ bundlesPath }/${ config.bundleName }"></script>`;
299303
const bundleESModulesScript = `<script type="module" src="${ bundlesPath }/${ config.bundleNameESModules }"></script>`;
300304

website/config/versions.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
"default": true,
55
"branch": "master"
66
},
7-
{
8-
"label": "v3",
9-
"branch": "master"
10-
},
117
{
128
"label": "v4 (alpha)",
13-
"path": "v4",
149
"branch": "v4"
1510
}
1611
]

website/scripts/helpers.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,23 @@ export async function readJSON(filePath) {
154154
const buffer = await readFile(filePath);
155155
return JSON.parse(buffer);
156156
}
157+
158+
export function expandVersionsConfig(config) {
159+
let defaultIndex = null
160+
const $config = config.map(({ label, branch, path, default: $default }, index) => {
161+
if ($default) {
162+
if (defaultIndex !== null) throw new Error('Duplicate default');
163+
defaultIndex = index;
164+
}
165+
return {
166+
branch,
167+
default: false,
168+
label,
169+
path: path ?? label,
170+
};
171+
});
172+
173+
if (defaultIndex === null) throw new Error('Missed default');
174+
175+
return [{ ...$config[defaultIndex], default: true }, ...$config];
176+
}

website/scripts/runner.mjs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
/* eslint-disable no-console -- needed for logging */
22
import {
3-
hasDocs, copyBlogPosts, copyBabelStandalone, copyCommonFiles, buildWeb, getDefaultVersion, readJSON, buildAndCopyCoreJS,
3+
hasDocs,
4+
copyBlogPosts,
5+
copyBabelStandalone,
6+
copyCommonFiles,
7+
buildWeb,
8+
getDefaultVersion,
9+
readJSON,
10+
buildAndCopyCoreJS,
11+
expandVersionsConfig,
412
} from './helpers.mjs';
13+
import config from '../config/config.mjs';
514
import childProcess from 'node:child_process';
615
import { cp, readdir, readlink } from 'node:fs/promises';
716
import { promisify } from 'node:util';
@@ -25,8 +34,8 @@ const BUILD_ID = new Date().toISOString().replaceAll(/\D/g, '-') + Math.random()
2534
const BUILD_DIR = `${ BUILDS_ROOT_DIR }/${ BUILD_ID }/`;
2635
const BUILD_SRC_DIR = `${ BUILD_DIR }${ SRC_DIR }/`;
2736
const BUILD_DOCS_DIR = `${ BUILD_DIR }builder/`;
28-
const SITE_FILES_DIR = `${ BUILD_SRC_DIR }/website/dist/`;
29-
const VERSIONS_FILE = `${ BUILD_SRC_DIR }website/config/versions.json`;
37+
const SITE_FILES_DIR = `${ BUILD_SRC_DIR }/${ config.resultDir }`;
38+
const VERSIONS_FILE = `${ BUILD_SRC_DIR }${ config.versionsFile }`;
3039

3140
async function copyWeb() {
3241
console.log('Copying web...');
@@ -182,7 +191,7 @@ async function getVersions(targetBranch) {
182191
const versions = await readJSON(VERSIONS_FILE);
183192
console.timeEnd('Got versions');
184193

185-
return versions;
194+
return expandVersionsConfig(versions);
186195
}
187196

188197
try {
@@ -194,6 +203,7 @@ try {
194203
if (!BRANCH) {
195204
const versions = await getVersions(targetBranch);
196205
for (const version of versions) {
206+
if (version.default) continue;
197207
await copyDocsToBuilder(version);
198208
await buildAndCopyCoreJS(version, BUILD_SRC_DIR, BUNDLES_DIR, true);
199209
}

0 commit comments

Comments
 (0)