|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { forEach } from 'lodash'; |
| 3 | +import { BaseProcesser } from "./BaseProcesser"; |
| 4 | +import exampleConfig from './exampleConfig'; |
| 5 | +import * as path from "path"; |
| 6 | +// import * as path from 'path'; |
| 7 | + |
| 8 | +export class ExampleProcessor extends BaseProcesser { |
| 9 | + private themePath = path.join(__dirname, '../themes/markdown'); |
| 10 | + private exampleOutputPath = path.join(__dirname, '../md_out/examples'); |
| 11 | + private homePageFile = 'examples_readme.hbs'; |
| 12 | + private srcExampleHomeTheme = path.join(this.themePath, this.homePageFile); |
| 13 | + private destExampleHomePage = path.join(__dirname, '../md_out/examples/README.md'); |
| 14 | + private examplePageFile = 'example_entity_page.hbs'; |
| 15 | + private srcExamplePageTheme = path.join(this.themePath, this.examplePageFile); |
| 16 | + |
| 17 | + /** |
| 18 | + * Run the processor |
| 19 | + * @param hbs |
| 20 | + */ |
| 21 | + public run(hbs): void { |
| 22 | + this.createDir(); |
| 23 | + this.createReadMe(hbs); |
| 24 | + this.createExamplePage(hbs); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Create example directory if not exist |
| 29 | + */ |
| 30 | + private createDir(): void { |
| 31 | + if (!fs.existsSync(this.exampleOutputPath)) { |
| 32 | + fs.mkdirSync(this.exampleOutputPath); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Create example category directory if not exist |
| 38 | + * @param fullPath |
| 39 | + */ |
| 40 | + private createDirByName(fullPath): void { |
| 41 | + if (!fs.existsSync(fullPath)) { |
| 42 | + fs.mkdirSync(fullPath); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates the readme file for examples |
| 48 | + * @param hbs |
| 49 | + */ |
| 50 | + private createReadMe(hbs): void { |
| 51 | + const exampleHomePageThemeContent = fs.readFileSync(this.srcExampleHomeTheme, 'utf8'); |
| 52 | + const template = hbs.compile(exampleHomePageThemeContent); |
| 53 | + const compiledPage = template(exampleConfig); |
| 54 | + fs.appendFileSync(this.destExampleHomePage, compiledPage, { flag: 'a' }); |
| 55 | + } |
| 56 | + |
| 57 | + private createExamplePage(hbs): void { |
| 58 | + const examplePageThemeContent = fs.readFileSync(this.srcExamplePageTheme, 'utf8'); |
| 59 | + const template = hbs.compile(examplePageThemeContent); |
| 60 | + forEach(exampleConfig, (category) => { |
| 61 | + const categoryKey = category.key; |
| 62 | + const categoryDir = path.join(this.exampleOutputPath, categoryKey); |
| 63 | + |
| 64 | + // Creates the category dir |
| 65 | + this.createDirByName(categoryDir); |
| 66 | + |
| 67 | + const categoryChildren = category.children; |
| 68 | + forEach(categoryChildren, (child) => { |
| 69 | + const childKey = child.key; |
| 70 | + const examplePageName = path.join(categoryDir, `${childKey}.md`); |
| 71 | + const compiledPage = template(child); |
| 72 | + fs.appendFileSync(examplePageName, compiledPage, { flag: 'a' }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments