mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
chore: validate block-superblock relationship (#62126)
This commit is contained in:
committed by
GitHub
parent
d19e7ede11
commit
97f759a8b7
@@ -17,7 +17,8 @@ const {
|
||||
getLanguageConfig,
|
||||
getCurriculumStructure,
|
||||
getBlockStructure,
|
||||
getSuperblockStructure
|
||||
getSuperblockStructure,
|
||||
getBlockStructurePath
|
||||
} = require('./file-handler');
|
||||
|
||||
/**
|
||||
@@ -206,7 +207,10 @@ const superBlockToFilename = Object.entries(superBlockNames).reduce(
|
||||
* @returns {Array<Object>} Array of superblock structure objects with filename, name, and blocks
|
||||
* @throws {Error} When a superblock file is not found
|
||||
*/
|
||||
function addSuperblockStructure(superblocks) {
|
||||
function addSuperblockStructure(
|
||||
superblocks,
|
||||
showComingSoon = process.env.SHOW_UPCOMING_CHANGES === 'true'
|
||||
) {
|
||||
debug(`Building structure for ${superblocks.length} superblocks`);
|
||||
|
||||
const superblockStructures = superblocks.map(superblockFilename => {
|
||||
@@ -218,7 +222,7 @@ function addSuperblockStructure(superblocks) {
|
||||
return {
|
||||
name: superblockName,
|
||||
blocks: transformSuperBlock(getSuperblockStructure(superblockFilename), {
|
||||
showComingSoon: process.env.SHOW_UPCOMING_CHANGES === 'true'
|
||||
showComingSoon
|
||||
})
|
||||
};
|
||||
});
|
||||
@@ -263,8 +267,36 @@ function getSuperblocks(
|
||||
.map(({ name }) => name);
|
||||
}
|
||||
|
||||
function validateBlocks(superblocks, blockStructureDir) {
|
||||
const withSuperblockStructure = addSuperblockStructure(superblocks, true);
|
||||
const blockInSuperblocks = withSuperblockStructure
|
||||
.flatMap(({ blocks }) => blocks)
|
||||
.map(b => b.dashedName);
|
||||
for (const block of blockInSuperblocks) {
|
||||
const blockPath = getBlockStructurePath(block);
|
||||
if (!fs.existsSync(blockPath)) {
|
||||
throw Error(
|
||||
`Block "${block}" is in a superblock, but has no block structure file at ${blockPath}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const blockStructureFiles = fs
|
||||
.readdirSync(blockStructureDir)
|
||||
.map(file => path.basename(file, '.json'));
|
||||
|
||||
for (const block of blockStructureFiles) {
|
||||
if (!blockInSuperblocks.includes(block)) {
|
||||
throw Error(
|
||||
`Block "${block}" has a structure file, ${getBlockStructurePath(block)}, but is not in a superblock`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function buildCurriculum(lang, filters) {
|
||||
const contentDir = getContentDir(lang);
|
||||
const blockStructureDir = getLanguageConfig(lang).blockStructureDir;
|
||||
const builder = new SuperblockCreator({
|
||||
blockCreator: getBlockCreator(lang, !isEmpty(filters))
|
||||
});
|
||||
@@ -277,9 +309,12 @@ async function buildCurriculum(lang, filters) {
|
||||
debug(`Found ${curriculum.superblocks.length} superblocks to build`);
|
||||
debug(`Found ${curriculum.certifications.length} certifications to build`);
|
||||
|
||||
validateBlocks(curriculum.superblocks, blockStructureDir);
|
||||
|
||||
const superblockList = addBlockStructure(
|
||||
addSuperblockStructure(curriculum.superblocks)
|
||||
);
|
||||
|
||||
const fullSuperblockList = applyFilters(superblockList, filters);
|
||||
const fullCurriculum = { certifications: { blocks: {} } };
|
||||
|
||||
|
||||
@@ -198,6 +198,7 @@ function getLanguageConfig(
|
||||
exports.getContentConfig = getContentConfig;
|
||||
exports.getContentDir = getContentDir;
|
||||
exports.getBlockStructure = getBlockStructure;
|
||||
exports.getBlockStructurePath = getBlockStructurePath;
|
||||
exports.getSuperblockStructure = getSuperblockStructure;
|
||||
exports.getCurriculumStructure = getCurriculumStructure;
|
||||
exports.writeBlockStructure = writeBlockStructure;
|
||||
|
||||
Reference in New Issue
Block a user