Skip to content

Commit 7bfd3dd

Browse files
committed
chore: get rid of hard-coded versions from multiversion docs site
- Gatsby multiversion is quite complex - This handles all automatically - Fetches two previous majors's latest minors - remove useless packages Refs: HDS-2883
1 parent e65a155 commit 7bfd3dd

File tree

12 files changed

+1005
-279
lines changed

12 files changed

+1005
-279
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [4.X.X] - Month, XX, 202X
99

10+
Updated documentation build process for multi-version docs by replacing the `gatsby-source-git` setup with a local filesystem-based source and adding automatic version detection from the documentation directory structure.
11+
1012
### React
1113

1214
#### Breaking
@@ -65,7 +67,6 @@ Changes that are not related to specific components
6567

6668
#### Fixed
6769

68-
- [Component] What bugs/typos are fixed?
6970
- [Checkbox] Stories to include full interactivities.
7071

7172
### Figma

site/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ yarn-error.log
3939

4040
# icon&group mapping
4141
icon_group.json
42+
43+
# Local version downloads
44+
.previous-versions

site/gatsby-config.js

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,94 @@ require("dotenv").config({
22
path: `.env.${process.env.NODE_ENV}`,
33
})
44

5+
const fs = require('node:fs');
6+
const path = require('node:path');
7+
58
const buildSingleVersion = process.env.BUILD_SINGLE_VERSION === 'true';
6-
const versionsFromGit = buildSingleVersion ? [] : require('./src/data/versionsFromGit.json');
79

8-
const gitSources = versionsFromGit.map(version => ({
9-
resolve: 'gatsby-source-git',
10-
options: {
11-
name: `docs-release-${version}`,
12-
remote: `https://github.com/City-of-Helsinki/helsinki-design-system`,
13-
branch: `release-${version}`,
14-
patterns: 'site/src/docs/**',
15-
},
16-
}));
10+
// Extract version number from directory name
11+
function extractVersionFromDir(dir) {
12+
const versionPart = dir.replace('helsinki-design-system-', '');
13+
// Extract only the semantic version part (X.Y.Z)
14+
// This ensures we extract just the version even if there's extra text after it
15+
const match = versionPart.match(/^(\d+\.\d+\.\d+)/);
16+
return match ? match[1] : null;
17+
}
18+
19+
// Sort versions descending (newest first)
20+
function sortVersionsDesc(a, b) {
21+
const aParts = a.split('.').map(Number);
22+
const bParts = b.split('.').map(Number);
23+
for (let i = 0; i < 3; i++) {
24+
if (bParts[i] !== aParts[i]) return bParts[i] - aParts[i];
25+
}
26+
return 0;
27+
}
28+
29+
// Auto-detect versions from .previous-versions directory
30+
function getPreviousVersions() {
31+
const previousVersionsDir = path.join(__dirname, '.previous-versions');
32+
if (!fs.existsSync(previousVersionsDir)) return [];
33+
34+
try {
35+
return fs.readdirSync(previousVersionsDir)
36+
.filter(item => {
37+
const itemPath = path.join(previousVersionsDir, item);
38+
try {
39+
return fs.statSync(itemPath).isDirectory() && item.startsWith('helsinki-design-system-');
40+
} catch {
41+
return false;
42+
}
43+
})
44+
.map(extractVersionFromDir)
45+
.filter(Boolean)
46+
.sort(sortVersionsDesc)
47+
.reduce((acc, version) => {
48+
// Ensure we only keep the latest minor for each major version
49+
const major = version.split('.')[0];
50+
if (!acc.some(v => v.split('.')[0] === major)) {
51+
acc.push(version);
52+
}
53+
return acc;
54+
}, [])
55+
.slice(0, 2); // Get latest minors from the previous two majors
56+
} catch (error) {
57+
console.warn('Warning: Could not read .previous-versions directory:', error.message);
58+
return [];
59+
}
60+
}
61+
62+
const previousVersions = buildSingleVersion ? [] : getPreviousVersions();
63+
64+
// Get current version from package.json for siteMetadata
65+
const packageJsonPath = path.join(__dirname, 'package.json');
66+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
67+
const currentVersion = packageJson.version;
68+
69+
// Combine current and previous versions into a single array
70+
const versions = buildSingleVersion ? [currentVersion] : [currentVersion, ...previousVersions];
71+
72+
// Create local sources using gatsby-source-filesystem
73+
const previousVersionSources = previousVersions.map(version => {
74+
const docsPath = path.join(__dirname, `.previous-versions/helsinki-design-system-${version}/site/src/docs`);
75+
76+
// Verify the path exists before adding it
77+
if (fs.existsSync(docsPath)) {
78+
return {
79+
resolve: `gatsby-source-filesystem`,
80+
options: {
81+
name: `docs-release-${version}`,
82+
path: docsPath,
83+
},
84+
};
85+
}
86+
return null;
87+
}).filter(Boolean);
1788

1889
module.exports = {
1990
pathPrefix: process.env.PATH_PREFIX,
2091
siteMetadata: {
92+
versions,
2193
title: `Helsinki Design System`,
2294
description: `Documentation for the Helsinki Design System`,
2395
siteUrl: process.env.SITE_URL,
@@ -163,7 +235,7 @@ module.exports = {
163235
path: `${__dirname}/src/docs`,
164236
},
165237
},
166-
...gitSources,
238+
...previousVersionSources,
167239
{
168240
resolve: `gatsby-plugin-mdx`,
169241
options: {

0 commit comments

Comments
 (0)