fix(tools): curriculum command line helpers (#61831)

This commit is contained in:
Oliver Eyton-Williams
2025-09-02 16:03:28 +02:00
committed by GitHub
parent c58ba56eeb
commit 10c565828e
40 changed files with 773 additions and 1010 deletions

View File

@@ -0,0 +1,94 @@
// TODO: this belongs in create-project, but we can't test that (since it uses
// prettier) until we migrate to vitest
import {
getSuperblockStructure,
writeSuperblockStructure
} from '../../../curriculum/file-handler';
import { insertInto } from './utils';
export async function updateSimpleSuperblockStructure(
block: string,
position: { order: number },
superblockFilename: string
) {
const existing = getSuperblockStructure(superblockFilename) as {
blocks: string[];
};
const updated = {
blocks: insertInto(existing.blocks, position.order, block)
};
await writeSuperblockStructure(superblockFilename, updated);
}
function createNewChapter(chapter: string, module: string, block: string) {
return {
dashedName: chapter,
modules: [
{
dashedName: module,
blocks: [block]
}
]
};
}
function createNewModule(module: string, block: string) {
return {
dashedName: module,
blocks: [block]
};
}
type ChapterModuleSuperblockStructure = {
chapters: {
dashedName: string;
modules: {
dashedName: string;
blocks: string[];
}[];
}[];
};
export async function updateChapterModuleSuperblockStructure(
block: string,
position: { order: number; chapter: string; module: string },
superblockFilename: string
) {
const existing = getSuperblockStructure(
superblockFilename
) as ChapterModuleSuperblockStructure;
const modifiedChapter = existing.chapters.find(
chapter => chapter.dashedName === position.chapter
);
const modifiedModule = modifiedChapter?.modules.find(
module => module.dashedName === position.module
);
const updatedModule = modifiedModule
? {
...modifiedModule,
blocks: insertInto(modifiedModule.blocks, position.order, block)
}
: createNewModule(position.module, block);
const updatedChapter = modifiedChapter
? {
...modifiedChapter,
modules: modifiedModule
? modifiedChapter.modules.map(module =>
module === modifiedModule ? updatedModule : module
)
: [...modifiedChapter.modules, updatedModule]
}
: createNewChapter(position.chapter, position.module, block);
const updated = {
chapters: modifiedChapter
? existing.chapters.map(chapter =>
chapter === modifiedChapter ? updatedChapter : chapter
)
: [...existing.chapters, updatedChapter]
};
await writeSuperblockStructure(superblockFilename, updated);
}