1import fsExtra from 'fs-extra'; 2import semver from 'semver'; 3 4const { readdirSync, readJsonSync } = fsExtra; 5const { version, betaVersion } = readJsonSync('./package.json'); 6 7const versionContents = readdirSync('./pages/versions', { withFileTypes: true }); 8const versionDirectories = versionContents.filter(f => f.isDirectory()).map(f => f.name); 9 10/** 11 * The current latest version of the docs. 12 * This is the `package.json` version. 13 */ 14export const LATEST_VERSION = `v${version}`; 15 16/** 17 * The currently active beta version. 18 * This is the `package.json` betaVersion field. 19 * This will usually be undefined, except for during beta testing periods prior to a new release. 20 */ 21export const BETA_VERSION = betaVersion ? `v${betaVersion}` : false; 22 23/** 24 * The list of all versions supported by the docs. 25 * It's calculated from the `pages/versions` folder names, and uses the following sorting: 26 * - `unversioned` 27 * - `latest` 28 * - versions from new to old (e.g. v39.0.0, v38.0.0, v37.0.0) 29 */ 30export const VERSIONS = versionDirectories 31 .filter(dir => { 32 // show all other versions in production except 33 // those greater than the package.json version number 34 const dirVersion = semver.clean(dir); 35 if (dirVersion) { 36 return semver.lte(dirVersion, version) || dirVersion === betaVersion; 37 } 38 return true; 39 }) 40 .sort((a, b) => { 41 if (a === 'unversioned' || a === 'latest') return -1; 42 if (b === 'unversioned' || b === 'latest') return 1; 43 44 return semver.major(b) - semver.major(a); 45 }) 46 .sort((a, b) => { 47 if (a === BETA_VERSION) return -1; 48 if (b === BETA_VERSION) return 1; 49 return 0; 50 }); 51