diff options
| -rw-r--r-- | .github/workflows/ci.yml | 3 | ||||
| -rw-r--r-- | docs/.vitepress/versions.ts | 57 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | pnpm-lock.yaml | 8 | ||||
| -rw-r--r-- | test/docs/versions.spec.ts | 29 |
5 files changed, 96 insertions, 3 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2a4b87b..e22aefaf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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] diff --git a/docs/.vitepress/versions.ts b/docs/.vitepress/versions.ts index c82a5ee0..7d37619d 100644 --- a/docs/.vitepress/versions.ts +++ b/docs/.vitepress/versions.ts @@ -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); diff --git a/package.json b/package.json index fa644c93..09478a67 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index df0d1258..d08861d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,6 +9,7 @@ specifiers: '@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 @@ -35,6 +36,7 @@ specifiers: 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 @@ -55,6 +57,7 @@ devDependencies: '@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_25sstg4uu2sk4pm7xcyzuov7xq '@typescript-eslint/parser': 5.40.0_z4bbprzjrhnsfa24uvmcbu7f5q @@ -81,6 +84,7 @@ devDependencies: react-dom: [email protected] 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 @@ -660,6 +664,10 @@ packages: resolution: {integrity: sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==} dev: true + /@types/semver/7.3.12: + resolution: {integrity: sha512-WwA1MW0++RfXmCr12xeYOOC5baSC9mSb0ZqCquFzKhcoF4TvHu5MKOuXsncgZcpVFhB1pXd5hZmM0ryAoCp12A==} + dev: true + /@types/sinonjs__fake-timers/8.1.1: resolution: {integrity: sha512-0kSuKjAS0TrGLJ0M/+8MaFkGsQhZpB6pxOmvS3K8FYI72K//YmdfoW9X2qPsAKh1mkwxGD5zib9s1FIFed6E8g==} dev: true diff --git a/test/docs/versions.spec.ts b/test/docs/versions.spec.ts new file mode 100644 index 00000000..cee280fa --- /dev/null +++ b/test/docs/versions.spec.ts @@ -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); + } + }); + }); +}); |
