|
| 1 | +import { readFile } from 'fs/promises'; |
| 2 | +import { freeCodeCampConfig, getState, ROOT } from '../tooling/env.js'; |
| 3 | +import { CoffeeDown, parseMarkdown } from '../tooling/parser.js'; |
| 4 | +import { join } from 'path'; |
| 5 | +import { logover } from '../tooling/logger.js'; |
| 6 | + |
1 | 7 | /** |
2 | 8 | * Project config from `config/projects.json` |
3 | 9 | * @typedef {Object} Project |
|
24 | 30 | * @property {boolean} isLoading |
25 | 31 | */ |
26 | 32 |
|
| 33 | +/** |
| 34 | + * @typedef {Object} Lesson |
| 35 | + * @property {string} description |
| 36 | + * @property {[[string, string]]} tests |
| 37 | + * @property {string[]} hints |
| 38 | + * @property {[{filePath: string; fileSeed: string} | string]} seed |
| 39 | + * @property {boolean?} isForce |
| 40 | + * @property {string?} beforeAll |
| 41 | + * @property {string?} afterAll |
| 42 | + * @property {string?} beforeEach |
| 43 | + * @property {string?} afterEach |
| 44 | + */ |
| 45 | + |
27 | 46 | export const pluginEvents = { |
28 | 47 | /** |
29 | 48 | * @param {Project} project |
@@ -55,5 +74,78 @@ export const pluginEvents = { |
55 | 74 | /** |
56 | 75 | * @param {Project} project |
57 | 76 | */ |
58 | | - onLessonFailed: async project => {} |
| 77 | + onLessonFailed: async project => {}, |
| 78 | + |
| 79 | + /** |
| 80 | + * @param {string} projectDashedName |
| 81 | + * @returns {Promise<{title: string; description: string; numberOfLessons: number}>} |
| 82 | + */ |
| 83 | + getProjectMeta: async projectDashedName => { |
| 84 | + const { locale } = await getState(); |
| 85 | + const projectFilePath = join( |
| 86 | + ROOT, |
| 87 | + freeCodeCampConfig.curriculum.locales[locale], |
| 88 | + projectDashedName + '.md' |
| 89 | + ); |
| 90 | + const projectFile = await readFile(projectFilePath, 'utf8'); |
| 91 | + const coffeeDown = new CoffeeDown(projectFile); |
| 92 | + const projectMeta = coffeeDown.getProjectMeta(); |
| 93 | + // Remove `<p>` tags if present |
| 94 | + const title = parseMarkdown(projectMeta.title).replace(/<p>|<\/p>/g, ''); |
| 95 | + const description = parseMarkdown(projectMeta.description); |
| 96 | + const numberOfLessons = projectMeta.numberOfLessons; |
| 97 | + return { title, description, numberOfLessons }; |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + * @param {string} projectDashedName |
| 102 | + * @param {number} lessonNumber |
| 103 | + * @returns {Promise<Lesson>} lesson |
| 104 | + */ |
| 105 | + getLesson: async (projectDashedName, lessonNumber) => { |
| 106 | + const { locale } = await getState(); |
| 107 | + const projectFilePath = join( |
| 108 | + ROOT, |
| 109 | + freeCodeCampConfig.curriculum.locales[locale], |
| 110 | + projectDashedName + '.md' |
| 111 | + ); |
| 112 | + const projectFile = await readFile(projectFilePath, 'utf8'); |
| 113 | + const coffeeDown = new CoffeeDown(projectFile); |
| 114 | + const lesson = coffeeDown.getLesson(lessonNumber); |
| 115 | + let seed = lesson.seed; |
| 116 | + if (!seed.length) { |
| 117 | + // Check for external seed file |
| 118 | + const seedFilePath = projectFilePath.replace(/.md$/, '-seed.md'); |
| 119 | + try { |
| 120 | + const seedContent = await readFile(seedFilePath, 'utf-8'); |
| 121 | + const coffeeDown = new CoffeeDown(seedContent); |
| 122 | + seed = coffeeDown.getLesson(lessonNumber).seed; |
| 123 | + } catch (e) { |
| 124 | + if (e?.code !== 'ENOENT') { |
| 125 | + logover.debug(e); |
| 126 | + throw new Error( |
| 127 | + `Error reading external seed for lesson ${lessonNumber}` |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + const { afterAll, afterEach, beforeAll, beforeEach, isForce } = lesson; |
| 133 | + const description = parseMarkdown(lesson.description); |
| 134 | + const tests = lesson.tests.map(([testText, test]) => [ |
| 135 | + parseMarkdown(testText), |
| 136 | + test |
| 137 | + ]); |
| 138 | + const hints = lesson.hints.map(parseMarkdown); |
| 139 | + return { |
| 140 | + description, |
| 141 | + tests, |
| 142 | + hints, |
| 143 | + seed, |
| 144 | + beforeAll, |
| 145 | + afterAll, |
| 146 | + beforeEach, |
| 147 | + afterEach, |
| 148 | + isForce |
| 149 | + }; |
| 150 | + } |
59 | 151 | }; |
0 commit comments