Files
freeCodeCamp/tools/challenge-helper-scripts/create-quiz.ts
2026-02-27 18:22:08 +05:30

180 lines
4.6 KiB
TypeScript

import fs from 'fs/promises';
import path from 'path';
import { prompt } from 'inquirer';
import { format } from 'prettier';
import { ObjectId } from 'bson';
import { SuperBlocks } from '@freecodecamp/shared/config/curriculum';
import {
createBlockFolder,
writeBlockStructure
} from '@freecodecamp/curriculum/file-handler';
import { superBlockToFilename } from '@freecodecamp/curriculum/build-curriculum';
import { createQuizFile, getAllBlocks, validateBlockName } from './utils.js';
import { getBaseMeta } from './helpers/get-base-meta.js';
import { updateSimpleSuperblockStructure } from './helpers/create-project.js';
const helpCategories = [
'HTML-CSS',
'JavaScript',
'Backend Development',
'Python'
] as const;
type BlockInfo = {
title: string;
intro: string[];
};
type SuperBlockInfo = {
blocks: Record<string, BlockInfo>;
};
type IntroJson = Record<SuperBlocks, SuperBlockInfo>;
interface CreateQuizArgs {
superBlock: SuperBlocks;
block: string;
helpCategory: string;
title?: string;
questionCount: number;
}
async function createQuiz({
superBlock,
block,
helpCategory,
questionCount,
title
}: CreateQuizArgs) {
if (!title) {
title = block;
}
await updateIntroJson(superBlock, block, title);
const challengeId = new ObjectId();
await createMetaJson(block, title, helpCategory, challengeId);
await createQuizChallenge({ challengeId, block, title, questionCount });
const superblockFilename = (
superBlockToFilename as Record<SuperBlocks, string>
)[superBlock];
void updateSimpleSuperblockStructure(block, { order: 0 }, superblockFilename);
}
async function updateIntroJson(
superBlock: SuperBlocks,
block: string,
title: string
) {
const introJsonPath = path.resolve(
__dirname,
'../../client/i18n/locales/english/intro.json'
);
const newIntro = await parseJson<IntroJson>(introJsonPath);
newIntro[superBlock].blocks[block] = {
title,
intro: ['', '']
};
void withTrace(
fs.writeFile,
introJsonPath,
await format(JSON.stringify(newIntro), { parser: 'json' })
);
}
async function createMetaJson(
block: string,
title: string,
helpCategory: string,
challengeId: ObjectId
) {
const newMeta = getBaseMeta('Quiz');
newMeta.name = title;
newMeta.dashedName = block;
newMeta.helpCategory = helpCategory;
newMeta.challengeOrder = [{ id: challengeId.toString(), title: title }];
await writeBlockStructure(block, newMeta);
}
async function createQuizChallenge({
block,
challengeId,
title,
questionCount
}: {
block: string;
challengeId: ObjectId;
title: string;
questionCount: number;
}) {
createQuizFile({
challengeId,
projectPath: await createBlockFolder(block),
title: title,
dashedName: block,
questionCount: questionCount
});
}
function parseJson<JsonSchema>(filePath: string) {
return withTrace(fs.readFile, filePath, 'utf8').then(
// unfortunately, withTrace does not correctly infer that the third argument
// is a string, so it uses the (path, options?) overload and we have to cast
// result to string.
result => JSON.parse(result as string) as JsonSchema
);
}
// fs Promise functions return errors, but no stack trace. This adds back in
// the stack trace.
function withTrace<Args extends unknown[], Result>(
fn: (...x: Args) => Promise<Result>,
...args: Args
): Promise<Result> {
return fn(...args).catch((reason: Error) => {
throw Error(reason.message);
});
}
void getAllBlocks()
.then(existingBlocks =>
prompt([
{
name: 'superBlock',
message: 'Which certification does this belong to?',
default: SuperBlocks.RespWebDesignV9,
type: 'list',
choices: Object.values(SuperBlocks)
},
{
name: 'block',
message: 'What is the dashed name (in kebab-case) for this quiz?',
validate: (block: string) => validateBlockName(block, existingBlocks),
filter: (block: string) => {
return block.toLowerCase().trim();
}
},
{
name: 'title',
default: ({ block }: { block: string }) => block
},
{
name: 'helpCategory',
message: 'Choose a help category',
default: 'HTML-CSS',
type: 'list',
choices: helpCategories
},
{
name: 'questionCount',
message: 'Should this quiz have either ten or twenty questions?',
default: 20,
type: 'list',
choices: [20, 10]
}
])
)
.then(async (args: CreateQuizArgs) => await createQuiz(args))
.then(() => console.log('All set. Restart the client to see the changes.'));