forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.js
49 lines (40 loc) · 1.67 KB
/
features.js
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
import path from 'path'
import { ROOT } from '../../lib/constants.js'
import getApplicableVersions from '../../lib/get-applicable-versions.js'
import { getDeepDataByLanguage } from '../../lib/get-data.js'
export default function features(req, res, next) {
if (!req.context.page) return next()
Object.entries(getFeaturesByVersion(req.context.currentVersion)).forEach(
([featureName, isFeatureAvailableInCurrentVersion]) => {
req.context[featureName] = isFeatureAvailableInCurrentVersion
}
)
return next()
}
let allFeatures
const cache = new Map()
function getFeaturesByVersion(currentVersion) {
if (!cache.has(currentVersion)) {
if (!allFeatures) {
// As of Oct 2022, the `data/features/**` reading is *not* JIT.
// The `data/features` is deliberately not ignored in nodemon.json.
// See internal issue #2389
allFeatures = getDeepDataByLanguage('features', 'en')
}
const features = {}
// Determine whether the currentVersion belongs to the list of versions the feature is available in.
for (const [featureName, feature] of Object.entries(allFeatures)) {
const { versions } = feature
const applicableVersions = getApplicableVersions(
versions,
path.join(ROOT, `data/features/${featureName}.yml`)
)
// Adding the resulting boolean to the context object gives us the ability to use
// `{% if featureName ... %}` conditionals in content files.
const isFeatureAvailableInCurrentVersion = applicableVersions.includes(currentVersion)
features[featureName] = isFeatureAvailableInCurrentVersion
}
cache.set(currentVersion, features)
}
return cache.get(currentVersion)
}