xref: /expo/docs/constants/versions.js (revision cd4bd26b)
1// @preval
2
3const { readdirSync } = require('fs');
4const semver = require('semver');
5
6const { version, betaVersion } = require('../package.json');
7
8const versionContents = readdirSync('./pages/versions', { withFileTypes: true });
9const versionDirectories = versionContents.filter(f => f.isDirectory()).map(f => f.name);
10
11/**
12 * The current latest version of the docs.
13 * This is the `package.json` version.
14 */
15const LATEST_VERSION = `v${version}`;
16
17/**
18 * The currently active beta version.
19 * This is the `package.json` betaVersion field.
20 * This will usually be undefined, except for during beta testing periods prior to a new release.
21 */
22const BETA_VERSION = betaVersion ? `v${betaVersion}` : undefined;
23
24/**
25 * The list of all versions supported by the docs.
26 * It's caluclated from the `pages/versions` folder names, and uses the following sorting:
27 *   - `unversioned`
28 *   - `latest`
29 *   - versions from new to old (e.g. 39, 38, 37)
30 */
31const VERSIONS = versionDirectories
32  .filter(dir => {
33    // show all versions in dev mode
34    if (process.env.NODE_ENV !== 'production') {
35      return true;
36    }
37
38    // hide unversioned in production
39    if (dir === 'unversioned') {
40      return false;
41    }
42
43    // show all other versions in production except
44    // those greater than the package.json version number
45    const dirVersion = semver.clean(dir);
46    if (dirVersion) {
47      return semver.lte(dirVersion, version) || dirVersion === betaVersion;
48    }
49    return true;
50  })
51  .sort((a, b) => {
52    if (a === 'unversioned' || a === 'latest') return -1;
53    if (b === 'unversioned' || b === 'latest') return 1;
54
55    return semver.major(b) - semver.major(a);
56  })
57  .sort((a, b) => {
58    if (a === BETA_VERSION) return -1;
59    if (b === BETA_VERSION) return 1;
60    return 0;
61  });
62
63module.exports = {
64  VERSIONS,
65  LATEST_VERSION,
66  BETA_VERSION,
67};
68