Skip to content

docs: link to next docs and vice versa #1438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# Required for docs/versions tests
fetch-depth: 0

- name: Install pnpm
uses: pnpm/[email protected]
Expand Down
57 changes: 54 additions & 3 deletions docs/.vitepress/versions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
import { execSync } from 'node:child_process';
import * as semver from 'semver';
import { version } from '../../package.json';

export const currentVersion = `v${version}`;
function readBranchName(): string {
return (
execSync('git branch --show-current').toString('utf8').trim() || 'unknown'
);
}

function readOtherLatestReleaseTagNames(): string[] {
const currentMajorVersion = semver.major(version);
const latestReleaseTagNames = execSync('git tag -l')
.toString('utf8')
.split('\n')
.filter((tag) => semver.valid(tag))
.reduce<Record<number, string[]>>((acc, tag) => {
const majorVersion = semver.major(tag);
// Only consider tags for our deployed website versions,
// excluding the current major version.
if (majorVersion >= 6 && majorVersion !== currentMajorVersion) {
(acc[majorVersion] = acc[majorVersion] ?? []).push(tag);
}
return acc;
}, {});
return Object.entries(latestReleaseTagNames)
.map(([major, tags]) => semver.maxSatisfying(tags, `^${major}`))
.sort(semver.rcompare);
}

// Set by netlify
const {
CONTEXT: deployContext = 'local',
BRANCH: branchName = readBranchName(),
} = process.env;

const hiddenLink =
deployContext === 'production'
? 'https://fakerjs.dev/'
: `https://${branchName}.fakerjs.dev/`;
const otherVersions = readOtherLatestReleaseTagNames();
const isReleaseBranch = /^v\d+$/.test(branchName);

export const currentVersion = isReleaseBranch ? `v${version}` : branchName;
export const oldVersions = [
{ version: 'v6.3.1', link: 'https://v6.fakerjs.dev/' },
];
{
version: 'latest',
link: 'https://fakerjs.dev/',
},
{
version: 'next',
link: 'https://next.fakerjs.dev/',
},
...otherVersions.map((version) => ({
version,
link: `https://v${semver.major(version)}.fakerjs.dev/`,
})),
].filter(({ link }) => link !== hiddenLink);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"@types/prettier": "~2.7.1",
"@types/react": "~18.0.21",
"@types/sanitize-html": "~2.6.2",
"@types/semver": "~7.3.12",
"@types/validator": "~13.7.8",
"@typescript-eslint/eslint-plugin": "~5.40.0",
"@typescript-eslint/parser": "~5.40.0",
Expand All @@ -127,6 +128,7 @@
"react-dom": "~18.2.0",
"rimraf": "~3.0.2",
"sanitize-html": "~2.7.2",
"semver": "~7.3.8",
"simple-git-hooks": "~2.8.0",
"standard-version": "~9.5.0",
"tsx": "~3.10.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions test/docs/versions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { major as semverMajor } from 'semver';
import { describe, expect, it } from 'vitest';
import { oldVersions } from '../../docs/.vitepress/versions';
import { version } from '../../package.json';

describe('docs versions', () => {
describe('oldVersions', () => {
it('should have a complete set of oldVersions', () => {
const versionText = `v${version}`;

expect(oldVersions.length).toBeGreaterThanOrEqual(2);
const currentMajorVersion = semverMajor(versionText);

expect(oldVersions[0]).toEqual({
version: 'latest',
link: 'https://fakerjs.dev/',
});
expect(oldVersions[1]).toEqual({
version: 'next',
link: 'https://next.fakerjs.dev/',
});

for (let i = 2; i < oldVersions.length; i++) {
const oldMajorVersion = semverMajor(oldVersions[i].version);
expect(oldMajorVersion).toBe(currentMajorVersion - i + 1);
}
});
});
});