|
| 1 | +import { readFileSync, readdirSync, rmSync, mkdirSync, writeFileSync } from 'fs'; |
| 2 | +const CONTENT = './src/content/'; |
| 3 | +const NONCOURSEROUTES = './src/routes/(content-single)/(non-course)'; |
| 4 | +const COURSEROUTES = './src/routes/(content-single)'; |
| 5 | +const types = readdirSync(CONTENT); |
| 6 | + |
| 7 | +for (const type of types) { |
| 8 | + console.log('EXECUTE', type); |
| 9 | + |
| 10 | + // Clean up directories |
| 11 | + if (type === 'course') { |
| 12 | + rmSync(`${COURSEROUTES}/${type}`, { recursive: true, force: true }); |
| 13 | + mkdirSync(`${COURSEROUTES}/${type}`); |
| 14 | + } else { |
| 15 | + rmSync(`${NONCOURSEROUTES}/${type}`, { recursive: true, force: true }); |
| 16 | + mkdirSync(`${NONCOURSEROUTES}/${type}`); |
| 17 | + } |
| 18 | + |
| 19 | + // Move file from md, to route |
| 20 | + if (type === 'course') { |
| 21 | + const courses = readdirSync(`${CONTENT}/${type}`); |
| 22 | + for (const course of courses) { |
| 23 | + // Read File |
| 24 | + const md = readFileSync(`${CONTENT}/${type}/${course}/index.md`, 'utf8'); |
| 25 | + |
| 26 | + // add types |
| 27 | + const finalMd = md.replace('---', `---\ntype: ${type}`); |
| 28 | + |
| 29 | + // Create new directory |
| 30 | + const dirName = course.replace('.md', ''); |
| 31 | + mkdirSync(`${COURSEROUTES}/${type}/${dirName}`); |
| 32 | + |
| 33 | + // Write +page as new file |
| 34 | + writeFileSync(`${COURSEROUTES}/${type}/${dirName}/+page.md`, finalMd); |
| 35 | + |
| 36 | + // Lessons |
| 37 | + const lessons = readdirSync(`${CONTENT}/${type}/${course}/lesson`); |
| 38 | + if (lessons) { |
| 39 | + mkdirSync(`${COURSEROUTES}/${type}/${course}/lesson`); |
| 40 | + } |
| 41 | + for (const lesson of lessons) { |
| 42 | + // Read File |
| 43 | + const md = readFileSync(`${CONTENT}/${type}/${course}/lesson/${lesson}`, 'utf8'); |
| 44 | + |
| 45 | + // add types |
| 46 | + const finalMd = md.replace('---', `---\ntype: lesson`); |
| 47 | + |
| 48 | + // Create new directory |
| 49 | + const dirName = lesson.replace('.md', ''); |
| 50 | + mkdirSync(`${COURSEROUTES}/${type}/${course}/lesson/${dirName}`); |
| 51 | + |
| 52 | + // Write +page as new file |
| 53 | + writeFileSync(`${COURSEROUTES}/${type}/${course}/lesson/${dirName}/+page.md`, finalMd); |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + const files = readdirSync(`${CONTENT}/${type}`); |
| 58 | + for (const file of files) { |
| 59 | + // Read File |
| 60 | + const md = readFileSync(`${CONTENT}/${type}/${file}`, 'utf8'); |
| 61 | + |
| 62 | + // add types |
| 63 | + const finalMd = md.replace('---', `---\ntype: ${type}`); |
| 64 | + |
| 65 | + // Create new directory |
| 66 | + const dirName = file.replace('.md', ''); |
| 67 | + mkdirSync(`${NONCOURSEROUTES}/${type}/${dirName}`); |
| 68 | + |
| 69 | + // Write +page as new file |
| 70 | + writeFileSync(`${NONCOURSEROUTES}/${type}/${dirName}/+page.md`, finalMd); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments