|
| 1 | +/** |
| 2 | + * @fileoverview Data for version selectors |
| 3 | + * @author Milos Djermanovic |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Requirements |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +const eleventyFetch = require("@11ty/eleventy-fetch"); |
| 11 | + |
| 12 | +//----------------------------------------------------------------------------- |
| 13 | +// Exports |
| 14 | +//----------------------------------------------------------------------------- |
| 15 | + |
| 16 | +module.exports = async function() { |
| 17 | + |
| 18 | + const thisBranch = process.env.BRANCH; |
| 19 | + const thisVersion = require("../../package.json").version; |
| 20 | + |
| 21 | + // Fetch the current list of ESLint versions from the `main` branch on GitHub |
| 22 | + const url = "https://raw.githubusercontent.com/eslint/eslint/main/docs/src/_data/versions.json"; |
| 23 | + |
| 24 | + const data = await eleventyFetch(url, { |
| 25 | + duration: "1d", // Cache for local development. Netlify does not keep this cache and will therefore always fetch from GitHub. |
| 26 | + type: "json" |
| 27 | + }); |
| 28 | + |
| 29 | + const { items } = data; |
| 30 | + |
| 31 | + let foundItemForThisBranch = false; |
| 32 | + |
| 33 | + for (const item of items) { |
| 34 | + const isItemForThisBranch = item.branch === thisBranch; |
| 35 | + |
| 36 | + foundItemForThisBranch ||= isItemForThisBranch; |
| 37 | + |
| 38 | + const isNumberVersion = /^\d/.test(item.version); // `false` for HEAD |
| 39 | + |
| 40 | + if (isNumberVersion) { |
| 41 | + |
| 42 | + // Make sure the version is correct |
| 43 | + if (isItemForThisBranch) { |
| 44 | + item.version = thisVersion; |
| 45 | + } |
| 46 | + |
| 47 | + item.display = `v${item.version}`; |
| 48 | + } else { |
| 49 | + item.display = item.version; |
| 50 | + } |
| 51 | + |
| 52 | + if (isItemForThisBranch) { |
| 53 | + item.selected = true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Add an empty item if this is not a production branch |
| 58 | + if (!foundItemForThisBranch) { |
| 59 | + items.unshift({ |
| 60 | + version: "", |
| 61 | + branch: "", |
| 62 | + display: "", |
| 63 | + path: "", |
| 64 | + selected: true |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + return data; |
| 69 | +} |
0 commit comments