Files
freeCodeCamp/tools/challenge-helper-scripts/helpers/get-step-template.ts
Oliver Eyton-Williams 7216ca55cc refactor: organise TypeScript config and migrate helpers (#44747)
* feat: allow more 1000 steps to be created at once

* refactor: start migrating to typescript

* refactor: delete-step to ts

* refactor: migrated some helpers

* refactor: migrate create-empty-steps

* refactor: migrate create-step-between

* refactor: finish migrating to TS

* refactor: migrate tests

* fix: ensure mock.restore is done after each test

* fix: prevent double-tscing

* fix: repair the tests

* chore: use ts-node for scripts

We don't need the performance boost of incremental compilation and
ts-node is easier to work with

* refactor: consolidate tsconfigs

* refactor: replace gulp

* fix: use ts-node for build-curriculum

* fix: allow ts compilation of config

* feat: create and use create:config script

* fix: add /config to eslint projects

* fix: remove gulp script
2022-01-25 11:34:16 +01:00

99 lines
2.2 KiB
TypeScript

import ObjectID from 'bson-objectid';
import { insertErms } from './insert-erms';
// Builds a block
function getCodeBlock(label: string, content?: string) {
return `\`\`\`${label}
${typeof content !== 'undefined' ? content : ''}
\`\`\`\n`;
}
// Builds a section
function getSeedSection(content: string, label: string) {
return content
? `
## --${label}--
${content}`
: '';
}
type StepOptions = {
challengeId: ObjectID;
challengeSeeds: Record<string, ChallengeSeed>;
stepBetween: boolean;
stepNum: number;
};
export interface ChallengeSeed {
contents: string;
ext: string;
editableRegionBoundaries: number[];
head?: string;
tail?: string;
}
// Build the base markdown for a step
function getStepTemplate({
challengeId,
challengeSeeds,
stepBetween,
stepNum
}: StepOptions): string {
const seedTexts = Object.values(challengeSeeds)
.map(({ contents, ext, editableRegionBoundaries }: ChallengeSeed) => {
let fullContents = contents;
if (editableRegionBoundaries.length >= 2) {
fullContents = insertErms(contents, editableRegionBoundaries);
}
return getCodeBlock(ext, fullContents);
})
.join('\n');
const seedHeads = Object.values(challengeSeeds)
.filter(({ head }: ChallengeSeed) => head)
.map(({ ext, head }: ChallengeSeed) => getCodeBlock(ext, head))
.join('\n');
const seedTails = Object.values(challengeSeeds)
.filter(({ tail }: ChallengeSeed) => tail)
.map(({ ext, tail }: ChallengeSeed) => getCodeBlock(ext, tail))
.join('\n');
const descStepNum = stepBetween ? stepNum + 1 : stepNum;
const stepDescription = `${
stepBetween ? 'new ' : ''
}step ${descStepNum} instructions`;
const seedChallengeSection = getSeedSection(seedTexts, 'seed-contents');
const seedHeadSection = getSeedSection(seedHeads, 'before-user-code');
const seedTailSection = getSeedSection(seedTails, 'after-user-code');
return (
`---
id: ${challengeId.toString()}
title: Step ${stepNum}
challengeType: 0
dashedName: step-${stepNum}
---
# --description--
${stepDescription}
# --hints--
Test 1
${getCodeBlock('js')}
# --seed--` +
seedChallengeSection +
seedHeadSection +
seedTailSection
);
}
export { getStepTemplate };