Oliver Eyton-Williams 339c6713d2
feat: update challenge helpers to handle id filenames (#44769)
* refactor: light tweaks for readability

* refactor: simplify metadata functions

* fix: most tests

* test: fix utils tests

* test: simplify mocks

* WIP: update get-last-step-file-content

* feat: finish create-next-step

* fix: type error

* test: provide mock meta.json for test

* refactor: get meta path from project path

* refactor: get project name from path

* refactor: simplify getProjectMetaPath further

Also removes some excessive mocking

* refactor: remove more mocks, always clear .env

* feat: update create-next-step

* feat: update create-empty steps

Also refactors slightly, so it's easier to insert steps into the meta

* docs: update challenge-helper-script docs

* feat: create-step-between

* refactor: allow metadata parse errors to propagate

* fix: convert reorderSteps to renameSteps

* refactor: create-step-between -> insert-step

* feat: update delete-step

* refactor: consolidate commands into commands.ts

* refactor: clean up and consolidation

* refactor: more cleanup

* fix: make cli args consistent

Everything accepts a single integer and nothing else

* refactor: renameSteps -> updateStepTitles

* docs: update with the names and args

* feat: add step validating meta + files are synced
2022-03-02 07:12:20 -08:00

92 lines
2.1 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>;
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,
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 stepDescription = `step ${stepNum} 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 };