Files
freeCodeCamp/tools/challenge-editor/api/utils/get-steps.ts
Oliver Eyton-Williams 4ff00922da refactor: fix hidden eslint errors (#49365)
* refactor: explicit types for validate

* refactor: explicit return types for ui-components

* refactor: use exec instead of match

* refactor: add lots more boundary types

* refactor: more eslint warnings

* refactor: more explicit exports

* refactor: more explicit types

* refactor: even more explicit types

* fix: relax type contrainsts for superblock-order

* refactor: final boundaries

* refactor: avoid using 'object' type

* fix: use named import for captureException

This uses TypeScript (which works) instead of import/namespace
(which doesn't) to check if captureException exists in sentry/gatsby
(it does)
2023-02-13 07:13:50 -08:00

45 lines
1.2 KiB
TypeScript

import { readdir, readFile } from 'fs/promises';
import { join } from 'path';
import matter from 'gray-matter';
import { PartialMeta } from '../interfaces/partial-meta';
import { CHALLENGE_DIR, META_DIR } from '../configs/paths';
const getFileOrder = (id: string, meta: PartialMeta) => {
return meta.challengeOrder.findIndex(([f]) => f === id);
};
type Step = {
name: string;
id: string;
path: string;
};
export const getSteps = async (sup: string, block: string): Promise<Step[]> => {
const filePath = join(CHALLENGE_DIR, sup, block);
const metaPath = join(META_DIR, block, 'meta.json');
const metaData = JSON.parse(await readFile(metaPath, 'utf8')) as PartialMeta;
const stepFilenames = await readdir(filePath);
const stepData = await Promise.all(
stepFilenames.map(async filename => {
const stepPath = join(filePath, filename);
const step = await readFile(stepPath, 'utf8');
const frontMatter = matter(step);
return {
name: frontMatter.data.title as string,
id: frontMatter.data.id as string,
path: filename
};
})
);
return stepData.sort(
(a, b) => getFileOrder(a.id, metaData) - getFileOrder(b.id, metaData)
);
};