|
1 | 1 | import { readFile } from 'fs/promises'; |
2 | 2 | import { join } from 'path'; |
3 | | -import { SUPERBLOCK_META_DIR, CHALLENGE_DIR } from '../configs/paths'; |
| 3 | +import { |
| 4 | + SUPERBLOCK_META_DIR, |
| 5 | + BLOCK_META_DIR, |
| 6 | + ENGLISH_LANG_DIR |
| 7 | +} from '../configs/paths'; |
4 | 8 | import { SuperBlockMeta } from '../interfaces/superblock-meta'; |
| 9 | +import { PartialMeta } from '../interfaces/partial-meta'; |
| 10 | +import { Intro } from '../interfaces/intro'; |
5 | 11 |
|
6 | 12 | type Block = { |
7 | 13 | name: string; |
8 | 14 | path: string; |
9 | 15 | }; |
10 | 16 |
|
11 | | -export const getModules = async (chap: string): Promise<string[]> => { |
12 | | - const superBlockDataPath = join( |
13 | | - SUPERBLOCK_META_DIR, |
14 | | - 'full-stack-developer' + '.json' |
15 | | - ); |
| 17 | +type Module = { |
| 18 | + name: string; |
| 19 | + path: string; |
| 20 | +}; |
| 21 | + |
| 22 | +type BlockLocation = { |
| 23 | + blocks: Block[]; |
| 24 | + currentModule: string; |
| 25 | + currentChapter: string; |
| 26 | +}; |
| 27 | + |
| 28 | +type ModuleLocation = { |
| 29 | + modules: Module[]; |
| 30 | + currentChapter: string; |
| 31 | + currentSuperBlock: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export const getModules = async ( |
| 35 | + superBlock: string, |
| 36 | + chap: string |
| 37 | +): Promise<ModuleLocation> => { |
| 38 | + const superBlockDataPath = join(SUPERBLOCK_META_DIR, superBlock + '.json'); |
16 | 39 |
|
17 | 40 | const superBlockMetaFile = await readFile(superBlockDataPath, { |
18 | 41 | encoding: 'utf8' |
19 | 42 | }); |
20 | 43 | const superBlockMeta = JSON.parse(superBlockMetaFile) as SuperBlockMeta; |
21 | 44 |
|
| 45 | + const introDataPath = join(ENGLISH_LANG_DIR, 'intro.json'); |
| 46 | + const introFile = await readFile(introDataPath, { |
| 47 | + encoding: 'utf8' |
| 48 | + }); |
| 49 | + const introData = JSON.parse(introFile) as Intro; |
| 50 | + |
| 51 | + const chapters = Object.entries(introData[superBlock]['chapters']!); |
| 52 | + |
22 | 53 | const chapter = superBlockMeta.chapters!.filter( |
23 | 54 | x => x.dashedName === chap |
24 | 55 | )[0]; |
25 | 56 |
|
26 | | - return await Promise.all( |
27 | | - chapter.modules!.map(async module => module.dashedName) |
| 57 | + const chapterTrueName = chapters.filter(x => x[0] === chap)[0][1]; |
| 58 | + |
| 59 | + let modules: Module[] = []; |
| 60 | + |
| 61 | + modules = await Promise.all( |
| 62 | + chapter.modules!.map(module => { |
| 63 | + const modules = Object.entries(introData[superBlock]['modules']!); |
| 64 | + const moduleTrueName = modules.filter( |
| 65 | + x => x[0] === module.dashedName |
| 66 | + )[0][1]; |
| 67 | + return { name: moduleTrueName, path: 'modules/' + module.dashedName }; |
| 68 | + }) |
28 | 69 | ); |
| 70 | + |
| 71 | + return { |
| 72 | + modules: modules, |
| 73 | + currentChapter: chapterTrueName, |
| 74 | + currentSuperBlock: introData[superBlock].title |
| 75 | + }; |
29 | 76 | }; |
30 | 77 |
|
31 | | -export const getBlocks = async (module: string): Promise<Block[]> => { |
32 | | - const superBlockDataPath = join( |
33 | | - SUPERBLOCK_META_DIR, |
34 | | - 'full-stack-developer' + '.json' |
35 | | - ); |
| 78 | +export const getBlocks = async ( |
| 79 | + superBlock: string, |
| 80 | + chapterName: string, |
| 81 | + moduleName: string |
| 82 | +): Promise<BlockLocation> => { |
| 83 | + const superBlockDataPath = join(SUPERBLOCK_META_DIR, superBlock + '.json'); |
36 | 84 |
|
37 | 85 | const superBlockMetaFile = await readFile(superBlockDataPath, { |
38 | 86 | encoding: 'utf8' |
39 | 87 | }); |
40 | 88 | const superBlockMeta = JSON.parse(superBlockMetaFile) as SuperBlockMeta; |
41 | 89 |
|
42 | | - const foundModule = superBlockMeta |
43 | | - .chapters!.flatMap(x => x.modules) |
44 | | - .filter(x => x.dashedName === module)[0]; |
| 90 | + const introDataPath = join(ENGLISH_LANG_DIR, 'intro.json'); |
| 91 | + const introFile = await readFile(introDataPath, { |
| 92 | + encoding: 'utf8' |
| 93 | + }); |
| 94 | + const introData = JSON.parse(introFile) as Intro; |
| 95 | + |
| 96 | + const modules = Object.entries(introData[superBlock]['modules']!); |
| 97 | + |
| 98 | + const moduleTrueName = modules.filter(x => x[0] === moduleName)[0][1]; |
| 99 | + |
| 100 | + const chapters = Object.entries(introData[superBlock]['chapters']!); |
| 101 | + |
| 102 | + const chapterTrueName = chapters.filter(x => x[0] === chapterName)[0][1]; |
| 103 | + |
| 104 | + const foundChapter = superBlockMeta.chapters?.filter( |
| 105 | + chapter => chapter.dashedName === chapterName |
| 106 | + )[0]; |
| 107 | + |
| 108 | + const foundModule = foundChapter?.modules.filter( |
| 109 | + module => module.dashedName === moduleName |
| 110 | + )[0]; |
45 | 111 |
|
46 | 112 | let blocks: { name: string; path: string }[] = []; |
47 | 113 |
|
48 | 114 | blocks = await Promise.all( |
49 | | - foundModule.blocks!.map(async block => { |
50 | | - const filePath = join(CHALLENGE_DIR, block); |
| 115 | + foundModule!.blocks!.map(async block => { |
| 116 | + const blockStructurePath = join(BLOCK_META_DIR, block + '.json'); |
| 117 | + const blockMetaFile = await readFile(blockStructurePath, { |
| 118 | + encoding: 'utf8' |
| 119 | + }); |
| 120 | + const blockMeta = JSON.parse(blockMetaFile) as PartialMeta; |
51 | 121 | return { |
52 | | - name: block, |
53 | | - path: filePath |
| 122 | + name: blockMeta.name, |
| 123 | + path: block |
54 | 124 | }; |
55 | 125 | }) |
56 | 126 | ); |
57 | | - return blocks; |
| 127 | + |
| 128 | + return { |
| 129 | + blocks: blocks, |
| 130 | + currentModule: moduleTrueName, |
| 131 | + currentChapter: chapterTrueName |
| 132 | + }; |
58 | 133 | }; |
0 commit comments