aboutsummaryrefslogtreecommitdiff
path: root/docs/.vitepress/versions.ts
blob: 5fd959236171445f4a274b23a85601148568f116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { execSync } from 'node:child_process';
import * as semver from 'semver';
import { version as version_ } from '../../package.json';

function readBranchName(): string {
  return (
    execSync('git branch --show-current').toString('utf8').trim() || 'unknown'
  );
}

function readCommitHash(): string {
  return execSync('git rev-parse HEAD').toString('utf8').trim() || 'unknown';
}

function readOtherLatestReleaseTagNames(): string[] {
  const tags = execSync('git tag -l').toString('utf8').split('\n');
  const latestTagByMajor: Record<string, string> = {};
  for (const tag of tags) {
    if (!semver.valid(tag)) {
      continue;
    }

    const majorVersion = semver.major(tag);
    if (majorVersion < 6) {
      continue;
    }

    const latestTag = latestTagByMajor[majorVersion];
    if (latestTag == null || semver.lt(latestTag, tag)) {
      latestTagByMajor[majorVersion] = tag;
    }
  }

  return Object.values(latestTagByMajor).sort(semver.rcompare);
}

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

const otherVersions = readOtherLatestReleaseTagNames();
export const isReleaseBranch = /^v\d+$/.test(branchName);

/**
 * The text of the version banner describing the current version.
 *
 * This is `null` in production and thus should not be displayed.
 */
export const versionBannerInfix: string | null = (() => {
  if (deployContext === 'production') {
    return null;
  }

  if (isReleaseBranch) {
    return '"an old version"';
  }

  if (branchName === 'next') {
    return '"the next (unreleased) version"';
  }

  return '"a development version"';
})();

/**
 * The current version of Faker from package.json.
 */
export const version = version_;

/**
 * The commit hash of the current version.
 */
export const commitHash = commitRef.substring(0, 7);

/**
 * The version label to display in the top-right corner of the site.
 */
export const versionLabel = isReleaseBranch ? `v${version}` : branchName;

/**
 * The links to other versions of the documentation.
 */
export const versionLinks = [
  {
    version: 'next',
    link: 'https://next.fakerjs.dev/',
  },
  ...otherVersions.map((version) => ({
    version,
    link: `https://v${semver.major(version)}.fakerjs.dev/`,
  })),
]
  // Don't link to the current branch's version.
  .filter(({ link }) => link !== `https://${branchName}.fakerjs.dev/`);

/**
 * The name of the Algolia index to use for search.
 */
export const algoliaIndex = isReleaseBranch
  ? `fakerjs-v${semver.major(version)}`
  : 'fakerjs-next';