1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/middleware/contextualizers/features.js
2023-07-11 11:44:52 +00:00

50 lines
1.7 KiB
JavaScript

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)
}